'Null applicationContext from Quartz Job
I have a QuartzJobBean and I want to access my Spring Application Context from my Quartz job. I was following the directions located on the following blog. However, the following code is not working properly:
private ApplicationContext getApplicationContext(JobExecutionContext context ) throws Exception {
System.err.println("check if variables are null - " + (context.getScheduler().getContext() == null));
System.err.flush();
ApplicationContext 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;
}
The following messages are printed at runtime:
check if if variables are null - false
No application context available in scheduler context for key "applicationContext"
I'm a bit confused about what I might have done wrong. Is it possible that my Spring application context is not named "applicationContext"? If so, where would the name of the spring application context be configured? Any other ideas about why my application context is null?
Thanks.
Solution 1:[1]
100% working Can you try the following steps.
- Create a class who will implement the ApplicationContextAware
- Give the implementation to the method
- create a static variable of ApplicationContext
- Get that variable in your class
The code will look like.
public class ApplicationUtils implements ApplicationContextAware {
private static ApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
ctx = appContext;
}
public static ApplicationContext getApplicationContext() {
return ctx;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lotus |