现在的位置: 首页 > 编程语言 > 正文

Springboot几种任务的整合方法

2020年02月13日 编程语言 ⁄ 共 2708字 ⁄ 字号 评论关闭

这篇文章主要介绍了Springboot几种任务的整合方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一 异步任务

启动类

@MapperScan("com.topcheer.*.*.dao")@SpringBootApplication@EnableCaching@EnableRabbit@EnableAsyncpublic class Oss6Application { public static void main(String[] args) { SpringApplication.run(Oss6Application.class, args); }}

Controller层

/** * @author WGR * @create 2019/10/12 -- 21:53 */@RestControllerpublic class AsynController {​ @Autowired AsynService asyncService;​ @GetMapping("/hello") public String hello(){ asyncService.hello(); return "success"; }}

Service层

/** * @author WGR * @create 2019/10/12 -- 21:52 */@Servicepublic 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@EnableSchedulingpublic class Oss6Application { public static void main(String[] args) { SpringApplication.run(Oss6Application.class, args); }}

服务类

@Servicepublic 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.comspring.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 开会",true);​ helper.setTo("***************"); helper.setFrom("**************");​ //上传文件 helper.addAttachment("nginx.md",new File("C:\\Users\\asus\\Desktop\\nginx.md"));​ mailSender.send(mimeMessage);​ }

结果:

总结

简单的介绍了几个任务,后面有时间会详细说明在项目实战的开发应用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: Springboot几种任务的整合方法

以上就上有关Springboot几种任务的整合方法的相关介绍,要了解更多springboot,任务,整合内容请登录学步园。

抱歉!评论已关闭.