- Asynchronous task
- Timed task
- Mail sending (commonly used, springboot official)
1, Asynchronous task
1. Create a springboot project and check the web launcher dependency
2. Create a new business
However, the data processing thread will sleep for 3 seconds, and the page will not be displayed during sleep
@Service//Business hosted by spring public class AsyncService { public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Data processing..."); } }
3. Write the controller to call this business
@RestController public class AsyncController { @Autowired AsyncService asyncService; @RequestMapping("/hello") public String hello(){ asyncService.hello();//Stop for three seconds and turn return "ok"; } }
4. Convert to asynchronous task
Add @ Async asynchronous task annotation to the business code block
Then open the asynchronous function annotation in the main program
Then you can execute the return result ok first, and the middle thread sleeps (that is, the data processing runs in the background for three seconds, and then displays it)
2, Mail task
1. Add mail springboot launcher
<!--Start mail function--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2. Send a simple email and test the effect
3. Send complex emails and their test results
The following code is placed in the controller layer in the business
4. Encapsulate mail sending into a tool class
It also comes with comments
/** * * @param html * @param subject * @param text * @throws MessagingException * @Autor Clear sky */ //Send mail tool class public void sendMail(Boolean html,String subject,String text) throws MessagingException { //A complex email MimeMessage mimeMessage = mailSender.createMimeMessage(); //assemble MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //text helper.setSubject(subject);//theme helper.setText(text,html);//The second parameter enables html parsing //enclosure helper.addAttachment("1.jpg",new File("D:\\desktop\\1.jpg")); helper.addAttachment("2.jpg",new File("D:\\desktop\\1.jpg")); helper.setTo("869729243@qq.com");//addressee helper.setFrom("869729243@qq.com");//Sender mailSender.send(mimeMessage); }
3, Timed task
Two core interfaces
TaskExecutor - task executor
Task Scheduler - task scheduler with timing function
@Enableshcheduling annotation for enabling the timing function
@Scheduled} when to execute
Cron expression
1. Write business code that needs to be executed regularly
Remember that the @ Service annotation is required for this task to be executed by the springboot main program
@Scheduled(cron = "0 32 15 * * ?") Timed execution time annotation
(cron expression interpretation: execute this task at 15:32:0 on the day of the week every month)
be careful:
- cron expression? Difference between placeholder and * placeholder:? It means that the exact time is not specified, * means that all times are executed, and the days of week and month are easy to conflict, so use? Station position
- Parameter format cron = "second minute hour day month week"
@Service public class ScheduleService { //Execute this method at a specific time //cron expressions // Second minute hour day month week @Scheduled(cron = "0 32 15 * * ?") public void hello(){ System.out.println("hello Executed"); } }
2. Start scheduled task execution in the main program class
@Enableshcheduling / / annotation for enabling the timing function