使用quartz动态创建定时任务

2017-11-10  本文已影响0人  越吃越胖斯基

最近,因为有个需求 限时卡券 需要动态的创建定时任务 并且支持集群和服务器重启不会丢失.经过多次考虑选择了使用quartz这个框架 因为它是把任务序列化到数据库 即使服务器宕机任务也不会丢失 .

在quartz官网查找 跟网上查找教程并没有资料,即使有也是过时好多年的不能用,为了不让大家走弯路我把我经过两天的研究成功解决这个问题;下面开始写教程

1 因为我的这个项目使用的spring做的管理容器,spring管理quartz很容易

bean id="mapScheduler" lazy-init="false" autowire="no"  class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">

在spring配置文件里注册这个工厂类 quartz就已经集成进来了(忘了说了这应该是quartz的工厂模式)

其实这里的具体细节就是通过JobBuider新建任务 跟触发器放进去执行利用quartz的任务可序列化又支持集群的特点来保证任务不会丢失

说多了没用直接上干货

定时任务类

public class CardJob extends QuartzJobBean {

private ApplicationContext applicationContext;

@Override

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

try {

// applicationContext= (ApplicationContext) context.getMergedJobDataMap().get("applicationContext");

//LogUtils.Log("execute [" + targetObject + "] at once>>>>>>");

Object otargetObject = applicationContext.getBean("pointsExchangeCashFailureBean");

Method m = null;

try {

m = otargetObject.getClass().getMethod("pointsExchangeCashFailure", new Class[] {JobExecutionContext.class}); //方法中的参数是JobExecutionContext类型

m.invoke(otargetObject, new Object[] {context});

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

}

} catch (Exception e) {

throw new JobExecutionException(e);

}

}

public void setApplicationContext(ApplicationContext applicationContext) {

this.applicationContext = applicationContext;

}

这里要说一下这个定时任务类是可序列化的 所以并没有让spring容器作为管理因此我们需要通过反射去spring容器里拿到已经被spring管理的类去执行该执行的方法.

下一步就是该动态创建任务 跟触发器了

@Component

public class QuartzManager {

@Autowired

private SchedulerFactoryBean mapScheduler;

public void addJob(String jobName,String triggerName,String groupName,String code) {

try {

mapScheduler.getScheduler().scheduleJob(getJobDetailFactoryBean(jobName,jobName,code), getSimpleTriggerFactoryBean(triggerName,triggerName));

} catch (SchedulerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void addJob(String jobName,String triggerName,String groupName,String tuokeOpenId,String unionid,String orderNum) {

try {

mapScheduler.getScheduler().scheduleJob(getJobDetailFactoryCouponRecordBean(jobName,jobName,tuokeOpenId,unionid,orderNum), getSimpleTriggerFactoryBean(triggerName,triggerName));

} catch (SchedulerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 定时任务定义

public JobDetail getJobDetailFactoryBean(String jobName,String groupName,String code) {

JobDetail job = JobBuilder.newJob(CardJob.class).withIdentity(jobName, groupName).usingJobData("code", code).build();

return job;

};

// 定时补全信息任务定义

public JobDetail getJobDetailFactoryCouponRecordBean(String jobName,String groupName,String tuokeOpenId,String unionid,String orderNum) {

JobDetail job = JobBuilder.newJob(CouponRecordReplenishMessageJob.class).withIdentity(jobName, groupName)

.usingJobData("tuokeOpenId", tuokeOpenId)

.usingJobData("unionid", unionid)

.usingJobData("orderNum", orderNum)

.build();

return job;

};

// 获取触发器

public SimpleTrigger getSimpleTriggerFactoryBean(String triggerName,String groupName) {

SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(triggerName, groupName)

.startAt(this.LocalDateTimeToUdate(LocalDateTime.now().plusMinutes(10))).build();

return trigger;

}

public Date LocalDateTimeToUdate(LocalDateTime localDateTime) {

ZoneId zone = ZoneId.systemDefault();

Instant instant = localDateTime.atZone(zone).toInstant();

Date date = Date.from(instant);

return date;

}

}

这个类就是创建定时任务的

最后我们就需要在该创建定时任务的地方创建即可

quartzManager.addJob("job"+pointsExchangeRecord.getCode(), "trigger"+pointsExchangeRecord.getCode(), "grop1", pointsExchangeRecord.getCode());

感觉代码写的不是很明白毕竟是我从项目粘贴过来的所以需要大家动动脑筋想想怎么改 demo就不写了

大家有啥不明白或者我有什么不足之处多多指点哈.

上一篇 下一篇

猜你喜欢

热点阅读