Spring中的Quartz配置

首先我们来写一个被调度的类: package com.kay.quartz;

 
  1. publicclassQuartzJob
  2. {
  3. publicvoidwork()
  4. {
  5. System.out.println("Quartz的任务调度!!!");
  6. }
  7. }

Spring的Quartz配置文件:

 
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <!--要调用的工作类-->
  5. <beanid="quartzJob"class="com.kay.quartz.QuartzJob"></bean>
  6. <!--定义调用对象和调用对象的方法-->
  7. <beanid="jobtask"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  8. <!--调用的类-->
  9. <propertyname="targetObject">
  10. <refbean="quartzJob"/>
  11. </property>
  12. <!--调用类中的方法-->
  13. <propertyname="targetMethod">
  14. <value>work</value>
  15. </property>
  16. </bean>
  17. <!--定义触发时间-->
  18. <beanid="doTime"class="org.springframework.scheduling.quartz.CronTriggerBean">
  19. <propertyname="jobDetail">
  20. <refbean="jobtask"/>
  21. </property>
  22. <!--cron表达式-->
  23. <propertyname="cronExpression">
  24. <value>10,15,20,25,30,35,40,45,50,55****?</value>
  25. </property>
  26. </bean>
  27. <!--总管理类如果将lazy-init='false'那么容器启动就会执行调度程序-->
  28. <beanid="startQuertz"lazy-init="false"autowire="no"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  29. <propertyname="triggers">
  30. <list>
  31. <refbean="doTime"/>
  32. </list>
  33. </property>
  34. </bean>
  35. </beans>测试程序:
  36. packagecom.kay.quartz;
  37. importorg.springframework.context.ApplicationContext;
  38. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  39. publicclassMainTest
  40. {
  41. /**
  42. *@paramargs
  43. */
  44. publicstaticvoidmain(String[]args)
  45. {
  46. System.out.println("Teststart.");
  47. ApplicationContextcontext=newClassPathXmlApplicationContext("quartz-config.xml");
  48. //如果配置文件中将startQuertzbean的lazy-init设置为false则不用实例化
  49. //context.getBean("startQuertz");
  50. System.out.print("Testend..");
  51. }
  52. }

我们需要把log4j的配置文件放入src目录下,启动main类就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值