一 异步任务

启动类

@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与任务整合(四)的更多相关文章

  1. springboot 与 shiro 整合 (简洁版)

    前言: 网上有很多springboot 与 shiro 整合的资料,有些确实写得很好, 对学习shiro和springboot 都有很大的帮助. 有些朋友比较省事, 直接转发或者复制粘贴.但是没有经过 ...

  2. springboot+mybatis+springmvc整合实例

    以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是“零配置”,&quo ...

  3. 30分钟带你了解Springboot与Mybatis整合最佳实践

    前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...

  4. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  5. SpringBoot与Mybatis整合方式01(源码分析)

    前言:入职新公司,SpringBoot和Mybatis都被封装了一次,光用而不知道原理实在受不了,于是开始恶补源码,由于刚开始比较浅,存属娱乐,大神勿喷. 就如网上的流传的SpringBoot与Myb ...

  6. Springboot security cas整合方案-实践篇

    承接前文Springboot security cas整合方案-原理篇,请在理解原理的情况下再查看实践篇 maven环境 <dependency> <groupId>org.s ...

  7. Springboot security cas整合方案-原理篇

    前言:网络中关于Spring security整合cas的方案有很多例,对于Springboot security整合cas方案则比较少,且有些仿制下来运行也有些错误,所以博主在此篇详细的分析cas原 ...

  8. 使用Springboot + Gradle快速整合Mybatis-Plus

    使用Springboot + Gradle快速整合Mybatis-Plus 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] MyBatis-Plus(简称 MP)是一个 MyBatis ...

  9. SpringBoot 2.x 整合ElasticSearch的demo

    SpringBoot 2.x 整合ElasticSearch的demo 1.配置文件application.yml信息 # Tomcat server: tomcat: uri-encoding: U ...

  10. SpringBoot与Dubbo整合下篇

    (1)pom.xml引入相关依赖jar包,如下: <dependency> <groupId>com.alibaba</groupId> <artifactI ...

随机推荐

  1. 使用webgl(three.js)搭建3D智慧园区、3D大屏,3D楼宇,智慧灯杆三维展示,3D灯杆,web版3D,bim管理系统——第六课

    前言: 今年是建国70周年,爱国热情异常的高涨,为自己身在如此安全.蓬勃发展的国家深感自豪. 我们公司楼下为庆祝国庆,拉了这样的标语,每个人做好一件事,就组成了我们强大的祖国. 看到这句话,深有感触, ...

  2. Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)

    Spring.Spring MVC.MyBatis整合(未万待续)

  3. springboot 使用i18n进行国际化乱码解决

    方式1.设置国际化的编码和你使用的编译器(IDEA之类)一致,如编译器为UTF-8则在application配置文件中添加 #i18n spring: messages: encoding: UTF- ...

  4. java工作错误总结

    1.访问接口出现以下错误 com.alibaba.dubbo.rpc.RpcException: Forbid consumer 192.168.200.126 access service com. ...

  5. Java 基础篇之反射

    反射 使用反射获取程序运行时的对象和类的真实信息. 获取 Class 对象 每个类被加载之后,系统会为该类生成一个对应的 Class 对象,通过该 Class 对象可以访问到 JVM 中的这个类. 使 ...

  6. RabbitMQ原理介绍

    RabbitMQ历史 RabbitMQ消息系统是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.在同步消息通讯的世界里有很多公开标准(如COBAR的IIO ...

  7. sbt 学习笔记(2)sbt生成项目导入eclipse

    在sbt配置eclipse插件 C:\Users\Administrator\.sbt\0.13\ 新建plugins目录,在目录中新建plugins.sbt 文件内容为: addSbtPlugin( ...

  8. 通​过​C​a​c​t​i​监​控​w​i​n​d​o​w​s​资​源

    前提条件 一.已安装好Cacti:ubuntu下cacti安装配置 二.准备好以下安装文件: Cacti_SNMP_Informant_Standard_Metrics_v13.zip(该cacti监 ...

  9. Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...

  10. UVA - 1160 X-Plosives

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...