SpringBoot高级整合系列 任务(异步,定时,邮件 任务)
文章目录
新建SpringBoot项目
异步任务
新建service
通过线程让他睡3秒钟
package com.luyi.service;
import org.springframework.stereotype.Service;
/**
* 异步任务
* @author 卢意
* @create 2020-11-03 16:39
*/
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中");
}
}
新建controller类
package com.luyi.controller;
import com.luyi.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 卢意
* @create 2020-11-03 16:41
*/
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
启动测发现会等待三秒钟
加入@Async注解
package com.luyi.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* 异步任务
* @author 卢意
* @create 2020-11-03 16:39
*/
@Service
public class AsyncService {
//告诉spring这是一个异步的方法 spring就会开一个线程池对她进行调用
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中");
}
}
主启动加入注解
package com.luyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync //开启异步注释功能
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
启动发现不用等三秒了
定时任务
cron参数
demo1
新建service类
package com.luyi.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author 卢意
* @create 2020-11-03 16:52
*/
@Service
public class ScheduleService {
@Scheduled(cron = "0 * * * * *")
public void hello(){
System.out.println("hello----");
}
}
主启动类加入注解
package com.luyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync //开启异步注释功能
@EnableScheduling// 开启基于注解的定时任务
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
每分钟的整点会执行一下该方法 如16:00
demo2
在每分钟的0,1 ,2 ,3 ,4 五个时刻执行方法
package com.luyi.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author 卢意
* @create 2020-11-03 16:52
*/
@Service
public class ScheduleService {
//@Scheduled(cron = "0 * * * * *")
@Scheduled(cron = "0,1,2,3,4 * * * * *")
public void hello(){
System.out.println("hello----");
}
}
@Scheduled(cron = "0-4 * * * * *") 区间的方式 也可以实现
demo3
每4秒执行一次
package com.luyi.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @author 卢意
* @create 2020-11-03 16:52
*/
@Service
public class ScheduleService {
//@Scheduled(cron = "0 * * * * *")
//@Scheduled(cron = "0,1,2,3,4 * * * * *")
//@Scheduled(cron = "0-4 * * * * *")
@Scheduled(cron = "0/4 * * * * *")
public void hello(){
System.out.println("hello----");
}
}
?冲突匹配
如果选择了每个月1号执行
在周的选项只能使用?
反之也一样 如果打算 每个星期1执行方法
在日期参数也要写?
星期参数
可以用英文 也可以用数字
注意 0-7 0和7都是表示周日
例子
邮件任务
流程
简单邮件
加入依赖
配置application.properties
在添加 一个配置 spring.mail.properties.mail.stmp.ssl.enable=true
测试类
package com.luyi;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
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.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootTaskApplicationTests {
@Autowired
JavaMailSenderImpl JavaMailSender;
@Test
void contextLoads() {
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
//邮件设置
simpleMailMessage.setSubject("哈哈哈");
simpleMailMessage.setText("我是卢意");
simpleMailMessage.setTo("xxx@qq.com");
simpleMailMessage.setFrom("xxx@qq.com");
JavaMailSender.send(simpleMailMessage);
}
}
不懂为什么显示JavaMailSenderImpl 没有注入Bean
参考网上方法 自己写一个配置类 再注入bean
package com.luyi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* @author 卢意
* @create 2020-11-03 17:30
*/
@Configuration
public class MailConfig {
@Bean
public JavaMailSenderImpl JavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.qq.com");
mailSender.setUsername("****@qq.com");
mailSender.setPassword("xmmwrfoepzc");
return mailSender;
}
}
复杂邮件邮件
@Test
void contextLoads2() throws MessagingException {
//复杂邮件
//创建一个复杂的邮件
MimeMessage mimeMessage=JavaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//邮件设置
helper.setSubject("哈哈哈");
helper.setText("<b style='color:red'>我是卢意</b>",true);// 开启html
helper.setTo("XXXX@qq.com");
helper.setFrom("XXXX@qq.com");
//helper 上传文件
helper.addAttachment("luyi.jpg", new File("C:\\Users\\71871\\Desktop\\luyi.jpg"));
JavaMailSender.send(mimeMessage);
}