quartz 的job中獲取到applicationContext


第一步:
定義SchedulerFactoryBean的applicationContextSchedulerContextKey
<bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
                <!-- 注入數據源 -->  
		<property name="dataSource">  
			<ref bean="dataSource" />  
		</property>
                <!-- 延遲30秒啟動Scheduler -->  
		<property name="startupDelay" value="30"></property>
                <!-- 通過applicationContextSchedulerContextKey屬性配置spring上下文 -->  
		<property name="applicationContextSchedulerContextKey">  
			<value>applicationContext</value>  
		</property> 
</bean>
 
定義之后,產生的效果是:
this.scheduler.getContext().put(this.applicationContextSchedulerContextKey, this.applicationContext);
第二步:
獲取到scheduler,然后從中取出applicationContext 即可
public class TestJob extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		Scheduler scheduler = (Scheduler) context.getScheduler();  
		//獲取JobExecutionContext中的service對象  
               try {
                        //獲取JobExecutionContext中的service對象 
			SchedulerContext schCtx = context.getScheduler().getContext();
                        //獲取Spring中的上下文  
			ApplicationContext appCtx = (ApplicationContext)schCtx.get("applicationContext");
			jobService= (JobService)appCtx.getBean("jobService");
			....
		} catch (SchedulerException e1) {
			// TODO 尚未處理異常
			e1.printStackTrace();
		} 
	} 
};


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM