生活随笔
收集整理的這篇文章主要介紹了
spring+quartz 完整例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://supben.iteye.com/blog/800704
極其簡單的例子,下載之后啟動一看遍明白。見附件。 工作類:
Java代碼 ?
package ?supben; ??import ?java.sql.Time; ??public ?class ?Job?{?? ??????public ?void ?work(){ ?? ????Time?time???=?new ?Time(System.currentTimeMillis()); ?? ????System.out.println("this?is?a?test?,and?now?is?" +time); ?? ????} ?? }??
package supben;
import java.sql.Time;
public class Job { public void work(){Time time = new Time(System.currentTimeMillis());System.out.println("this is a test ,and now is "+time);}
}
xml配置:
Xml代碼 ?
?? < beans > ?????? ??< bean ?id ="quartzJob" ?class ="supben.Job" > </ bean > ?? ?? ???? ??< bean ?id ="jobtask" ?class ="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > ?? ?????? ????< property ?name ="targetObject" > ?? ??????< ref ?bean ="quartzJob" ?/> ?? ????</ property > ?? ?????? ????< property ?name ="targetMethod" > ?? ??????< value > work</ value > ?? ????</ property > ?? ??</ bean > ?? ?? ???? ??< bean ?id ="doTime" ?class ="org.springframework.scheduling.quartz.CronTriggerBean" > ?? ????< property ?name ="jobDetail" > ?? ??????< ref ?bean ="jobtask" ?/> ?? ????</ property > ?? ?????? ????< property ?name ="cronExpression" > ?? ??????< value > 0/10?*?*?*?*??</ value > ?? ????</ property > ?? ??</ bean > ?? ?? ???? ??< bean ?id ="startQuertz" ?lazy-init ="false" ?autowire ="no" ?class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" > ?? ????< property ?name ="triggers" > ?? ??????< list > ?? ????????< ref ?bean ="doTime" ?/> ?? ??????</ list > ?? ????</ property > ?? ??</ bean > ??
<beans><!-- 要調用的工作類 --><bean id="quartzJob" class="supben.Job"></bean><!-- 定義調用對象和調用對象的方法 --><bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!-- 調用的類 --><property name="targetObject"><ref bean="quartzJob" /></property><!-- 調用類中的方法 --><property name="targetMethod"><value>work</value></property></bean><!-- 定義觸發時間 --><bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="jobtask" /></property><!-- cron表達式 --><property name="cronExpression"><value>0/10 * * * * ?</value></property></bean><!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序 --><bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="doTime" /></list></property></bean>
因為設置了lazy-init="false",所以web項目一起就會執行調度任務。 如果想不起項目,測試 用下面的代碼
Java代碼 ?
public ?static ?void ?main(String[]?args) ??{ ?? ????System.out.println("Test?start." ); ?? ????ApplicationContext?context?=?new ?ClassPathXmlApplicationContext("applicationContext.xml" ); ?? ?????context.getBean("startQuertz" ); ?? ????System.out.print("Test?end.." );??????? ?? }??
?
與50位技術專家面對面 20年技術見證,附贈技術全景圖
總結
以上是生活随笔 為你收集整理的spring+quartz 完整例子 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。