首页 > 代码库 > 快速入門系列-spring和quartz
快速入門系列-spring和quartz
檔案說明
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="http://www.mamicode.com/sync" /> </bean> <!-- 每五秒就執行一次 --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="runMeJob" /> <property name="repeatInterval" value="http://www.mamicode.com/5000" /> <property name="startDelay" value="http://www.mamicode.com/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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。