异步任务
简介
异步任务就是,我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务。
但是每次都需要自己手动去编写多线程的实现的话,太麻烦了,我们只需要用一个简单的办法,在我们的方法上加一个简单的注解@Async
即可
实例展示:网页先展示,后台输出延迟三秒钟
- 编写
AsyncService.java
文件
package com.example.task.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
@Async//标注这是一个异步方法
public void hello(){
try{
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("延迟了3秒钟");
}
}
复制代码
- 编写
AsyncController.java
package com.example.task.controller;
import com.example.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();
return "异步任务";
}
}
复制代码
- 在主程序上添加一个注解@EnableAsync ,开启异步注解功能
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync//开启异步注解功能
public class Springboot10TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot10TaskApplication.class, args);
}
}
复制代码
- 测试结果:网页瞬间响应!
定时任务
简介
定时发送消息,完成任务;在自己规定的时间完成
实例展示:每两秒钟,打印一次“hello”
- 编写
ScheduledService.java
package com.example.task.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//秒 分 时 日 月 周几
//0 * * * * 0-7
//注意cron表达式的用法; 0和7代表周天
@Scheduled(cron = "0/2 * * * * ?")
public void hello(){
System.out.println("hello");
}
}
复制代码
- 在主程序上增加
@EnableScheduling
注解,开启定时任务功能
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableAsync//开启异步注解功能
@EnableScheduling//开启基于注解的定时任务
public class Springboot10TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot10TaskApplication.class, args);
}
}
复制代码
cron表达式
- 提供一个转换工具:cron
- 常用的表达式
(1)0/2 * * * * ? 表示每2秒 执行任务
(1)0 0/2 * * * ? 表示每2分钟 执行任务
(1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED 表示每个星期三中午12点
(7)0 0 12 * * ? 每天中午12点触发
(8)0 15 10 ? * * 每天上午10:15触发
(9)0 15 10 * * ? 每天上午10:15触发
(10)0 15 10 * * ? 每天上午10:15触发
(11)0 15 10 * * ? 2005 2005年的每天上午10:15触发
(12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
(18)0 15 10 15 * ? 每月15日上午10:15触发
(19)0 15 10 L * ? 每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
复制代码
邮件任务
简介
就是发送邮件,和qq邮箱的发邮件功能一样
实例展示:给自己发邮件
- 引入依赖
<!-- 发送邮件需要的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
复制代码
- 配置发送邮件的配置
授权码在qq邮箱可以获得
spring.mail.username=1906713811@qq.com
# cjyjjhkbwriaebhb 授权码
spring.mail.password=cjyjjhkbwriaebhb
spring.mail.host=smtp.qq.com
# qq需要配置ssl
spring.mail.properties.mail.smtp.ssl.enable=true
复制代码
- 编写
EmailController.java
来发送邮件
package com.example.task.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RestController
public class EmailController {
@Autowired
JavaMailSenderImpl mailSender;
@RequestMapping("/t1")
//@Scheduled(cron = "0/1 * * * * ?")
public void test(){
//邮件设置1:一个简单的邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("一个简单的邮件");
message.setText("嘻嘻嘻嘻嘻");
message.setTo("1906713811@qq.com");
message.setFrom("1906713811@qq.com");
mailSender.send(message);
System.out.println("success");
}
public void test2() throws MessagingException {
//邮件设置2:一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("一个复杂的邮件");
helper.setText("<b style='color:red'>哈哈哈哈</b>",true);
//发送附件
helper.addAttachment("1.jpg",new File(""));
helper.addAttachment("2.jpg",new File(""));
helper.setTo("1906713811@qq.com");
helper.setFrom("1906713811@qq.com");
mailSender.send(mimeMessage);
}
}
复制代码
4. 结果:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END