Spring Boot与任务

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

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

两个注解:
@EnableAysnc、@Aysnc

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o1BVt9OC-1581771042721)(http://39.102.36.205:8090/upload/2020/2/image-22ee08be534c43beb4d4ec6e8e2f5b78.png)]

@Service
public class ScheduledService {

    /**
     * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
     * 0 * * * * MON-FRI
     *  【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
     *  【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
     *  【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
     *  【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
     *  【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
     */
   // @Scheduled(cron = "0 * * * * MON-SAT")
    //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
   // @Scheduled(cron = "0-4 * * * * MON-SAT")
    @Scheduled(cron = "0/4 * * * * MON-SAT")  //每4秒执行一次
    public void hello(){
        System.out.println("hello ... ");
    }
}

三、邮件任务

  1. 邮件发送需要引入spring-boot-starter-mail
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
  1. Spring Boot 自动配置MailSenderAutoConfiguration
@EnableAsync  //开启异步注解功能
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class Springboot04TaskApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot04TaskApplication.class, args);
	}
}
  1. 定义MailProperties内容,配置在application.yml中
spring.mail.username=534096094@qq.com
spring.mail.password=gtstkoszjelabijb
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
  1. 自动装配JavaMailSender
  2. 测试邮件发送
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7li3gor7-1581771042722)(http://39.102.36.205:8090/upload/2020/2/image-d0b610e393094584bc5f8ef4241f8a40.png)]
@Autowired
	JavaMailSenderImpl mailSender;

	@Test
	public void contextLoads() {
		SimpleMailMessage message = new SimpleMailMessage();
		//邮件设置
		message.setSubject("通知-今晚开会");
		message.setText("今晚7:30开会");

		message.setTo("[email protected]");
		message.setFrom("[email protected]");

		mailSender.send(message);
	}

	@Test
	public void test02() throws  Exception{
		//1、创建一个复杂的消息邮件
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

		//邮件设置
		helper.setSubject("通知-今晚开会");
		helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);

		helper.setTo("[email protected]");
		helper.setFrom("[email protected]");

		//上传文件
		helper.addAttachment("1.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\1.jpg"));
		helper.addAttachment("2.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\2.jpg"));

		mailSender.send(mimeMessage);

	}

注意:
在进行测试的时候,需要获取其验证
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-btJn9Mnl-1581771042723)(http://39.102.36.205:8090/upload/2020/2/image-c7c590fdc2f948b6b543ecdddcce4bb4.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T8rtiy8t-1581771042724)(http://39.102.36.205:8090/upload/2020/2/image-4ebfe3d3d4834270844047ffe81242ca.png)]
谢谢!!!
顺便说一下我的个人博客
天涯博客

发布了36 篇原创文章 · 获赞 26 · 访问量 7566

猜你喜欢

转载自blog.csdn.net/weixin_43566977/article/details/104334338
今日推荐