Spring +quartz获取ApplicationContext上下文

job存在数据库中,可以进行动态的增增删改查,最近遇到了如何获取ApplicationContext上下文的问题,解决办法如下

applicationContext-quartz.xml





    
        
        
    

  

对应的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


你可能感兴趣的:(javaEE)