这篇文章主要介绍Springboot中异步、定时、邮件任务的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
异步处理还是非常常用的,比如我们在网站上发送邮件,后台会去发送邮件,此时前台会造成响应不动,直到邮件发送完毕,响应才会成功,所以我们一般会采用多线程的方式去处理这些任务。
package com.rk.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
public void hello(){
try {
System.out.println("数据处理中~");
Thread.sleep(3000);//停止三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.rk.controller;
import com.rk.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
现在启动项目进行测试,三秒后才会出现success,现在还不是异步
@Async//告诉spring这是一个异步方法
public void hello(){
try {
System.out.println("数据处理中~");
Thread.sleep(3000);//停止三秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@EnableAsync//开启异步注解功能
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
#用户名
spring.mail.username=1624603357@qq.com
#密码
spring.mail.password=yblyxhvmnsurbbci
#发送邮件服务器
spring.mail.host=smtp.qq.com
#开启加密验证 ssl
spring.mail.properties.mail.smtp.ssl.enable=true
简单邮件
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("你好,rk");//邮件标题
mailMessage.setText("测试邮件");//邮件内柔
mailMessage.setTo("r1624603357@126.com");//收件人邮箱
mailMessage.setFrom("1624603357@qq.com");//发件人邮箱
mailSender.send(mailMessage);
}
复杂邮件
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//正文
helper.setSubject("你好,rk");
helper.setText("<h2 style='color:red'>测试邮件</h2>",true);
//附件
helper.addAttachment("1.png",new File("D:\\QLDownloadGame\\2\\1.png"));
helper.addAttachment("rk.docx",new File("E:\\桌面\\rk.docx"));
// 发/收件人
helper.setTo("r1624603357@126.com");
helper.setFrom("1624603357@qq.com");
//发送
mailSender.send(mimeMessage);
}
@Service
public class ScheduledService {
//秒 分 时 日 月 周几
//0 * * * * MON-FRI
//注意cron表达式的用法; 每天20:28 0秒执行该方法
@Scheduled(cron = "0 28 20 * * 0-7")
public void hello(){
System.out.println("现在是20:28");
System.out.println("hello.....");
}
}
项目启动后每天20:28:00执行hello方法
@EnableAsync//开启异步注解功能
@EnableScheduling//开启定时功能注解
@SpringBootApplication
public class Springboot09TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot09TestApplication.class, args);
}
}
cron表达式练习
/*
【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点期间,每个整点都执行一次
*/
以上是“Springboot中异步、定时、邮件任务的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。