@AutowiredprivateJobFactory jobFactory;/*** spring容器启动就创建一个quartz调度器,其它地方通过这个bean根据业务生成触发器和任务
*@returnquartz调度器*/@BeanpublicSchedulerFactoryBean createSchedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean){
SchedulerFactoryBean schedulerFactoryBean=newSchedulerFactoryBean();
schedulerFactoryBean.setApplicationContextSchedulerContextKey(GlobalVariable.APPLICATION_CONTEXT_KEY);
schedulerFactoryBean.setStartupDelay(GlobalVariable.QUARTZ_NOTIFICATION_START_DELAY);
schedulerFactoryBean.setAutoStartup(GlobalVariable.QUARTZ_AUTO_START);
schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(GlobalVariable.QUARTZ_WAIT_FOR_JOBS_TO_COMPLETE_ON_SHUTDOWN);
schedulerFactoryBean.setJobFactory(jobFactory);
schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject());returnschedulerFactoryBean;
}/*** 创建一个订单过期的job
*
* 这种的只适合spring初始化容器的时候固定执行任务
*
*@return创建一个job*/@BeanpublicJobDetailFactoryBean createBookingExpiredJob() {
JobDetailFactoryBean jobDetailFactoryBean=newJobDetailFactoryBean();
jobDetailFactoryBean.setGroup(GlobalVariable.QUARTZ_NOTIFICATION_JOB_GROUP_NAME);
jobDetailFactoryBean.setName(GlobalVariable.QUARTZ_NOTIFICATION_BOOKING_EXPIRED_JOB_NAME);
jobDetailFactoryBean.setJobClass(JobTest.class);
MapdataAsMap=newHashMap<>();
dataAsMap.put("booking","123");
jobDetailFactoryBean.setJobDataAsMap(dataAsMap);returnjobDetailFactoryBean;
}/*** 创建一个订单过期的trigger
*
* 这种的只适合spring初始化容器的时候固定执行任务
*
*@return创建一个trigger*/@BeanpublicCronTriggerFactoryBean createBookingExpiredTrigger(JobDetailFactoryBean jobDetailFactoryBean) {
CronTriggerFactoryBean cronTriggerFactoryBean=newCronTriggerFactoryBean();
cronTriggerFactoryBean.setGroup(GlobalVariable.QUARTZ_NOTIFICATION_JOB_GROUP_NAME);
cronTriggerFactoryBean.setName(GlobalVariable.QUARTZ_NOTIFICATION_BOOKING_EXPIRED_TRIGGER);
String cronExpression="5/3 * * * * ? *";
cronTriggerFactoryBean.setCronExpression(cronExpression);
cronTriggerFactoryBean.setTimeZone(TimeZone.getDefault());
cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());returncronTriggerFactoryBean;
}/*** 这种的直接把方法作为job,不需要实现job接口*/public MethoInvokingJobDetailFactoryBeancreateMethodInvokingJobDetailFactoryBean() {
MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean=newMethodInvokingJobDetailFactoryBean();
methodInvokingJobDetailFactoryBean.setTargetClass(null);
methodInvokingJobDetailFactoryBean.setTargetMethod("doJob");
methodInvokingJobDetailFactoryBean.setConcurrent(false);
methodInvokingJobDetailFactoryBean.setGroup("notification");
methodInvokingJobDetailFactoryBean.setName("notification-booking-expired");