Quartz在Spring中的使用

1.Quartz是一个定时器,用于在某个时间进行任务调度。

在pom.xml中加入Quartz需要的jar信息

<dependency> 

    <groupId>org.quartz-scheduler</groupId >  

    <artifactId>quartz</artifactId >  

    <version>1.8.0</version > 

</dependency> 

2.新建一个quartz.properties文件,在文件中加入

org.quartz.threadPool.threadCount = 10//设置线程数

org.quartz.scheduler.skipUpdateCheck=true//取消检查,因为进行检查会浪费时间

 

 

3.新建一个spring-tdrcs.xml注入文件信息数据,并且设置定时器信息

<bean id="tdrcsConfig" class="org.springframework.beans.factory.config.PropertiesFactoryBean">

        <property name="locations">

            <list>

                <value>classpath:tdrcs/conf-tdrcs/***.properties</value>

            </list>

        </property>

</bean>

 

 

<!-- 定时更新黑名单-->

    <bean id="UpdateBlackListTimer"

        class="com.tangdi.production.tdrcs.timer.Update***ListTimer"><!-- 实现业务逻辑-->

    </bean>

    <bean id="UpdateBlackListTimerDetail"

      class="com.tangdi.production.tdcomm.quartz.TdMethod****DetailFactoryBean"><!-- 该类需要继承MethodInvokingJobDetailFactoryBean-->

        <property name="targetObject" ref="UpdateBlackListTimer" />

        <property name="targetMethod" value="updateBlackList"/>

        <property name="concurrent" value="false" />

    </bean>

 

    <bean id="UpdateBlackListTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

        <property name="jobDetail" ref="UpdateBlackListTimerDetail" />

        <!-- 每天凌晨跑一次 -->

         <property name="cronExpression" value="0 20 1 * * ?" />

        <!--  <property name="cronExpression" value="0 0/1 * * * ?" /> -->

    </bean>

 

    <bean id="schedulerFactoryrcs"

        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

        <property name="triggers">

            <list>                 

<ref bean="UpdateBlackListTrigger" />

            </list>

        </property>

        <!-- quartz配置文件路径 -->

        <property name="configLocation" value="classpath:tdrcs/conf-tdrcs/***.properties" />

    </bean>

    <!-- 定时器 end-->

在主Spring配置文件中引入spring-tdrcs.xml文件

posted on 2017-04-05 09:50  hudashu  阅读(176)  评论(0编辑  收藏  举报

导航