上次介紹 Spring Quartz Job Schedule Simple Example 工作排程器 ,現在再來使用 Spring 原生的 Task Execution and Scheduling,首先來瞭解一下 @Scheduled annotation 的意思
This annotation is used for task scheduling. The trigger information needs to be provided along with this annotation. You can use the properties fixedDelay/fixedRate/cron to provide the triggering information. 1. fixedRate: makes Spring run the task on periodic intervals even if the last invocation may be still running. 固定時間執行,不管上一次是否結束 2. fixedDelay: specifically controls the next execution time when the last execution finishes. 上一次執行結束後,再依設定的固定時間執行 3. cron: is a feature originating from Unix cron utility and has various options based on your requirements. BJ4(就不解釋了) |
方法 1) Task scheduling using fixed delay attribute in @Scheduled annotation
Step 01: 設定 applicationContext.xml,將 task 選取
<?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- Spring Task using Annotation--> <task:annotation-driven/> <bean id="SpringTask-Annotation" class="com.mis.demo.SpringTaskAnnotationDemo"/> </beans> |
Step 02: 建立 SpringTaskAnnotationDemo.java
package com.mis.demo; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; public class SpringTaskAnnotationDemo { @Scheduled(fixedDelay = 5000) //@Scheduled(fixedRate = 5000) public void demoServiceMethod() { System.out.println("Method executed at every 5 seconds. Current time is :: "+ new Date()); } } |
這樣就可以運作了,簡單吧 !!
方法 2) Task scheduling using cron expression in @Scheduled annotation
Step 01: 在 applicationContext.xml 設定 class 的 task:scheduled 執行方法,所以在 SpringTaskExecutorDemo.java 的執行 method 為 execute。
<!-- Spring Task Using cron --> <bean id="SpringTask-Cron" class="com.mis.demo.SpringTaskExecutorDemo"/> <task:scheduled-tasks> <task:scheduled ref="SpringTask-Cron" method="execute" cron="*/5 * * * * ?"/> </task:scheduled-tasks> |
Step 02: 建立 SpringTaskExecutorDemo.java
package com.mis.demo; import java.util.Calendar; public class SpringTaskExecutorDemo { public void execute () { System.out.println("SpringTaskExecutorDemo: Run at " + Calendar.getInstance().getTimeInMillis() ); } } |
這樣就可以運作了,也很簡單吧 !!
參考:
http://howtodoinjava.com/spring/spring-core/4-ways-to-schedule-tasks-in-spring-3-scheduled-example/
http://jimwayne.blogspot.tw/2014/04/spring-framework.html
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html