Quartz动态管理一次性定时任务(Spring)
2017-01-22 11:41
387 查看
本次实现采用的是简陋的Quartz实现。在Spring容器中配好相应的组件包。不采用配置文件实现每天定时循环任务,本次实现的是一次性定时。(几天后执行一个操作,操作完成后删除该定时,每次操作的内容不相同)。在执行任务的时候注入Spring管理的对象。
1、动态的添加删除定时任务
2、添加定时任务
3、执行动态定时测试类
以上就可以实现动态的添加删除不同的一次性定时任务。
备注:如果要在Spring启动时完成这样的一次定时任务添加,需要注意必须等待Spring加载完毕后,ContextLoader.getCurrentWebApplicationContext()才能获取到对应的servletContext 实例对象,如果发现加载时取不到,可以用线程等待休眠一段时间,等Spring容器先加载完毕后,即可获取。
1、动态的添加删除定时任务
import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerKey; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.CronTriggerImpl; import org.springframework.context.ApplicationContext; public class TriggerManager { /** * 添加定时任务 * @param JobName 工作名称,可任意定义,不重复 * @param JobGroupName 工作组名称,建议统一规划,可重复 * @param time 启动的时间 * @param jobClass 要执行的工作类 * @param parameter 传递的参数 * @param servletContent application对象,取得spring bean对象的关键 */ @SuppressWarnings({ "deprecation" }) public static void addJob(String JobName, String JobGroupName, String time, Class<? extends Job> jobClass,String parameter,ApplicationContext servletContent) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = new JobDetailImpl(JobName, JobGroupName, jobClass);// 任务名,任务组,任务执行类 jobDetail.getJobDataMap().put("jobName", parameter); jobDetail.getJobDataMap().put("servletContent", servletContent); CronTrigger trigger = new CronTriggerImpl(JobName, JobGroupName, time);// 触发器名,触发器组,触发时间 scheduler.scheduleJob(jobDetail, trigger);// 将任务信息添加到sheduler中 } catch (Exception e) { e.printStackTrace(); } } public static void delJob(String JobName, String JobGroupName) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); TriggerKey triggerKey = new TriggerKey(JobName, JobGroupName); JobKey jobKey = new JobKey(JobName, JobGroupName); scheduler.pauseTrigger(triggerKey);// 停止触发器 scheduler.unscheduleJob(triggerKey);// 删除触发器 scheduler.deleteJob(jobKey);// 删除任务 } catch (SchedulerException e) { e.printStackTrace(); } } }
2、添加定时任务
@Deprecated private void test(){ //在任意地方取得application对象 WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext(); ServletContext servletContext = webApplicationContext.getServletContext(); ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(servletContext); String time = null; try { time = ConversionTime.conversionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-01-01 01:01")); } catch (ParseException e1) { e1.printStackTrace(); } TriggerManager.addJob("test","default", time,com.shcedule.JTest.class,"test",application); Scheduler scheduler = null; try { //添加并启动 scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); } }
3、执行动态定时测试类
import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.ApplicationContext; @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap dataMap= context.getJobDetail().getJobDataMap(); String jobName = dataMap.getString("jobName"); ApplicationContext servletContext = (ApplicationContext) dataMap.get("servletContent"); JTest test= servletContext.getBean(JTest.class); try { System.out.println("jobName"+jobName); test.sendMessage("crazy"); } catch (Exception e) { e.printStackTrace(); } TriggerManager.delJob(jobName,jobName); } }
以上就可以实现动态的添加删除不同的一次性定时任务。
备注:如果要在Spring启动时完成这样的一次定时任务添加,需要注意必须等待Spring加载完毕后,ContextLoader.getCurrentWebApplicationContext()才能获取到对应的servletContext 实例对象,如果发现加载时取不到,可以用线程等待休眠一段时间,等Spring容器先加载完毕后,即可获取。
相关文章推荐
- Spring 动态管理定时任务(使用quartz) 只是管理启动时间 不能做启动和暂停
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- [置顶] spring整合quartz实现动态定时任务的前台网页配置与管理
- Quartz+Spring Boot实现动态管理定时任务
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- Spring动态对Quartz定时任务的管理,实现动态加载,停止的配置实例代码
- Spring 3整合Quartz 2实现定时任务二:动态添加任务
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)------转帖
- Spring 3整合Quartz 2实现定时任务三:动态暂停 恢复 修改和删除任务
- Quartz在Spring中动态设置cronExpression研究(spring设置动态定时任务)
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
- Quartz在Spring中动态设置cronExpression研究(spring设置动态定时任务)
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
- Quartz在Spring中设置动态定时任务
- Quartz在Spring中设置动态定时任务 .
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)