檔案說明
pom.xml - maven設定
Main.class 主程式
StoreOrderSyncTask.class - 一個需要固定執行的Task,裡面只有一個方法
Spring-Quartz.xml - 設定要固定執行的Task,和執行的時間
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>conno</groupId> <artifactId>schedule</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.6</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies> </project>
Main.class
public static void main(String[] args) { //spring config的擺放位址 new ClassPathXmlApplicationContext("Spring-Quartz.xml"); }
StoreOrderSyncTask.class
package syncDBTask; public class StoreOrderSyncTask { public void sync(){ System.out.println("sync..."); } }
Spring-Quartz.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="runMeTask" class="syncDBTask.StoreOrderSyncTask" /> <bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="runMeTask" /> <property name="targetMethod" value="sync" /> </bean> <!-- 每五秒就執行一次 --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="repeatInterval" value="5000" /> <property name="startDelay" value="1000" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean> </beans>
運行結果
本文出自 “程序元” 博客,请务必保留此出处http://timfan1121.blog.51cto.com/8757381/1405384