利用spring的ApplicationContext在程序中唤醒quartz的job
1.AppService:一个持有ApplicationContext的单例
2.timerContext.xml quartz定时器配置文件
3.唤醒job代码
请注意红色部分
3.唤醒job代码
- AppService.setContext(new ClassPathXmlApplicationContext("classpath:atf/config/spring/applicationContext.xml"));
- CronTriggerBean trigger = (CronTriggerBean) AppService.getService(CronTriggerBean.class,"atf.ticket.pnrChkTrigger");
- System.out.println(trigger.getNextFireTime());
- <span style="color: #ff0000;">org.quartz.Scheduler scheduler=(</span>org.quartz.impl.StdScheduler)AppService.getContext().getBean<span style="color: #ff0000;">("schedulerFactoryService");</span>
- JobDetail job=trigger.getJobDetail();
- <span style="color: #ff0000;">scheduler.triggerJob(job.getName(),job.getGroup());</span>
2.quartz定时器配置文件
- <!-- 定时器 -->
- <bean <span style="color: #ff0000;">id="schedulerFactoryService"</span> class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="atf.ticket.pnrChkTrigger" />
- </list>
- </property>
- </bean>
- <!--pnr 自动审核 1小时一次 -->
- <bean id="atf.ticket.pnrChkTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="atf.ticket.pnrChkTask" />
- </property>
- <property name="cronExpression">
- <value>0 0 0/1 * * ?</value>
- </property>
- </bean>
- <bean id="atf.ticket.pnrChkTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject">
- <ref bean="atf.pnr.pnrAutoCheckProcesser" />
- </property>
- <property name="targetMethod">
- <value>checkPnr</value>
- </property>
- <property name="concurrent">
- <value>false</value>
- </property>
- </bean>
1.AppService:一个持有ApplicationContext的单例模式
- public class AppService
- {
- private AppService()
- {
- }
- private static ApplicationContext context = null;
- public static ApplicationContext getContext()
- {
- if (context == null)
- context = new ClassPathXmlApplicationContext("classpath:atf/config/spring/applicationContext.xml");
- return context;
- }
- public static void setContext(ApplicationContext aContext)
- {
- context = aContext;
- }
- public static <T> Object getService(Class<T> type, final String service)
- {
- return getContext().getBean(service);
- }
- public static void autowireService(Object bean)
- {
- ((AbstractApplicationContext) context).getBeanFactory().autowireBeanProperties(bean,
- AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
- }
- public static Statistics getStatistics()
- {
- SessionFactory sessionFactory = (SessionFactory) getContext().getBean("hibernateSessionFactory");
- Statistics statistics = sessionFactory.getStatistics();
- return statistics;
- }
- }
本文介绍如何使用Spring框架整合Quartz调度器实现任务的即时触发。通过创建单例模式的AppService类来管理Spring的ApplicationContext,并从配置文件中获取Quartz的JobDetail和Trigger,最终手动触发指定的任务。

317

被折叠的 条评论
为什么被折叠?



