@Scheduled使用

clearLshjTask.properties文件內容

jobs.schedule = */5 * * * * ?

applicationContext-quartz.xml文件

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"  
	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/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
	
	<!-- 自動掃描-->
	<context:component-scan base-package="com.chen.controller" />
	
    <!--TaskExecutor提供的線程池支持也是基於jdk自帶的Executor的-->
    <task:executor id="executor" pool-size="5" />
    
    <!--調度器-->
    <task:scheduler id="scheduler" pool-size="10" />
    
    <!-- 開啓定時任務註解 -->
    <task:annotation-driven executor="executor" scheduler="scheduler" />
    
    <!--要想使用@Value 用${}佔位符注入屬性,這個bean是必須的,這個就是佔位bean,另一種方式是不用value直接用Envirment變量直接getProperty('key')-->
    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"></bean>
	
</beans>

主要定時任務類ClearLshjTask:

package com.chen.controller;

import java.util.Date;

import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:config/clearLshjTask.properties")
public class ClearLshjTask {
	
	@Scheduled(cron="${jobs.schedule}")
	public void doTask(){
		System.out.println("定時任務執行:"+new Date());
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章