开发者

Start class right after deployment, not at session start for JSF

开发者 https://www.devze.com 2022-12-20 00:18 出处:网络
For a web application I make use of JSF 1.2 and Facelets. The problem is that we now do the initialisation via a singleton pattern and that takes about 5-15 seconds because it read in data files (we

For a web application I make use of JSF 1.2 and Facelets.

The problem is that we now do the initialisation via a singleton pattern and that takes about 5-15 seconds because it read in data files (we are not using a database). This happens when the first user browses to the corresponding web page (the 2nd and other users don't have this delay).

I would like to have this singleton initialised right after deployment. How can I do this? I've tried to add an application bean but it does not get called. I've also tried to add a servlet as followings:

  <servlet>
    <description>MyApplicationContextListener S开发者_开发百科ervlet</description>
    <display-name>MyApplicationContextListener Servlet</display-name>
    <servlet-name>MyApplicationContextListener</servlet-name>
    <servlet-class>mydomain.beans.MyApplicationContextListener</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <listener>
    <listener-class>mydomain.beans.MyApplicationContextListener</listener-class>
  </listener>

with the following code:

package mydomain.beans;
import javax.servlet.ServletContextEvent;

public class MyApplicationContextListener {


    public void contextInitialized(ServletContextEvent event) {
        System.out.println("MyApplicationContextListener.contextInitialized started");
    }

    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("MyApplicationContextListener.contextInitialized stopped");
    }

}

An example including changes needed in web.xml and/or faces-config.xml would be nice!


How about using a ServletContextListener ? Its contextInitialized(..) method will be called at the moment the context is initialized. It's mapped in web.xml like this:

<listener>
    <listener-class>com.example.MyServletContextListener</listener-class>
</listener>

Also, (not sure this would work), you can configure your faces-servlet to be loaded on startup.:

<load-on-startup>1</load-on-startup>

Clarification: For the listener approach, your listener must implement the ServletContextListener:

public class MyServletContextListener implements ServletContextListener { .. }
0

精彩评论

暂无评论...
验证码 换一张
取 消