目录
1. 异步任务
package com.zx.task.service;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中...");
}
}
package com.zx.task.controller;
import com.zx.task.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();
//3秒以后才会响应页面
return "success";
}
}
同步的效果,访问http://localhost:8080/hello,运行三秒以后才会响应页面
在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。
两个注解:
@EnableAysnc(开启异步注解功能)、@Aysnc(告诉spring,这是一个异步方法,spring会自己开启一个线程池进行调用)
package com.zx.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.zx.task.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
//告诉spring,这是一个异步方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中...");
}
}
异步的效果,访问http://localhost:8080/hello,会立即响应
2. 定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor、TaskScheduler接口。
两个注解:@EnableScheduling、@Scheduled
package com.zx.task.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class ScheduledService {
/**
* For example, {@code "0 * * * * MON-FRI"} means once per minute on weekdays
* second, minute, hour, day of month, month, and day of week.
* 【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") //周一到周六 每分钟的0秒运行一次
//@Scheduled(cron = "0,10,20,30,40,50 * * * * MON-SAT")
//@Scheduled(cron = "0-4 * * * * MON-SAT")
@Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次
public void hello(){
System.out.println("hello...");
System.out.println(new Date());
}
}
3. 邮件任务
1.邮件发送需要引入spring-boot-starter-mail
2.Spring Boot 自动配置MailSenderAutoConfiguration
3.定义MailProperties内容,配置在application.properties中
spring.mail.username=140686106@qq.com
spring.mail.password=wenkmjaeviyniggj
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
4.自动装配JavaMailSender 测试邮件发送
package com.zx.task;
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.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class Springboot04TaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
//邮件设置
simpleMailMessage.setSubject("通知");
simpleMailMessage.setText("2020.06.05");
simpleMailMessage.setTo("1863579329@163.com");
simpleMailMessage.setFrom("140686106@qq.com");
mailSender.send(simpleMailMessage);
}
//带附件的邮件发送
@Test
void test02() throws MessagingException {
//1.创建一个复杂的消息邮件
MimeMessage mimeMessage=mailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
//邮件设置
helper.setSubject("通知");
helper.setText("<b style='color:red'>2020.06.05 09:57</b>",true);
helper.setTo("1863579329@163.com");
helper.setFrom("140686106@qq.com");
//上传文件
helper.addAttachment("a.jpg",new File("D:\\Users\\zx\\Pictures\\Camera Roll\\a.jpg"));
mailSender.send(mimeMessage);
}
}