首页 > 代码库 > sping的quartz设置定时任务

sping的quartz设置定时任务

除了spring相关的jar包之外,还需要引入quartz-all-1.6.6.jar。

spring配置文件增加quartz-bean.xml和quartz-set.xml

quartz-bean.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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">               <!-- 测试  -->    <bean id="testQuartzTask" class="com.tech.jin.quartz.TestQuartzTask"/>    </beans>

quartz-set.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:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <!-- 定时任务定义 -->    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="quartzProperties">           <props>              <prop key="org.quartz.threadPool.threadCount">50</prop>           </props>        </property>        <!-- 自动启动 -->           <property name="autoStartup">            <value>true</value>           </property>        <property name="triggers">            <list>                <ref local="testQuartzTaskTrigger" ></ref><!-- 测试自动任务 --><!--                 <ref local="TransQueryJobTrigger" /> -->                            </list>        </property>    </bean>        <!-- 测试自动任务 -->    <bean id="testQuartzTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">        <property name="jobDetail">            <ref bean="testQuartzTaskJobDetail"/>        </property>        <property name="cronExpression">             <value>0/10 * * * * ? </value>        </property>        <property name="jobDataAsMap">            <map>                <entry key="jobClass" value="testQuartzTask"/>                <entry key="jobName" value="测试自动任务"/>            </map>         </property>    </bean>    <bean id="testQuartzTaskJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">        <property name="targetObject"><ref bean="testQuartzTask"/></property>            <property name="targetMethod"><value>doTask</value></property>           <property name="concurrent" value="false"/>    </bean>    </beans>

调用的类TestQuartzTask:

package com.tech.jin.quartz;import org.apache.log4j.Logger;public class TestQuartzTask {        private Logger logger = Logger.getLogger(TestQuartzTask.class);        public void doTask(){        logger.info("66666666666666666666666");    }}

 

除此之外,还需要在web.xml中添加上读取sping配置文件的配置:classpath:spring/quartz-*xml

  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>        classpath:spring/ApplicationContext.xml;        classpath:spring/quartz-*xml    </param-value>  </context-param>

 

sping的quartz设置定时任务