Springboot asynchronous, mail, and timed tasks.

Original
join here set stop 3s

package com.jj.demo.service;

import org.springframework.stereotype.Service;

@Service
public class Asnycservice {
    
    
    public void hello() throws InterruptedException {
    
    
        Thread.sleep(3000);
        System.out.println("数据在处理!!");
    }
}


Console code

package com.jj.demo.controller;

import com.jj.demo.service.Asnycservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class Asnycontrller {
    
    
//    注入service
    @Autowired
    Asnycservice asnycservice;
      @RequestMapping("/hello")
    @ResponseBody
    public String demo1() throws Exception {
    
    
        asnycservice.hello();
        return "ok!";
    }
}

Effect, the user will wait on the page
Insert picture description here

At this time, we can use Springboot's asynchronous task, only need to open two annotations, purpose, if we are sending emails, we must first tell the user that it is sending, the background can process data.
Insert picture description here
Insert picture description here
That's it,
effect
Insert picture description here
mail,
simple text mail,
import dependency

<!--        邮箱的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

L yml

#邮箱的配置
spring:
  mail:
    host: smtp.163.com
    username: ************@163.com
    password: ****************
    protocol: smtp
    default-encoding: UTF-8

Write in the test class. The second
Insert picture description here
effect is
Insert picture description here
to send unusual emails!
Insert picture description here
Effects,
Insert picture description here
colors and pictures are here!
Insert picture description here
Timed tasks,
critical interfaces

TaskScheduler task scheduler
TackExecutor task executor
, add comments to the main program
Insert picture description here

Write in the business layer

package com.jj.demo.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class Schedulingservice {
    
    
    //cron 表达式
    //秒 分 时 日 月  周几
    @Scheduled(cron = "0 38 16 * * ?")
public  void helo(){
    
    
        System.out.println("哈哈哈哈哈");
    }
}

The effect of
Insert picture description here
Baidu's a cron expression

Guess you like

Origin blog.csdn.net/m0_46937429/article/details/111769554