springtask 的使用方法和 cron 表達式解析
更新時間:2019年10月14日 11:04:06 作者:魔有追求
這篇文章主要介紹了springtask 的使用方法和 cron 表達式解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
spring 容器依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency>
開啟任務注解驅(qū)動。即掃描的時候掃描 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>
準備 Spring 容器 + 定時任務
為了使用 springtask 需要準備 spring 容器和定時任務。通過 main 方法創(chuàng)建 spring 容器。@Scheduled 注解創(chuàng)建定時任務。
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);
}
}
參考文檔
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java?@Scheduled定時任務不執(zhí)行解決辦法
這篇文章主要給大家介紹了關(guān)于Java?@Scheduled定時任務不執(zhí)行解決的相關(guān)資料,當@Scheduled定時任務不執(zhí)行時可以根據(jù)以下步驟進行排查和解決,需要的朋友可以參考下2023-10-10
Java實現(xiàn)讀取SFTP服務器指定目錄文件的方法
SFTP是一種在安全通道上傳輸文件的協(xié)議,它是基于SSH(Secure Shell)協(xié)議的擴展,用于在客戶端和服務器之間進行加密的文件傳輸,這篇文章主要介紹了Java實現(xiàn)讀取SFTP服務器指定目錄文件,感興趣的朋友跟隨小編一起看看吧2023-08-08
SpringBoot實現(xiàn)點餐系統(tǒng)的登錄與退出功能流程詳解
結(jié)束了Springboot+MyBatisPlus也是開始了項目之旅,將從后端的角度出發(fā)來整理這個項目中重點業(yè)務功能的梳理與實現(xiàn)2022-10-10

