• spring-quartz定时器简单用法


    基本配置:

    1.quartz.properties

    #==============================================================    
    #Configure Main Scheduler Properties    
    #==============================================================     
    org.quartz.scheduler.instanceName = quartzScheduler  
    org.quartz.scheduler.instanceId = AUTO  
      
    #==============================================================    
    #Configure JobStore    
    #==============================================================   
    org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
    org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
    org.quartz.jobStore.tablePrefix = QRTZ_  
    org.quartz.jobStore.isClustered = true  
    org.quartz.jobStore.clusterCheckinInterval = 20000    
    org.quartz.jobStore.dataSource = myDS  
       
    #==============================================================    
    #Configure ThreadPool    
    #==============================================================   
    org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
    org.quartz.threadPool.threadCount = 10  
    org.quartz.threadPool.threadPriority = 5  
    org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

    2.spring-quartz.xml文件调度工厂配置

    <!-- ======================== 调度工厂 ======================== -->
        <bean id="SpringJobSchedulerFactoryBean"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="customerCardTaskTriggerBean" />
                    <ref bean="transactionCardStatisticeTaskTriggerBean" />
                    <ref bean="reportStatisticeSettlementTaskTriggerBean" />
                    <ref bean="checkActivityExpiredTaskTriggerBean" />
                    <ref bean="checkActivityProgressTaskTriggerBean" />
                    <ref bean="sendUserCouponTaskTriggerBean" />
                    <ref bean="marketStatisticsTaskTriggerBean" />
                    <ref bean="precisionTaskTriggerBean" />
                    <ref bean="remindUserCouponExpireTaskTriggerBean" />
                    <ref bean="updateUserCouponExpiredTaskTriggerBean" />
                    <ref bean="preorderTimeoutTaskTriggerBean" />
                    <ref bean="generateRecommendOrderTaskTriggerBean"/>
                    <ref bean="employeeCommissionTaskTriggerBean" />
                    <ref bean="employeeBonusTaskTriggerBean" />
                    <ref bean="bonusGrantMoneyTaskTriggerBean" />
                    <ref bean="birthdayCouponTaskTriggerBean" />
                </list>
            </property>
            <!-- 文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文 -->
            <property name="applicationContextSchedulerContextKey" value="applicationContext" />
        </bean>

    3.spring-quartz.xml文件调度任务配置。

    <bean id="birthdayCouponTaskTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="birthdayCoupon" />
            <property name="cronExpression" value="${birthdayCouponCron}" />
        </bean>
        <bean id="birthdayCoupon"
              class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="birthdayCouponTask" />
            <property name="targetMethod" value="sendActivityCoupons" />
        </bean>

    4.实现具体调用功能。

    @Component("birthdayCouponTask")
    @Slf4j
    public class BirthdayCouponTask {
    
        @Reference
        private ActivityService activityService;
    
        public void sendActivityCoupons() {
            log.info("开始执行生日赠券定时任务。");
            try {
                activityService.sendActivityCoupons(Constants.ACTIVITY_TYPE_BIRTHDAY);
            }catch (Exception e) {
                log.info("生日赠券发券异常:" + e.getMessage());
            }
    
        }
    
    }
  • 相关阅读:
    SpringSource发布Spring Data Redis 1.0.0
    C#实现的GDI+时钟
    敏捷团队应对打扰的七种方法
    JBoss发布Hibernate 4.0
    在 IE10 的 XHR 的麦子
    Spring AMQP 1.0 GA发布了
    对 64 个以上逻辑处理器使用任务管理器
    预览Visual Studio11: 敏捷的支持、团队协作以及代码克隆监测
    在 Windows 8 中支持传感器
    HTTP API可演进性最佳实践
  • 原文地址:https://www.cnblogs.com/arli/p/9109915.html
Copyright © 2020-2023  润新知