views:

207

answers:

3

I can't seem to find a way to force an application-scoped managed bean to be instantiated/initialized when the web app is started. It seems that application-scoped beans get lazy-instantiated the first time the bean is accessed, not when the web app is started up. For my web app this happens when the first user opens a page in the web app for the first time.

The reason I want to avoid this is because a number of time-consuming database operations happen during the initialization of my application-scoped bean. It has to retrieve a bunch of data from persistent storage and then cache some of it that will be frequently displayed to the user in the form of ListItem elements, etc. I don't want all that to happen when the first user connects and thus cause a long delay.

My first thought was to use an old style ServletContextListener contextInitialized() method and from there use an ELResolver to manually request the instance of my managed bean (thus forcing the initialization to happen). Unfortunately, I can't use an ELResolver to trigger the initialization at this stage because the ELResolver needs a FacesContext and the FacesContext only exists during the lifespan of a request.

Does anyone know of an alternate way to accomplish this?

I am using MyFaces 1.2 as the JSF implementation and cannot upgrade to 2.x at this time.

+1  A: 

As far as I know, you can't force a managed bean to be instantiated at application startup.

Maybe you could use a ServletContextListener which, instead of instantiating your managed bean, will perform all the database operations itself?


Another solution might be to instantiate your bean manually at application startup, and then set the bean as an attribute of your ServletContext.

Here is a code sample:

public class MyServletListener extends ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext ctx = sce.getServletContext();
        MyManagedBean myBean = new MyManagedBean();
        ctx.setAttribute("myManagedBean", myManagedBean);
    }

}

In my opinion, this is far from clean code, but it seems like it does the trick.

Vivien Barousse
A: 

Well , Alternative can be create an orphan page,
and map application scoped bean there with the page,
and make a HTTP GET request from ServletContextListener to that page, so you will get your application scoped bean initialized.

org.life.java
Of course its not the good solution, but it will work for your case
org.life.java
+3  A: 

My first thought was to use an old style ServletContextListener contextInitialized() method and from there use an ELResolver to manually request the instance of my managed bean (thus forcing the initialization to happen). Unfortunately, I can't use an ELResolver to trigger the initialization at this stage because the ELResolver needs a FacesContext and the FacesContext only exists during the lifespan of a request.

It doesn't need to be that complicated. Just instantiate the bean and put it in the application scope with the same managed bean name as key. JSF will just reuse the bean when already present in the scope. With JSF on top of Servlet API, the ServletContext represents the application scope (as HttpSession represents the session scope and HttpServletRequest represents the request scope, each with setAttribute() and getAttribute() methods). In a nut:

public void contextInitialized(ServletContextEvent event) {
    event.getServletContext().setAttribute("bean", new Bean());
}

where "bean" should be the same as the <managed-bean-name> of the application scoped bean in faces-config.xml.

BalusC
1+ Simply nice explanation.Learning from you.
org.life.java
+1 for an effective solution. One little question: is it officially ok to do that as per the spec, or does it rely on some JSF implementation details? I mean, a JSF implementation could decide to keep track of whether an application bean was instantiated in a completely non-obvious way and would then recreate the bean, for instance.
ewernli
@BalusC That was so simple and it works. I had avoided using the setAttribute() method in the ServletContext because I thought it would interfere with JSF, but apparently not. PS: Love your page at blogspot.com - your old article on using DataTables was helpful.
Jim Tough
@Jim: you're welcome. @ewernli: the spec doesn't explicitly allow that, but it does also not expliticly disallow that. The spec however describes that a managed bean must be created when not present in the scope.
BalusC
@BalusC, what if application scoped bean has properties that is either JSF managed bean or Spring bean, how to initialize it with fully flagged dependencies?
org.life.java
@org: you would need to take care over them yourself. As a hacky alternative, you can also create a simple view with the application scoped bean attached and call `URL#openStream()` on the view's local URL during `contextInitialized()`.
BalusC
@BalusC , That is what i had suggested, to maintain transitive dependencies.
org.life.java