8、Spring Boot任务
1.异步任务
在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。
主要使用两个注解完成,@EnableAysnc、@Aysnc
(1).Springboot04TaskApplication.java
|
@EnableAsync //开启异步注解功能 @SpringBootApplication public class Springboot04TaskApplication { public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); } } |
(2).AsyncController.java
|
package com.hosystem.task.controller; import com.hosystem.task.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"; } } |
(3).AsyncService.java
|
package com.hosystem.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("处理数据...."); } } |

2.定时任务
开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。
使用到的注解有两个,@EnableScheduling、@Scheduled
Cron表达式:

(1).ScheduledService.java
|
package com.hosystem.task.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledService { /** * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几). * 0 * * * * MON-FRI * [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") //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") // @Scheduled(cron = "0-4 * * * * MON-SAT") @Scheduled(cron = "0/4 * * * * 0-7") //每4秒执行一次 public void hello() { System.out.println("hello ..."); } } |
(2).Springboot04TaskApplication.java
|
package com.hosystem.task; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @EnableScheduling //开启基于注解定时任务 @SpringBootApplication public class Springboot04TaskApplication { public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); } } |
3.邮件任务

(1).导入pom.xml
|
<!--mail--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> |
(2).开启POP3/SMTP
(3).开启IMAP/SMTP
(4).生成授权码
(5).applicaiton.properties
|
server.port=80 #配置邮箱信息 spring.mail.username=username@qq.com #授权码 spring.mail.password=password #smtp地址 spring.mail.host=smtp.qq.com spring.mail.protocol=smtp spring.mail.properties.mail.smtp.ssl.enable=true |
(6).Springboot04TaskApplicationTests.java
SendMailTest测试邮箱发送,一定要在mail中开启SMTP
|
package com.hosystem.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.MimeMessageHelper; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class Springboot04TaskApplicationTests { @Autowired JavaMailSenderImpl mailSender; @Test public void contextLoads() { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //邮件设置 simpleMailMessage.setSubject("通知-----"); simpleMailMessage.setText("学习java"); //setTo:目的邮件地址 setFrom:发送邮件地址 simpleMailMessage.setTo("username@163.com"); simpleMailMessage.setFrom("username@qq.com"); mailSender.send(simpleMailMessage); } @Test public void test01() throws Exception{ //1.创建一个复杂的消息邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); //邮件设置 helper.setSubject("通知-----"); /** * public void setText(String text) throws MessagingException {this.setText(text, false); } */ helper.setText("<b style='color:red'>学习java</b>",true); //setTo:目的邮件地址 setFrom:发送邮件地址 helper.setTo("username@163.com"); helper.setFrom("username@qq.com"); //上传文件 helper.addAttachment("8a92681cb892bf78ec83af62f2b6a82.jpg",new File("E:\\Users\\Asuna\\Desktop\\html\\8a92681cb892bf78ec83af62f2b6a82.jpg")); helper.addAttachment("8f82df421233241732db6ec6baed07e.jpg",new File("E:\\Users\\Asuna\\Desktop\\html\\8f82df421233241732db6ec6baed07e.jpg")); mailSender.send(mimeMessage); } } |


参考文档:
8、Spring Boot任务的更多相关文章
- 玩转spring boot——快速开始
开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- 玩转spring boot——开篇
很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
- 玩转spring boot——AOP与表单验证
AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...
- 玩转spring boot——结合JPA入门
参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...
- 玩转spring boot——结合JPA事务
接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 玩转spring boot——结合AngularJs和JDBC
参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...
- 玩转spring boot——结合jQuery和AngularJs
在上篇的基础上 准备工作: 修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 玩转spring boot——MVC应用
如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...
随机推荐
- py正则表达式(全是干货系列)
正则表达式的作用在这里不多赘述了,反正处理文本任务贼六就对了.Python中的正则表达式是内置在re模块中的,我们就对这个模块进行详细地讲解.这是一篇媲美帮助文档的文章!对就这么自信,不服你顺着网 ...
- MVC单文件上传
前言 现在来写下最基础的单文件上传,完成后可以扩展成各种不同的上传方式 HTML <input id="Input_File" type="file" / ...
- node初学
安装node.js 往往需要解析环境,但是现在直接安装时就已经配置好了, cmd打开 输入cd/ 在输入node -v 显示版本号 Node与php比较:https://www.techug.co ...
- Lombda表达式(五)
public class Test05 { /* * lambda表达式是用来简化匿名内部类的一种函数式编程的语法. * 只有SAM接口才能使用lambda表达式 * 方法引用和构造器引用是用来简化l ...
- retrofit和RxJava结合
public class MainActivity extends AppCompatActivity { @SuppressLint("CheckResult") protect ...
- Redis学习笔记(六)——数据结构之Set
一.介绍 Redis的Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据. Redis中集合是通过哈希表实现的,所以添加.删除.查找的复杂度都是O(1). 集合中 ...
- STM32入门系列-启动文件介绍
在启动文件内部使用的都是汇编语言,这个文件的作用是负责执行微控制器从"复位"到"开始执行 main 函数"中间这段启动时间所必须进行的工作.它完成的具体工作有: ...
- 【译】Rust中的array、vector和slice
原文链接:https://hashrust.com/blog/arrays-vectors-and-slices-in-rust/ 原文标题:Arrays, vectors and slices in ...
- vue父组件促发子组件中的方法
实现在父组件中促发子组件里面的方法 子组件: <template> <div> 我是子组件 </div> </template> <script& ...
- 【Azure 环境】存储在Azure上的文件,使用IE/Edge时自动打开的问题,如何变为下载而非自动打开
问题描述 存储,作为云服务最重要的一部分.当需要从云存储中下载文件时,时常面临一些格式的文件被浏览器自动打开而非下载,那如何来解决这个问题呢? 在Azure中,存储的服务有以下方式: Azure Bl ...



