zoukankan      html  css  js  c++  java
  • 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();
    		} 
    	} 
    };


     
  • 相关阅读:
    POJ1006 UVA756 UVALive5421 Biorhythms【中国剩余定理】
    HDU2098 分拆素数和
    HDU2098 分拆素数和
    HDU2099 整除的尾数【模除】
    HDU2099 整除的尾数【模除】
    I00003 贝尔三角形
    I00003 贝尔三角形
    模乘逆元与孙子定理
    Maximal Binary Matrix CodeForces
    带精度问题的二分的方法
  • 原文地址:https://www.cnblogs.com/beiyeren/p/3473650.html
Copyright © 2011-2022 走看看