springtask 的使用方法和 cron 表達(dá)式解析
spring 容器依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency>
開啟任務(wù)注解驅(qū)動(dòng)。即掃描的時(shí)候掃描 springtask 相關(guān)的注解。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd
">
<context:component-scan base-package="com.mozq.task"/>
<task:annotation-driven/>
</beans>
準(zhǔn)備 Spring 容器 + 定時(shí)任務(wù)
為了使用 springtask 需要準(zhǔn)備 spring 容器和定時(shí)任務(wù)。通過 main 方法創(chuàng)建 spring 容器。@Scheduled 注解創(chuàng)建定時(shí)任務(wù)。
package com.mozq.task;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TaskDemo {
@Scheduled(cron="* * * * * ?")
public void sendOrderMessage(){
System.out.println("發(fā)送訂單消息");
}
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring-task.xml");
Thread.sleep(1000);
}
}
參考文檔
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring schedule配置多任務(wù)動(dòng)態(tài)cron(增刪啟停)
- SpringMVC Cron定時(shí)器Demo常見問題解決方案
- springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù)(實(shí)例代碼)
- Spring Boot支持Crontab任務(wù)改造的方法
- springboot Quartz動(dòng)態(tài)修改cron表達(dá)式的方法
- Spring @Scheduler使用cron表達(dá)式時(shí)的執(zhí)行問題詳解
- Spring的@Scheduled 如何動(dòng)態(tài)更新cron表達(dá)式
相關(guān)文章
Java實(shí)現(xiàn)SHA-1算法實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)SHA-1算法,實(shí)例分析了java實(shí)現(xiàn)SHA-1算法的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
SpringBoot接收J(rèn)SON類型的參數(shù)方式
這篇文章主要介紹了SpringBoot接收J(rèn)SON類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
MyBatis 動(dòng)態(tài)SQL和緩存機(jī)制實(shí)例詳解
這篇文章主要介紹了MyBatis 動(dòng)態(tài)SQL和緩存機(jī)制實(shí)例詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-09-09
Java?@Scheduled定時(shí)任務(wù)不執(zhí)行解決辦法
這篇文章主要給大家介紹了關(guān)于Java?@Scheduled定時(shí)任務(wù)不執(zhí)行解決的相關(guān)資料,當(dāng)@Scheduled定時(shí)任務(wù)不執(zhí)行時(shí)可以根據(jù)以下步驟進(jìn)行排查和解決,需要的朋友可以參考下2023-10-10
Java實(shí)現(xiàn)讀取SFTP服務(wù)器指定目錄文件的方法
SFTP是一種在安全通道上傳輸文件的協(xié)議,它是基于SSH(Secure Shell)協(xié)議的擴(kuò)展,用于在客戶端和服務(wù)器之間進(jìn)行加密的文件傳輸,這篇文章主要介紹了Java實(shí)現(xiàn)讀取SFTP服務(wù)器指定目錄文件,感興趣的朋友跟隨小編一起看看吧2023-08-08
Java使用建造者模式實(shí)現(xiàn)辦理手機(jī)套餐功能詳解
這篇文章主要介紹了Java使用建造者模式實(shí)現(xiàn)辦理手機(jī)套餐功能,較為詳細(xì)的描述了建造者模式的概念、原理并結(jié)合實(shí)例形式分析了Java使用建造者模式實(shí)現(xiàn)的辦理手機(jī)套餐功能具體步驟與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-05-05
SpringBoot實(shí)現(xiàn)點(diǎn)餐系統(tǒng)的登錄與退出功能流程詳解
結(jié)束了Springboot+MyBatisPlus也是開始了項(xiàng)目之旅,將從后端的角度出發(fā)來(lái)整理這個(gè)項(xiàng)目中重點(diǎn)業(yè)務(wù)功能的梳理與實(shí)現(xiàn)2022-10-10

