1、异步任务

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

@EnableAysnc、@Aysnc

/**
* @Author: jiatp
* Description:测试异步任务
*/ @Service
public class AsyncService { //告诉springboot这是一个异步任务
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中.....");
}
}

测试:

@RestController
public class AsyncController {
@Autowired
AsyncService asyncService; @GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}

2、定时任务

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

两个注解:@EnableScheduling、@Scheduled

cron表达式:

字段 允许值 允许的特殊字符
0-59 , - * /
0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 , - * /
星期 0-7或SUN-SAT 0,7是SUN , - * ? / L C #

特殊字符说明:

特殊字符 代表含义
, 枚举
- 区间
* 任意
/ 步长
? 日/星期冲突匹配
L 最后
W 工作日
C 和calendar联系后计算过的值
# 星期,4#2,第2个星期四

service:

@Service
public class ScheduledService { /**cron表达式
* second, minute, hour, day of month, month, and day of week.
* 举例:0 * * * * MON-FRI
* */
//@Scheduled(cron = "* * * * * MON-SAT")//周1-6每一月一天一小时一分钟一份秒运行
// @Scheduled(cron = " * * * * MON-SAT")//区间
//@Scheduled(cron = "0/4 * * * * MON-SAT")
@Scheduled(cron = "0,1,2,3,4,5 * * * * MON-SAT")//枚举
public void hell0(){
System.out.println("hello...定时任务");
}
}

这里只给出例子,其余的自行测试;

3、邮件任务

导入依赖

 <!--邮件任务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

application.properties配置

spring.mail.username=XXXXXXXX@qq.com
spring.mail.password=epkjwhqabfnzbhbb
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

注意这里:

这里要开启smtp服务,点生成授权码,获取连接密码,将密码写在application.properties中

测试

@SpringBootTest
class Springboot04TaskApplicationTests { @Autowired
JavaMailSenderImpl mailSender; //简单邮件发送
@Test
void contextLoads() {
SimpleMailMessage msg = new SimpleMailMessage();
//邮件信息
msg.setSubject("通知!通知!通知!");
msg.setText("我不吃了我不吃了!");
msg.setTo("jatpeo@163.com");
msg.setFrom("393287859@qq.com");
mailSender.send(msg);
System.out.println("发送成功!");
} //复杂的邮件发送
@Test
void test02() throws Exception{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("官宣!");
helper.setText("<h1 style='color:blue'>嘻嘻嘻嘻嘻嘻</h1>",true);//支持html的形式
helper.setTo("xxxxxxx@qq.com");
helper.setFrom("393287859@qq.com"); //发送两个图片
helper.addAttachment("1.jpg",new File("C:\\Users\\Administrator\\Pictures\\002.jpg"));
helper.addAttachment("2.jpg",new File("C:\\Users\\Administrator\\Pictures\\162538-15586863381bcc.jpg")); mailSender.send(mimeMessage);
System.out.println("发送成功!");
}

其余的测试请查看官方文档。

最新文章

  1. ABP源码分析十二:本地化
  2. centos7上consul的集群安装
  3. [原创]cocos2d-x研习录-第二阶 基本概念
  4. JS 在线网站
  5. 【转】Hive导入10G数据的测试
  6. $_SERVER 中重要的元素
  7. Maven那点事儿(Eclipse版)
  8. 2016&quot;百度之星&quot; - 复赛(Astar Round3) 1003 拍照
  9. linux添加ssh用户
  10. mysql优化--博森瑞
  11. as3 打开窗口类
  12. css布局-双飞翼布局
  13. Url校验正则
  14. 我的devops实践经验分享一二
  15. LiteQuery MAX(Integer)、MAX(String) 判断是否返回值
  16. linux init命令
  17. 【转】学习Linux守护进程详细笔记
  18. Ubuntu x86-64汇编(4) 数值操作指令
  19. C/C++基础----函数
  20. Java并发编程,多线程[转]

热门文章

  1. db2,用户名密码不对导致无法连接数据库: Reason: User ID or Password invalid. ERRORCODE=-4214, SQLSTATE=28000
  2. CSS实现背景图片固定
  3. Airbnb React/JSX 编码规范
  4. mysql 记录(record)
  5. Pregel Master
  6. Linux下Qt调用共享库文件.so
  7. 微信公众号开发上传图文素材带有卡片小程序报错:errcode=45166,errmsg = invalid content hint
  8. PHP算法之最长公共前缀
  9. 关于print()里面的sep和end参数的使用
  10. Servlet(Server Applet) 详解