Asynchronous tasks:
@EnableAysnc、@Aysnc
Regular tasks:
@EnableScheduling、@Scheduled
Mail tasks:
pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.study.springboot-job</groupId> <artifactId>study-job</artifactId> <version>0.0.1-SNAPSHOT</version> <name>study-job</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
YML
Server: Port: 9999 the Spring: mail: username: XXX @ # qq.com own mailbox password: aaaedijj # set up their own content in open smtp to send a short message obtained in the qq mailbox Host: smtp.qq.com properties.mail. smtp.ssl.enable: to true # the configuration is required to send attachments
Master Boot categories:
package com.study.springboot.job; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @EnableAsync //开启异步任务 @EnableScheduling//开启定时任务 @SpringBootApplication public class StudyJobApplication { public static void main(String[] args) { SpringApplication.run(StudyJobApplication.class, args); } }
Core code:
controller
package com.study.springboot.job.controller; import com.study.springboot.job.service.SyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class AsyncController { @Autowired private SyncService syncService; @GetMapping("/async/test") @ResponseBody public String getMsg(){ syncService.syncJob(); return "async test ..."; } }
service
package com.study.springboot.job.service; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.Date; @Service public class SyncService { //异步任务 @Async public void syncJob(){ System.out.println("异步任务开始执行。"); try { Thread.sleep(5000); } catch(InterruptedException E) { e.printStackTrace (); } System.out.println ( . "Asynchronous task is finished" ); } / * field allows the special character values allowed sec 0-59, - * / min 0-59, - * / h 0-23, - * / date 1-31, - * / LWC? January 1-12 - * / week 0-7 or SUN-SAT 0,7 is SUN, -? * / LC # special character represents meaning , enumeration - interval * any / step ? day / week conflict matching L last W days After the calculated value C and calendar Information Week # 4 # 2, the second Thursday * / // timer task @Scheduled (the cron = "* * * 0 * 0-7" ) public void schedulJob () { System.out.println ( "task starts execution timing of . "+ new new the SimpleDateFormat. (" the mM-dd-YYYY HH: mm: SS ") the format ( new new a Date ())); } }
Send a test message:
package com.study.springboot.job; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class StudyJobApplicationTests { @Autowired private JavaMailSenderImpl javaMailSender; privateFromMailAddress = String "[email protected]" ; // own mailbox qq Private String toMailAddress = "[email protected]" ; 163 // own mailbox @Test void contextLoads () { } @Test public void Testmail () { SimpleMailMessage SimpleMailMessage = new new SimpleMailMessage (); simpleMailMessage.setSubject ( "test message service" ); simpleMailMessage.setText ( "test messages sent to the mailbox 163" ); simpleMailMessage.setFrom (fromMailAddress); simpleMailMessage.setTo (toMailAddress); javaMailSender. send (simpleMailMessage); } @Test public void testFuZaMail () throws Exception { the MimeMessage mimeMailMessage = javaMailSender.createMimeMessage (); MimeMessageHelper MimeMessageHelper = new new MimeMessageHelper (mimeMailMessage, to true ); mimeMessageHelper.setSubject ( "Test message with attachments" ); mimeMessageHelper.setText ( "<B style =: the body of 'Color Green'> </ B> ", to true ); mimeMessageHelper.addAttachment ( " 1.jpg ", new new File (" C: \\ \\ 25017 Desktop the Users \\ \\ \\ art springcloud introduction \\ hot deployment .png " )); // send the file to a local address MimeMessageHelper.addAttachment ("2.jpg",new File("C:\\Users\\25017\\Desktop\\springcloud\\技术介绍\\热部署2.png")); mimeMessageHelper.setFrom(fromMailAddress); mimeMessageHelper.setTo(toMailAddress); javaMailSender.send(mimeMailMessage); } }