异步任务
在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。
两个注解:
@EnableAysnc、@Aysnc
package com.matthew.task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync//开启异步注解功能
@SpringBootApplication
public class Springboot04TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot04TaskApplication.class, args);
}
}
package com.matthew.task.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/19 18:11
* @Version 1.0
*/
@Service
public class AsyncService {
//告诉Spring这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理中。。。");
}
}
package com.matthew.task.controller;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.matthew.task.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/19 18:12
* @Version 1.0
*/
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
如果不写注解那么访问页面会转3秒然后出现下面的页面同时后台输出,但是加上Aysnc后范文页面会直接出现等3秒后后台输出结果。
定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。
两个注解:@EnableScheduling、@Scheduled
cron表达式:
@EnableScheduling//开启基于注解的定时任务
@SpringBootApplication
public class Springboot04TaskApplication {
package com.matthew.task.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
/**
* @Description TODO
* @Author Matthew
* @Date 2019/6/19 18:20
* @Version 1.0
*/
@Service
public class ScheduledService {
/*
* TODO cron:
* second(秒), minute(分), hour(时), day of month(日),month(月), day of week(周几)
* 0 * * * * MON-FRI
*/
//每月的周一到周六的每天每时每分的0秒会执行一次定时任务
// @Scheduled(cron = "0 * * * * MON-SAT")
//每月的周一到周六的每天每时每分的0秒,1秒,2秒,3秒,4秒会执行一次定时任务
// @Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
//每月的周一到周六的每天每时每分的0秒到4秒(包括0和4)会执行一次定时任务
// @Scheduled(cron = "0-4 * * * * MON-SAT")
//每月的周一到周六的每天每时每分的从0秒开始每4秒会执行一次定时任务
//?用来解决日期和周几的冲突问题,如果发生冲突就不会执行,如果都包含就执行
//假设今天是19号,那么下面就会执行,但是再下就不会执行
// @Scheduled(cron = "0/4 * * 19 * *")
// @Scheduled(cron = "0/4 * * 20 * *")
public void hello(){
System.out.println("hello ....");
}
}
邮件任务
- 邮件发送需要引入spring-boot-starter-mail
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- Spring Boot 自动配置MailSenderAutoConfiguration
- 定义MailProperties内容,配置在application.yml中
spring.mail.username=941076796@qq.com
spring.mail.password=nkyiyefskcgabbdb
spring.mail.host=smtp.qq.com
# 安全连接,不写也可以,如果报错就写
spring.mail.properties.mail.smtp.ssl.enable=true
点击生成授权码,填入密码
- 自动装配JavaMailSender
- 测试邮件发送
package com.matthew.task;
import org.junit.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.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.annotation.Async;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot04TaskApplicationTests {
@Autowired
JavaMailSender javaMailSender;
@Test
public void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//邮件设置
simpleMailMessage.setSubject("借你的号测试一下");
simpleMailMessage.setText("我用Java程序写的邮件,好好学以后你也可以装逼");
simpleMailMessage.setTo("12964720@qq.com");
simpleMailMessage.setFrom("941076795@qq.com");
javaMailSender.send(simpleMailMessage);
}
@Test
public void test02() throws Exception{
//1. 创建一个复杂的消息邮件
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
//邮件设置
mimeMessageHelper.setSubject("借你的号测试一下");
mimeMessageHelper.setText("我用Java程序写的邮件,好好学以后你也可以装逼");
mimeMessageHelper.setText("<b style='color:red'> 复杂邮件可以设置字体样式 </b>",true);
mimeMessageHelper.setTo("1296472300@qq.com");
mimeMessageHelper.setFrom("941076795@qq.com");
//上传附件
mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\Pictures\\Camera Roll\\1.jpg"));
mimeMessageHelper.addAttachment("2.jpg",new File("C:\\User\\Pictures\\Camera Roll\\2.jpg"));
javaMailSender.send(mimeMessage);
}
}