4.1_springboot2.2任务之异步、定时、邮件任务

1、异步任务

​ 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。

@EnableAysnc、@Aysnc

/**
 * @Author: jiatp
 * Description:测试异步任务
 */

@Service
public class AsyncService {

    //告诉springboot这是一个异步任务
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("处理数据中.....");
    }
}

测试:

@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;

    @GetMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "success";
    }
}

2、定时任务

​ 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口

两个注解:@EnableScheduling、@Scheduled

cron表达式:

字段 允许值 允许的特殊字符
0-59 , - * /
0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 , - * /
星期 0-7或SUN-SAT 0,7是SUN , - * ? / L C #

特殊字符说明:

特殊字符 代表含义
, 枚举
- 区间
* 任意
/ 步长
? 日/星期冲突匹配
L 最后
W 工作日
C 和calendar联系后计算过的值
# 星期,4#2,第2个星期四

service:

@Service
public class ScheduledService {


    /**cron表达式
     * second, minute, hour, day of month, month, and day of week.
     * 举例:0 * * * * MON-FRI
     * */
    //@Scheduled(cron = "* * * * * MON-SAT")//周1-6每一月一天一小时一分钟一份秒运行
    // @Scheduled(cron = " * * * * MON-SAT")//区间
    //@Scheduled(cron = "0/4 * * * * MON-SAT")
    @Scheduled(cron = "0,1,2,3,4,5 * * * * MON-SAT")//枚举
    public void hell0(){
        System.out.println("hello...定时任务");
    }
}

这里只给出例子,其余的自行测试;

3、邮件任务

导入依赖

 <!--邮件任务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

application.properties配置

spring.mail.username=XXXXXXXX@qq.com
spring.mail.password=epkjwhqabfnzbhbb
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.auth=true 
spring.mail.properties.mail.smtp.starttls.enable=true  
spring.mail.properties.mail.smtp.starttls.required=true  


注意这里:

在这里插入图片描述

这里要开启smtp服务,点生成授权码,获取连接密码,将密码写在application.properties中

在这里插入图片描述

测试

@SpringBootTest
class Springboot04TaskApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;
    
    //简单邮件发送
    @Test
    void contextLoads() {
        SimpleMailMessage msg = new SimpleMailMessage();
        //邮件信息
        msg.setSubject("通知!通知!通知!");
        msg.setText("我不吃了我不吃了!");
        msg.setTo("jatpeo@163.com");
        msg.setFrom("393287859@qq.com");
        mailSender.send(msg);
        System.out.println("发送成功!");
    }

    //复杂的邮件发送
    @Test
    void test02() throws Exception{
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("官宣!");
        helper.setText("<h1 style='color:blue'>嘻嘻嘻嘻嘻嘻</h1>",true);//支持html的形式
        helper.setTo("xxxxxxx@qq.com");
        helper.setFrom("393287859@qq.com");

        //发送两个图片
        helper.addAttachment("1.jpg",new File("C:\Users\Administrator\Pictures\002.jpg"));
        helper.addAttachment("2.jpg",new File("C:\Users\Administrator\Pictures\162538-15586863381bcc.jpg"));

        mailSender.send(mimeMessage);
        System.out.println("发送成功!");
    }

其余的测试请查看官方文档。

原文地址:https://www.cnblogs.com/jatpeo/p/11767469.html