Quartz in Spring memo: part 6

我试了一下,用spring的MethodInvokingJobDetailFactoryBean,然后在job class里面用到其他的spring的bean,那这个job details是不能被persist的,因为不能序列化。如果不用MethodInvokingJobDetailFactoryBean,那job class根本就不用在spring里面配置,所以也不能在job class里面引用spring的bean了。在quartz的文档里看到有一个办法,可以从job class里面拿到application context。

首先是在SchedulerFactoryBean的配置里面配置一下applicationContextSchedulerContextKey属性,然后在job class里面可以用如下的方法拿到(job class一定要实现quartz的job接口):ApplicationContext appContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext"); 代码如下:

   1: <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
   2:         <property name="triggers">
   3:             <list>
   4:             </list>
   5:         </property>
   6:         <property name="jobDetails">
   7:             <list>
   8:                 <ref local="myJobDetail"/>
   9:             </list>
  10:         </property>
  11:         <property name="dataSource" ref="dataSource"/>
  12:         <property name="configLocation" value="classpath:quartz.properties"/>
  13:         <property name="applicationContextSchedulerContextKey" value="applicationContext"/>
  14:     </bean>

   1: ApplicationContext appContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");

Related posts:

  1. Quartz in Spring memo: part 5
  2. Quartz in Spring memo: part 3
This entry was posted in java, memo and tagged , , . Bookmark the permalink.
blog comments powered by Disqus