Spring Task定時任務Scheduled

Spring的任務調度,採用註解的形式

Spring中@Scheduled的用法。

spring的配置文件如下,先掃描到任務的類,打開spirng任務的標籤


<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:task="http://www.springframework.org/schema/task"   
    --------------------
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
<!-- 掃描controller -->
<context:component-scan base-package="com.springmvc.controller" />
<task:annotation-driven/>

Spring的任務類HelloJobController

package com.springmvc.controller;

import java.util.Date;

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

@Component
public class HelloJobController {

	@Scheduled(cron = "*/5 * * * * ?")
	//@Scheduled(fixedDelay = 5000)
	public void demoServiceMethod() {
		System.out.println("Method executed at every 5 seconds. Current time is :: " + (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
		
	}

}

運行結果

scheduling

task-scheduling-simplifications-in-spring-3-0

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