Introduction to the

  • Synchronization: Tasks are executed in sequential steps, usually from top to bottom in code, and subsequent tasks are not executed until the previous task is completed
  • Asynchronous: The system does not execute the data step by step. If the data is fast, the system does not wait for the data to be executed slowly

The sample

General steps

  1. On the main method@EnableAsync, enable asynchrony
  2. On methods that require asynchrony,@AsyncTell Spring that this method is asynchronous
  3. The other way, it doesn’t matter@AsyncWhether the marking method is complete,

code

  • service
@Service
public class HelloAsyn {
    @Async// Tell Spring that this method is asynchronous
    public void asyn(a){
        try {
            Thread.sleep(4000);
        } catch(InterruptedException e) { e.printStackTrace(); }}}Copy the code
  • controller
@Controller
public class MyController {
    @Autowired
    HelloAsyn helloAsyn;
    @GetMapping("/h")
    @ResponseBody
    public String a(a){
        helloAsyn.asyn();
        return "asynchronous"; }}Copy the code
  • The main method
@EnableAsync// Enable asynchrony
@SpringBootApplication
public class SpringBootAsynchronousApplication {
    public static void main(String[] args) { SpringApplication.run(SpringBootAsynchronousApplication.class, args); }}Copy the code

The simple mail

  • Import dependence
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.4.4</version>
</dependency>
Copy the code
  • application.properties
spring.mail.username=[email protected]
# My QQ account
spring.mail.password=ffjkxcqamcriebie
# Password from qq mailbox, set -> account inside the SMTP service authorization code
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
Copy the code
  • Java code
@SpringBootTest
class SpringBootMailApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads(a) {
        // Simple mail
        SimpleMailMessage mailMessage = new SimpleMailMessage();// Simple mail
        mailMessage.setTo("[email protected]");// Where to send
        mailMessage.setFrom("[email protected]");// By whom
        mailMessage.setSubject("Email from Spring Boot.");// The subject of the email
        mailMessage.setText("Spring Boot is great. Learn it.");// The contents of the email
        mailSender.send(mailMessage);// Send the email to}}Copy the code

Timing task

Steps:

  1. In the program entry, the main method annotation@EnableScheduling
  2. in@Scheduled(cron ="" )
  • B: Corn expressions

code

  • main
@EnableScheduling// Enable a scheduled task
@SpringBootApplication
public class SpringBootMailApplication {

    public static void main(String[] args) { SpringApplication.run(SpringBootMailApplication.class, args); }}Copy the code
  • Timing task
@Service
public class ScheduledService {
    / / cron expression
    // second minute day month week
    @Scheduled(cron ="0/2 * * * * ?" )// Execute every two seconds
    public void scheduled(a){
        System.out.println("Scheduled task, do not reply."); }}Copy the code