SpringBoot 1x 系列之(十一)Spring Boot与任务

Spring Boot与任务

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

1. 异步任务

1.1 应用场景

执行一些操作(如:邮件任务等)不想阻塞当前线程的情况下,可以通过多线程的方式进行异步处理。

1.2 快速使用

主配置类

//开启@Async异步注解功能
@EnableAsync
@EnableRabbit
@EnableCaching
@MapperScan("com.atguigu.springboot.mapper")
@SpringBootApplication
public class SpringBootCacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootCacheApplication.class, args);
    }
}

Service中的测试方法

@Service
public class DepartmentService {

  	//告诉Spring这是一个异步方法
    @Async
    public void helloWorld(){

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行中。。。。。。。。。");
    }
}

Controller中的调用方法

@RestController
public class DepartmentController {

    @GetMapping("/test")
    public String testAsync(){
        departmentService.helloWorld();
        return "success";
    }
}

2. 定时任务

2.1 快速使用

主配置类

//开启@Scheduled定时任务注解功能
@EnableScheduling
@EnableAsync
@EnableRabbit
@EnableCaching
@MapperScan("com.atguigu.springboot.mapper")
@SpringBootApplication
public class SpringBootCacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootCacheApplication.class, args);
    }
}

Service中的测试方法

@Service
public class DepartmentService {

  	//指定cron表达式,每个月的周一到周六的每一天的每时每分的1-4秒每一秒都执行一次
    @Scheduled(cron = "1-4 * * * * MON-SAT")
    public void testSchedule(){
        System.out.println("定时任务执行了");
    }
}

2.2 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个星期四

3. 邮件任务

邮件发送的流程:

​ 1)现在lisi要给zhangsan发送邮件,首先lisi要用自己的账号密码登录邮箱服务器;

​ 2)163邮箱服务器把lisi写的邮件发给QQ邮箱服务器;

​ 3)lisi上线后,再从QQ邮箱服务器中获取自己的邮件。

3.1 引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

3.2 自动配置

自动配置类:MailSenderAutoConfiguration

向容器中放了JavaMailSenderImpl,用于发送邮件

3.3 全局配置

以163邮箱向QQ邮箱发邮件为例,需要配置163邮箱信息

spring:
  mail:
    host: smtp.163.com
    username: yxt_20011030@163.com
    # 授权码:用于第三方登录,防止真正的密码泄露
    password: XXXXXXXXXXXXX
    # 如果是QQ邮箱向其他邮箱发邮件,需要增加如下配置
    properties:
      mail:
        smtp:
          ssl:
            enable: true

网易163邮箱SMTP服务器获取:

网易163邮箱授权码获取:

通过SpringBoot发邮件是需要开启服务的,同时开启服务时会拿到授权码

3.4 快速使用

3.4.1 简单邮件

163邮箱向QQ邮箱发送一封简单邮件

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    public void testSimpleMail(){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

//        设置标题
        simpleMailMessage.setSubject("Hello World!");
//        设置内容
        simpleMailMessage.setText("Spring Boot Mail");
//        设置收件人
        simpleMailMessage.setTo("1021377096@qq.com");
//        设置发件人
        simpleMailMessage.setFrom("yxt_20011030@163.com");
//        调用JavaMailSenderImpl的API发送邮件
        mailSender.send(simpleMailMessage);
    }

QQ邮箱收到的信息如下:

3.4.2 复杂邮件

复杂邮件:带附件或邮件内容是Html

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    public void testComplateMail() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
//        MimeMessageHelper:复杂的消息邮件,第二个参数的含义是是否要上传文件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

//        设置标题
        helper.setSubject("Hello World!");
//        设置内容为Html,当内容为Html时,需要传第二个参数为true,默认是false,false不会解析html
        helper.setText("<b style='color:red'>Spring Boot Mail</b>", true);
//        设置收件人
        helper.setTo("1021377096@qq.com");
//        设置发件人
        helper.setFrom("yxt_20011030@163.com");
//        设置要携带的附件,如果有多个,设置多个就可以
        helper.addAttachment("1.txt", new File("C:\\Users\\商务小本本\\Desktop\\新建文本文档.txt"));
//        最终发送的邮件是MimeMessage而不是MimeMessageHelper
        mailSender.send(mimeMessage);
    }

posted @   刘二水  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示