Quartz兩個定時任務的配置
阿新 • • 發佈:2019-02-02
1、首先與spring進行整合,需要在spring的總配置中或者在web.xml中spring監聽中加上
applicationContext-quartz.xml。 我是直接加了個xml檔案。
注:根據自己的業務需要,設定定時器配置。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 業務建立的定時任務 --> <bean id="類名1" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>類路徑</value> </property> </bean> <bean id="類名2" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>類路徑</value> </property> </bean> <!-- 觸發器配置 時間指定 -->
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="類名1" />
<property name="cronExpression">
<!--每10秒執行一次 -->
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 觸發器配置 時間指定 -->
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="類名2" /> <property name="cronExpression"> <!--每10秒執行一次 --> <value>0/10 * * * * ?</value> </property> </bean> <!-- 總管理類 如果將lazy-init='false' 那麼容器啟動就會執行排程程式 --> <bean id="schedulerFactoryBean" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="configLocation" value="classpath:quartz.properties" /> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <property name="triggers"> <list> <ref bean="cronTrigger1" /> <ref bean="cronTrigger2" /> </list> </property> </bean> </beans>