用Quartz可以很轻松的实现定时的任务调度,使用Quartz之前需要添加jar包: quartz-1.6.1.jar 下载地址:
三、在web.xml载入applicationContext.xml
图-1
b. 配置web.xml
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="2.5"xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
|
c. 配置applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="" xmlns:xsi="" xmlns:jee="" xsi:schemaLocation="/spring-beans-2.0.xsd /spring-jee-2.0.xsd"> <beanid="MyJobDetail"class="org.springframework.scheduling.quartz.JobDetailBean"> <propertyname="jobClass"value="com.demo.quartz.MyJob"/> <propertyname="jobDataAsMap"> <map> <entrykey="size"value="10"></entry> </map> </property> <propertyname="applicationContextJobDataKey"value="applicationContext"/> </bean> <beanid="MyJobScheduledTask"class="org.springframework.scheduling.quartz.CronTriggerBean"> <propertyname="jobDetail"ref="MyJobDetail"/> <propertyname="cronExpression"value="019106*?*"></property> </bean> <beanid="HiJobDetail"class="org.springframework.scheduling.quartz.JobDetailBean"> <propertyname="jobClass"value="com.demo.quartz.HiJob"/> <propertyname="jobDataAsMap"> <map> <entrykey="size"value="10"></entry> </map> </property> <propertyname="applicationContextJobDataKey"value="applicationContext"/> </bean> <beanid="HiJobScheduledTask"class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <propertyname="startDelay"value="10000"/> <propertyname="repeatInterval"value="2000"/> <propertyname="jobDetail"ref="HiJobDetail"/> </bean> <beanid="QuartzJobFactory"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <propertyname="triggers"> <list> <refbean="MyJobScheduledTask"/> <!-- <refbean="HiJobScheduledTask"/>--> </list> </property> <!--设置是否Spring容器初始化后马上启动Scheduler,默认为true。如果设置为false则需要手工启动Scheduler--> <propertyname="autoStartup"value="true"/> </bean> </beans>
|
d. HiJob类
packagecom.demo.quartz; importjava.util.Date; importorg.quartz.Job; importorg.quartz.JobExecutionContext; importorg.quartz.JobExecutionException; publicclassHiJobimplementsJob { publicvoidexecute(JobExecutionContextcontext) throwsJobExecutionException{ System.out.println("ThisisHiJob,Runtimeis"+newDate()); } }
|
e. MyJob类
packagecom.demo.quartz; importjava.util.Date; importorg.quartz.Job; importorg.quartz.JobExecutionContext; importorg.quartz.JobExecutionException; publicclassMyJobimplementsJob{ publicvoidexecute(JobExecutionContextcontext) throwsJobExecutionException{ System.out.println("ThisisMyJob,Runtimeis"+newDate()); } }
|
f. Client测试类
packagecom.demo.quartz; importorg.quartz.Scheduler; importorg.quartz.SchedulerException; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassClient{ /** *@paramargs */ publicstaticvoidmain(String[]args){ ApplicationContextcontext= newClassPathXmlApplicationContext("applicationContext.xml"); Schedulera=(Scheduler)context.getBean("QuartzJobFactory"); try{ a.start(); }catch(SchedulerExceptione){ e.printStackTrace(); } } }
|
在Spring中集成Quartz极大地方便了任务调度功能的实现,并且通过SchedulerFactoryBean能够和Spring容器的生命周期关联,在Spring容器启动时,启动调度器。
|
|
原文地址 http://space.itpub.net/183473/viewspace-434670,tml |
阅读(1502) | 评论(0) | 转发(0) |