In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to integrate scheduled tasks and mail tasks in SpringBoot". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and study and learn "how to integrate scheduled tasks and mail tasks in SpringBoot" together!
scheduled task
Projects often need to perform timed tasks, such as analyzing the previous day's log information once a day in the early hours of the morning. Spring gives us a way to perform task scheduling asynchronously
Provided:
TaskExecutor Task scheduling
TaskScheduler Task Performer Two comments:
@EnableScheduling//Enable scheduled tasks
@Scheduled //indicates when to execute
cron expression:
field allowed values allowed special characters
seconds 0-59 - * /
0-59 - * /
hours 0-23 - * /
Date 1-12 or JAN-DEC - * ? / L W C Month 0-59 - * /
Week 1-7 or SUN-SAT - * ? / L C #
cron expression example:
(0)0/20 * * * * ? indicates that the task is adjusted every 20 seconds (1) 0 0 2 1 * ? Indicates that the task is adjusted at 2 a.m. on the first day of each month (2) 0 15 10 ? * MON-FRI indicates that homework (3) is performed at 10:15 a.m. Monday through Friday. 0:15 a.m. 6L 2002-2006 Indicates the last Friday of each month from 2002 to 2006 at 10:15 a.m. Performed as (4) 0 0 10,14,16 * * ? Daily 10:00 a.m., 2:00 p.m., 4:00 p.m. (5)0 0/30 9-17 * * ? 9:00 to 5:00 every half hour during business hours (6)0 0 12 ? * WED means every Wednesday at 12 noon. (7)0 0 12 * * ? Triggers every day at 12 noon (8)0 15 10 ? * * Triggers every day at 10:15 a.m. (9)0 15 10 * * ? Triggers every day at 10:15 a.m. (10)0 15 10 * * ? * Triggers every day at 10:15 a.m. (11)0 15 10 * * ? 2005 Triggers every day at 10:15 a.m. in 2005 (12)0 * 14 * * ? Triggers every 1 minute between 2 p.m. and 2:59 p.m. every day (13)0 0/5 14 * * ? Triggers every 5 minutes between 2 p.m. and 2:55 p.m. each day (14)0 0/5 14,18 * * ? Triggers every 5 minutes between 2:00 p.m. and 2:55 p.m. and between 6:00 p.m. and 6:55 p.m. daily (15)0 0-5 14 * * ? Triggers every 1 minute between 2 p.m. and 2:05 p.m. every day (16)0 10,44 14 ? 3 WED Triggers every Wednesday in March at 2:10 and 2:44 p.m. (17)0 15 10 ? * MON-FRI Triggers Monday through Friday at 10:15 a.m. (18)0 15 10 15 * ? Triggers at 10:15 AM on the 15th of each month (19)0 15 10 L * ? Triggers at 10:15 AM on the last day of each month (20)0 15 10 ? * 6L Triggers at 10:15 am on the last Friday of each month (21)0 15 10 ? * 6L 2002-2005 Triggers at 10:15 a.m. on the last Friday of each month from 2002 to 2005 (22)0 15 10 ? * 6#3 Triggers at 10:15 am on the third Friday of each month
test
@Servicepublic class task { //seconds, minutes, days, months, weeks @Scheduled(cron = "0 17 18 * * ? ") public void test(){ System.out.println("This is a timed task, the current time is: "+new Date()); }}
mail task
Spring-boot-starter-mail is required for email sending
org.springframework.boot spring-boot-starter-mail
Define MailProperties content, configured in application.yml.
spring.mail.username=wangmeng@XXX.comspring.mail.password= EXHFOVQXOTZUKWSLspring.mail.host =smtp.163.com#enable encryption verification spring.mail.properties.mail.smtp.ssl.enable=true
Test Mail Send
@Autowired private JavaMailSenderImpl javaMailSender; @Test void contextLoads() { SimpleMailMessage simpleMailMessage=new SimpleMailMessage(); simpleMailMessage.setSubject("This is SpringBoot integrated mail, the first message sent"); simpleMailMessage.setText("Hello, hello, hello, hello! "); simpleMailMessage.setTo("mengwangq@XXXX.com","jhwucc@XXX.com"); simpleMailMessage.setFrom("wangmeng@XXX.com"); javaMailSender.send(simpleMailMessage); } @Test void Test() throws Exception { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helperMessage = new MimeMessageHelper(mimeMessage, true,"utf-8"); helperMessage.setSubject("Today's Work Progress"); helperMessage.setText("Today's work report, please see the attachment for details
"); //Annex helperMessage.addAttachment("Today's Work Progress.docx", new File("D:\\usr\\local\\Work Daily.docx")); helperMessage.setTo("mengwangq@XXXX.com"); helperMessage.setFrom("wangmeng@XXXXX.com"); javaMailSender.send(mimeMessage); } Thank you for reading, the above is the content of "How to integrate scheduled tasks and mail tasks in SpringBoot". After studying this article, I believe everyone has a deeper understanding of how to integrate scheduled tasks and mail tasks in SpringBoot. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.