springboot的异步任务、定时任务、邮件任务

一、异步任务

在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。
在需要进行异步处理的方法上加@Async注解,并且在控制器上加@EnableAsync来开启异步注解功能即可
例如:

/** AsynService.java服务类 */
package com.example.renwu.service;

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

@Service
public class AsynService {
    @Async
    public void hello(){
        try{
            Thread.sleep(3000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("处理数据中");
    }
}
/** AsyncController.java控制器类 */
package com.example.renwu.controller;

import com.example.renwu.service.AsynService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableAsync//开启异步注解功能
@RestController
public class AsyncController {

    @Autowired
    AsynService asynService;

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

二、定时任务

项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。
在实际使用的时候只需要两个注解即可
在定时任务上加@Scheduled注解,并且在控制器上加@EnableScheduling来开启定时任务功能即可。
而在@Scheduled注解上可以使用cron参数来进行设置定时任务执行的时间:
在这里插入图片描述在这里插入图片描述
例如:

/** ScheduledService.java  */
package com.example.renwu.service;

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

@Service
public class ScheduledService {
    //second(秒), minute(分), hour(时), day of month(日), month(月), and day of week(周几)
    //表达式: * * * * * *
    //@Scheduled(cron = "0 * * * * MON-FRI")//每周一到周五每分钟都执行
    @Scheduled(cron = "0,1,2,3,4 0/4 * * * MON-FRI")//每周一到周五每4分钟的0,1,2,3,4秒执行
    public void hello(){
        System.out.println("hello");
    }
}
/** AsyncController.java控制器类 */
package com.example.renwu.controller;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RestController;
@EnableScheduling//开启基于注解的定时任务
@RestController
public class AsyncController {
}

三、邮件任务

  • 邮件发送需要引入spring-boot-starter-mail
  • Spring Boot 自动配置MailSenderAutoConfiguration
  • 定义MailProperties内容,配置在application.yml中
  • 自动装配JavaMailSender
  • 测试邮件发送
    例:
#application.properties中配置
#输入账号
spring.mail.username=********@qq.com
#输入密码,或者授权码,qq邮箱可以在设置中获得
spring.mail.password=*******
#服务器地址
spring.mail.host=smtp.qq.com
#设置安全连接(2.0之后好像不用了)
spring.mail.properties.smtp.ssl.enable=true
//测试类进行发送
package com.example.renwu;

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 RenwuApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    public void test01() {
        //创建一个简单邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("设置标题");
        message.setText("设置内容");
        //设置收件人地址
        message.setTo("*******@qq.com");
        //设置发件人
        message.setFrom("******@163.com");
        mailSender.send(message);
    }
    @Test
    public void test02() throws Exception{
        //创建一个复杂邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //第二个参数是是否有附件
        MimeMessageHelper helper= new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("设置标题");
        //第二个参数表示是html
        helper.setText("<h1 style='color:blue'>设置内容</h1>",true);
        //设置收件人地址
        helper.setTo("*********@qq.com");
        //设置发件人
        helper.setFrom("********@qq.com");
        //上传文件
        helper.addAttachment("文件名",new File("这里填写地址"));
		//发送邮件
        mailSender.send(mimeMessage);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅梦曾倾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值