基本配置:
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()); } } }