• Spring +quartz获取ApplicationContext上下文


    job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题。解决的方法例如以下

    applicationContext-quartz.xml

    <?xml version="1.0" encoding="UTF-8"?

    > <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/> <property name="configLocation" value="classpath:quartz.properties"/> </bean> </beans>


    <!--applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了quartz的上下文中了。能够用applicationContextSchedulerContextKey所定义的key得到相应的spring上下文-->  

    相应的job任务

    public class QuartzJobBean implements Job {
    
    
    	/* (non-Javadoc)
    	 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
    	 */
    	@Override
    	public void execute(JobExecutionContext jobContext) throws JobExecutionException {
    		
    		
    		String jobId = jobContext.getJobDetail().getDescription();
    		String serviceId = jobContext.getTrigger().getDescription();
    		
    
    		ApplicationContext applicationContext=null;
    		try {
    			applicationContext=getApplicationContext(jobContext);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		WebServiceService webServiceService = (WebServiceService) applicationContext.getBean("webServiceService");
    		WebServicePOJO  webService = webServiceService.getWebService(serviceId);
    		ScheduleService.schedule(webService.getNameSpace(), webService.getServiceName(), webService.getWsdlURL(), webService.getMethod());
    	}
    	
    	private static final String APPLICATION_CONTEXT_KEY = "applicationContextKey";
    	private ApplicationContext getApplicationContext(JobExecutionContext context) throws Exception {
    			ApplicationContext appCtx = null;
    			appCtx = (ApplicationContext) context.getScheduler().getContext().get(APPLICATION_CONTEXT_KEY);
    			if (appCtx == null) {
    				throw new JobExecutionException("No application context available in scheduler context for key "" + APPLICATION_CONTEXT_KEY + """);
    			}
    			return appCtx;
    	}
    
    
    }
    APPLICATION_CONTEXT_KEY的值是第一个XML中配置的值,通过getApplicationContext方法传入quartz的context就可以获取ApplicationContext上下文,进而获取相应的bean


  • 相关阅读:
    使用CAShapeLayer做出圆形的进度条 —— #DF!
    NSDate分类,把系统返回的时间与现在进行比较---秀清
    NSPredicate类,指定过滤器的条件---董鑫
    UITextFIeld的输入格式问题 ----W.P
    AttributeText创建多彩文字Label --- hl
    自定义简单动画
    Swift 学习网址精选 By HL
    【C++服务端技术】移动广播
    【C++服务端技术】定时器
    【面试攻略】C++面试-4399
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6708233.html
Copyright © 2020-2023  润新知