您的位置:首页 > 编程语言 > Java开发

Springboot几种任务的整合方法

2020-03-11 17:51 369 查看

这篇文章主要介绍了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);
​
}

结果:

总结

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

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring boot 任务 整合