0

I have an ApplicationScoped bean that I'd like to access in a quartz job implementation. That bean holds a hashmap through run-time and I'd like to populate the hashmap when the job runs. However, the FacesContext is out of context inside the job. I have access to the ServletContext. Is it possible to access my bean through the ServletContext?

My code to access the Servlet Context:

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {

    SchedulerContext schedulerContext=null;
    try {
        schedulerContext=context.getScheduler().getContext();
    }
    catch (SchedulerException e) {
        e.printStackTrace();
    }

    ServletContext servletContext=(ServletContext)schedulerContext.get("QuartzServletContext");
    BOCacheM bOCacheM = (BOCacheM) servletContext.getAttribute("bOCacheM");
}

My QuartzServletContext is defined in web.xml as:

<context-param>
    <param-name>quartz:scheduler-context-servlet-context-key</param-name>
    <param-value>QuartzServletContext</param-value>
</context-param>

<listener>
    <listener-class>
        org.quartz.ee.servlet.QuartzInitializerListener
    </listener-class>
</listener>
Rita
  • 1,233
  • 2
  • 14
  • 23
  • Quartz Scheduler and those things use their own thread to run. Looks like some design concerns. The answer is solely dependent upon the functional requirements which are invisible in the question. – Tiny Jan 26 '15 at 17:23

1 Answers1

0

Yes, it's stored as an attribute in ServletContext. Obtain it like any other attribute:

YourApplicationScopedBean bean = servletContext.getAttribute("yourApplicationScopedBeanName");
//use it...

If bean is null then looks like your bean wasn't created when the quartz job started. Make sure the bean is created by adding eager=true to its definition:

@ManagedBean(eager=true)
@ApplicationScoped
public class YourApplicationScopedBean {
    //...

    @PostConstruct
    public void init() {
        //initialize your shared resources here...
    }
}

Note that eager=true only applies for @ApplicationScoped beans.

If this still doesn't work, seems like your quartz job is being fired even before the bean is created and stored in the application context. It would be better to initialize this resource in the ServletContextListener rather than in an @ApplicationScoped bean and provide access to this resource through another component.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Oddly, eager=true does not seem to be be working. The bean is not instantiated when the job runs. Through debugging I've seen the servletContext attributes and my bean is not part of it. Maybe I'm getting the servletContext wrong. – Rita Jan 26 '15 at 17:13
  • I would recommend you to create this `Map` in a class implementing `ServletContextListener#initialize` rather than in your application scoped bean or in a third element like a cache library (Infinispan, EhCache, whatever you like). Note that quartz doesn't fire a HTTP request so it's a bad design that your quartz jobs access to any of the web components e.g. `ServletContext`, `HttpServletSession`, `Servlet` in general... – Luiggi Mendoza Jan 26 '15 at 17:18
  • If the job was the only way I'd be accessing the hash, I'd think your suggestion was correct. However, I have a xhtml file where the hashmap is listed and I have a button that updates the hash so I kind of need it to be in a bean. I appreciate your help though. – Rita Jan 26 '15 at 17:29
  • @Rita you can consume it from your quartz jobs and let your managed beans change it. It will ave an additional cost (like 10 to 50 ms) to perform the changes in the beans because now they have to access to this resource rather than directly to `ServletContext` but still it's not that hard. – Luiggi Mendoza Jan 26 '15 at 17:31
  • I'm trying to figure out why I can't find the bean inside the servletContext. Is it possible that I might be accessing my ServletContext in a wrong manner? – Rita Jan 26 '15 at 17:48
  • @Rita we may know if you provide the relevant code to analyze the problem. – Luiggi Mendoza Jan 26 '15 at 17:49