首先我们来写一个被调度的类: package com.kay.quartz;
- publicclassQuartzJob
- {
- publicvoidwork()
- {
- System.out.println("Quartz的任务调度!!!");
- }
- }
Spring的Quartz配置文件:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!--要调用的工作类-->
- <beanid="quartzJob"class="com.kay.quartz.QuartzJob"></bean>
- <!--定义调用对象和调用对象的方法-->
- <beanid="jobtask"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <!--调用的类-->
- <propertyname="targetObject">
- <refbean="quartzJob"/>
- </property>
- <!--调用类中的方法-->
- <propertyname="targetMethod">
- <value>work</value>
- </property>
- </bean>
- <!--定义触发时间-->
- <beanid="doTime"class="org.springframework.scheduling.quartz.CronTriggerBean">
- <propertyname="jobDetail">
- <refbean="jobtask"/>
- </property>
- <!--cron表达式-->
- <propertyname="cronExpression">
- <value>10,15,20,25,30,35,40,45,50,55****?</value>
- </property>
- </bean>
- <!--总管理类如果将lazy-init='false'那么容器启动就会执行调度程序-->
- <beanid="startQuertz"lazy-init="false"autowire="no"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <propertyname="triggers">
- <list>
- <refbean="doTime"/>
- </list>
- </property>
- </bean>
- </beans>测试程序:
- packagecom.kay.quartz;
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
- publicclassMainTest
- {
- /**
- *@paramargs
- */
- publicstaticvoidmain(String[]args)
- {
- System.out.println("Teststart.");
- ApplicationContextcontext=newClassPathXmlApplicationContext("quartz-config.xml");
- //如果配置文件中将startQuertzbean的lazy-init设置为false则不用实例化
- //context.getBean("startQuertz");
- System.out.print("Testend..");
- }
- }
我们需要把log4j的配置文件放入src目录下,启动main类就可以了。