Springboot幾種任務(wù)的整合方法
這篇文章主要介紹了Springboot幾種任務(wù)的整合方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
一 異步任務(wù)
啟動(dòng)類
@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這是一個(gè)異步方法
@Async
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("處理數(shù)據(jù)中...");
}
}
測(cè)試結(jié)果:
頁面直接顯示success,控制臺(tái)過3秒顯示處理數(shù)據(jù)中...
二 定時(shí)任務(wù)
此處的定時(shí),標(biāo)注在方法上+注解,假如想修改生成環(huán)境的時(shí)間,不是很靈活,后面補(bǔ)充Quartz+boot,采用數(shù)據(jù)庫配置和反射的原理。
注:java的cron表達(dá)式和Linux的不太一樣,請(qǐng)注意,java為6位,linux為5位。
啟動(dòng)類
@SpringBootApplication
@EnableScheduling
public class Oss6Application {
public static void main(String[] args) {
SpringApplication.run(Oss6Application.class, args);
}
}
服務(wù)類
@Service
public class ScheduledService {
/**
* second(秒), minute(分), hour(時(shí)), day of month(日), month(月), day of week(周幾).
* 0 * * * * MON-FRI
* 【0 0/5 14,18 * * ?】 每天14點(diǎn)整,和18點(diǎn)整,每隔5分鐘執(zhí)行一次
* 【0 15 10 ? * 1-6】 每個(gè)月的周一至周六10:15分執(zhí)行一次
* 【0 0 2 ? * 6L】每個(gè)月的最后一個(gè)周六凌晨2點(diǎn)執(zhí)行一次
* 【0 0 2 LW * ?】每個(gè)月的最后一個(gè)工作日凌晨2點(diǎn)執(zhí)行一次
* 【0 0 2-4 ? * 1#1】每個(gè)月的第一個(gè)周一凌晨2點(diǎn)到4點(diǎn)期間,每個(gè)整點(diǎn)都執(zhí)行一次;
*/
// @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秒執(zhí)行一次
public void hello(){
System.out.println("hello ... ");
}
}
三 郵件任務(wù)
pom.xml
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<scope>test</scope>
</dependency>
配置文件
spring: mail: username: *********** password: ********* (這是qq郵箱的授權(quán)碼) host: smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true
測(cè)試類
@Autowired(required = false)
JavaMailSenderImpl mailSender;
@Test
public void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
//郵件設(shè)置
message.setSubject("通知-今晚開會(huì)");
message.setText("今晚7:30開會(huì)");
message.setTo("**************");
message.setFrom("**************");
mailSender.send(message);
}
@Test
public void test02() throws Exception{
//1、創(chuàng)建一個(gè)復(fù)雜的消息郵件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//郵件設(shè)置
helper.setSubject("測(cè)試");
helper.setText("<b style='color:red'>今天 7:30 開會(huì)</b>",true);
helper.setTo("***************");
helper.setFrom("**************");
//上傳文件
helper.addAttachment("nginx.md",new File("C:\\Users\\asus\\Desktop\\nginx.md"));
mailSender.send(mimeMessage);
}
結(jié)果:

總結(jié)
簡(jiǎn)單的介紹了幾個(gè)任務(wù),后面有時(shí)間會(huì)詳細(xì)說明在項(xiàng)目實(shí)戰(zhàn)的開發(fā)應(yīng)用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot整合quartz實(shí)現(xiàn)多個(gè)定時(shí)任務(wù)的方法
- springboot整合Quartz實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法
- 詳解SpringBoot開發(fā)案例之整合定時(shí)任務(wù)(Scheduled)
- springboot整合quartz實(shí)現(xiàn)定時(shí)任務(wù)示例
- SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法
- springboot schedule 解決定時(shí)任務(wù)不執(zhí)行的問題
- SpringBoot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)
相關(guān)文章
Java實(shí)現(xiàn)猜數(shù)字小游戲代碼
大家好,本篇文章主要講的是Java實(shí)現(xiàn)猜數(shù)字小游戲代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
springboot 排除redis的自動(dòng)配置操作
這篇文章主要介紹了springboot 排除redis的自動(dòng)配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
java之a(chǎn)ssert關(guān)鍵字用法案例詳解
這篇文章主要介紹了java之a(chǎn)ssert關(guān)鍵字用法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Springboot快速集成sse服務(wù)端推流(最新整理)
SSE?Server-Sent?Events是一種允許服務(wù)器向客戶端推送實(shí)時(shí)數(shù)據(jù)的技術(shù),它建立在?HTTP?和簡(jiǎn)單文本格式之上,提供了一種輕量級(jí)的服務(wù)器推送方式,通常也被稱為“事件流”(Event?Stream),這篇文章主要介紹了Springboot快速集成sse服務(wù)端推流(最新整理),需要的朋友可以參考下2024-02-02
Spring的@Value如何從Nacos配置中心獲取值并自動(dòng)刷新
這篇文章主要介紹了Spring的@Value如何從Nacos配置中心獲取值并自動(dòng)刷新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Mybatis批量插入數(shù)據(jù)返回主鍵的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis批量插入數(shù)據(jù)返回主鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Java實(shí)現(xiàn)動(dòng)物換位游戲完整?過程詳解
大家好,今天嘗試用Java編程設(shè)計(jì)一個(gè)GUI界面的動(dòng)物換位游戲,游戲的結(jié)果是讓左右兩組的動(dòng)物交換位置,以下是具體設(shè)計(jì)過程,供大家參考2022-07-07

