700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring3.2.4集成quartz2.2.1定时任务(demo).

Spring3.2.4集成quartz2.2.1定时任务(demo).

时间:2024-04-18 23:44:21

相关推荐

Spring3.2.4集成quartz2.2.1定时任务(demo).

独角兽企业重金招聘Python工程师标准>>>

在JavaEE系统中,我们会经常用到定时任务,下面是我自己写的一个demo.

源码地址:/s/1BXHv3

1.所需要的jar

2.实体bean

package cn.zyc.quartz;import java.util.Date;public class Study {public void doStudy() {System.out.println("It is " + new Date());}}

3.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:p="/schema/p"xmlns:aop="/schema/aop" xmlns:context="/schema/context"xmlns:tx="/schema/tx"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.0.xsd/schema/tx /schema/tx/spring-tx-3.0.xsd /schema/aop /schema/aop/spring-aop-3.0.xsd /schema/context /schema/context/spring-context-3.0.xsd"><!-- bean --><bean id="study" class="cn.zyc.quartz.Study" /><!-- 定义调用对象和调用对象的方法 --><bean id="studyDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!-- 调用的类 --><property name="targetObject" ref="study" /><!-- 调用类中的方法 --><property name="targetMethod" value="doStudy" /><!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 --><property name="concurrent" value="false"/></bean><!-- quartz-1.8以前的配置 <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="studyDetail" /> </property> <property name="cronExpression"> <value>0/1 * * * * ?</value> </property> </bean> --><!-- quartz-2.x的配置 --><!-- 定义触发时间 --><bean id="myJobTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail"><ref bean="studyDetail" /></property><property name="cronExpression"><value>0/5 * * * * ?</value></property></bean><!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --><!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 --><bean name="startQuertz" lazy-init="false" autowire="no"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="myJobTrigger" /></list></property><!-- <property name="autoStartup" value="true"/> --></bean></beans>

4.web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="/xml/ns/javaee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。