Springboot与任务整合(四)
一 异步任务
启动类
@MapperScan("com.topcheer.*.*.dao")
@SpringBootApplication
@EnableCaching
@EnableRabbit
@EnableAsync
public class Oss6Application {
public static void main(String[] args) {
SpringApplication.run(Oss6Application.class, args);
}
}
Controller层
/**
* @author WGR
* @create 2019/10/12 -- 21:53
*/
@RestController
public class AsynController {
@Autowired
AsynService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
Service层
/**
* @author WGR
* @create 2019/10/12 -- 21:52
*/
@Service
public class AsynService {
//告诉Spring这是一个异步方法
@Async
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据中...");
}
}
测试结果:
页面直接显示success,控制台过3秒显示处理数据中...
二 定时任务
此处的定时,标注在方法上+注解,假如想修改生成环境的时间,不是很灵活,后面补充Quartz+boot,采用数据库配置和反射的原理。
注:java的cron表达式和Linux的不太一样,请注意,java为6位,linux为5位。

启动类
@SpringBootApplication
@EnableScheduling
public class Oss6Application {
public static void main(String[] args) {
SpringApplication.run(Oss6Application.class, args);
}
}
服务类
@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 * * * * MON-SAT") //每4秒执行一次
public void hello(){
System.out.println("hello ... ");
}
}
三 邮件任务
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<scope>test</scope>
</dependency>
配置文件
spring:
mail:
username: ***********
password: ********* (这是qq邮箱的授权码)
host: smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
测试类
@Autowired(required = false)
JavaMailSenderImpl mailSender;
@Test
public void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
//邮件设置
message.setSubject("通知-今晚开会");
message.setText("今晚7:30开会");
message.setTo("**************");
message.setFrom("**************");
mailSender.send(message);
}
@Test
public void test02() throws Exception{
//1、创建一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//邮件设置
helper.setSubject("测试");
helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);
helper.setTo("***************");
helper.setFrom("**************");
//上传文件
helper.addAttachment("nginx.md",new File("C:\\Users\\asus\\Desktop\\nginx.md"));
mailSender.send(mimeMessage);
}
结果:

总结
简单的介绍了几个任务,后面有时间会详细说明在项目实战的开发应用。
Springboot与任务整合(四)的更多相关文章
- springboot 与 shiro 整合 (简洁版)
前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...
- springboot+mybatis+springmvc整合实例
以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是“零配置”,&quo ...
- 30分钟带你了解Springboot与Mybatis整合最佳实践
前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...
- 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源
目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...
- SpringBoot与Mybatis整合方式01(源码分析)
前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...
- Springboot security cas整合方案-实践篇
承接前文Springboot security cas整合方案-原理篇,请在理解原理的情况下再查看实践篇 maven环境 <dependency> <groupId>org.s ...
- Springboot security cas整合方案-原理篇
前言:网络中关于Spring security整合cas的方案有很多例,对于Springboot security整合cas方案则比较少,且有些仿制下来运行也有些错误,所以博主在此篇详细的分析cas原 ...
- 使用Springboot + Gradle快速整合Mybatis-Plus
使用Springboot + Gradle快速整合Mybatis-Plus 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] MyBatis-Plus(简称 MP)是一个 MyBatis ...
- SpringBoot 2.x 整合ElasticSearch的demo
SpringBoot 2.x 整合ElasticSearch的demo 1.配置文件application.yml信息 # Tomcat server: tomcat: uri-encoding: U ...
- SpringBoot与Dubbo整合下篇
(1)pom.xml引入相关依赖jar包,如下: <dependency> <groupId>com.alibaba</groupId> <artifactI ...
随机推荐
- 数据结构与算法(C/C++版)【数组】
第五章<数组> 一.概念 根据数组中存储的数据元素之间的逻辑关系,可以将数组分为 : 一维数组.二维数组.….n维数组.n维数组中,维数 n 的判断依据是:根据数组中为确定元素所在位置使用 ...
- JsonConfig的jsonConfig.setExcludes的用法
1.问题描述 在项目中经常会有两个类存在一对多或者多对一的关联关系,这样在查询多的一方时,会深入查询关联的一方,而我们可能并不需要去深入查询那些数据,此时使用JsonConfig的jsonConfig ...
- maven 项目报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决
idea在使用maven构建的项目中使用mybatis时报错org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...
- 在window里面安装ubuntu子系统并安装图形化界面
一.开启windows子系统 1. 在win10设置里面开启开发人员选项 (设置-->更新安全--> 开发者选项 )选择开启 2.在控制面板里面开启windows子系统 (启用或关闭wi ...
- Docker系列(一):容器监控工具Weave Scope安装
项目进行容器化之后,配套的基础设施包括监控.编排.管理等都需要进行一并完善.这里也是自己一边学习一边进行记录. Weave Scope 的最大特点是会自动生成一张 Docker 容器地图,让我们能够直 ...
- Flume 学习笔记之 Flume NG概述及单节点安装
Flume NG概述: Flume NG是一个分布式,高可用,可靠的系统,它能将不同的海量数据收集,移动并存储到一个数据存储系统中.轻量,配置简单,适用于各种日志收集,并支持 Failover和负载均 ...
- spring5 源码深度解析----- 事务增强器(100%理解事务)
上一篇文章我们讲解了事务的Advisor是如何注册进Spring容器的,也讲解了Spring是如何将有配置事务的类配置上事务的,实际上也就是用了AOP那一套,也讲解了Advisor,pointcut验 ...
- hihocoder 数论二·Eular质数筛法
数论二·Eular质数筛法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上次我学会了如何检测一个数是否是质数.于是我又有了一个新的问题,我如何去快速得 ...
- 基于mosquitto的MQTT客户端实现C语言
在对MQTT的学习过程中 一下的内容对我提供了帮助 https://www.runoob.com/w3cnote/mqtt-intro.html 对MQTT的入门级介绍 很基础讲解了什么是MQTT h ...
- django rest framework1
内容回顾: 1.开发模式 - 普通开发方式(前后端放在一起写) - 前后端分离 2.后端开发 为前端提供URL(API/接口的开发) 注:永远返回HttpResponse 3.Django FBV.C ...