You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Claudio Tamietto <c....@poker.it> on 2005/06/08 14:47:09 UTC

Re: initialize a mangaged bean in application scope at applicationstartup

if i undestand your answer i have to subclass the class 
org.apache.myfaces.webapp.StartupServletContextListener
and override contextInitialized. Correct ?
  ----- Original Message ----- 
  From: Pierpaolo Follia 
  To: MyFaces Discussion 
  Sent: Tuesday, June 07, 2005 11:00 AM
  Subject: Re: initialize a mangaged bean in application scope at applicationstartup


  No Claudio, you have to use a Servlet that initialize it (maybe simply referincing it via FacesContext).
  Another solution is to create a new ServletContextListener and use the contextInitialized() method to initialize your factory.

  bye

  Claudio Tamietto wrote: 
    i'm tryng to use jpox and jsf and i have used a managed bean at application scope for initialize the PersistenceManagerFactory.
    This activity is very time consuming and start only when the first user request reference the bean. is there a way to initialize the bean
    at application startup and not at the first reference ?

  --
  Pierpaolo Follia

Re: initialize a mangaged bean in application scope at applicationstartup

Posted by Claudio Tamietto <c....@poker.it>.
thank you for the answer and for the sample.


----- Original Message ----- 
From: "steve rock" <st...@gmail.com>
To: "MyFaces Discussion" <us...@myfaces.apache.org>
Sent: Wednesday, June 08, 2005 3:01 PM
Subject: Re: initialize a mangaged bean in application scope at
applicationstartup


Actually you can have multiple application startup listeners, each
doing it's own thing. You don't need to override
StartupServletContextListener. Just make a new one and implement the
functionality that you need. For example I have 2 application scope
listeners, and one session scope listener. In my web.xml:

<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</lis
tener-class>
</listener>

<listener>
<listener-class>com.m2g.stationtools.web.listeners.StationToolsApplicationLi
stener</listener-class>
</listener>

<listener>
<listener-class>com.m2g.stationtools.web.listeners.StationToolsSessionListen
er</listener-class>
</listener>

In StationToolsApplicationListener I load resources like config
parameters from the database, and make sure database connections are
closed when the application shuts down.

In StationToolsSessionListener I load a list of stations from the
database and put it in the session that is a JSF referenced-bean.

Class:
public class StationToolsSessionListener implements HttpSessionListener {

    private static Log log =
LogFactory.getLog(StationToolsSessionListener.class);

    public void sessionCreated(HttpSessionEvent event) {
        log.info("sessionCreated(): Starting");
        HttpSession session = event.getSession();
        session.setAttribute("stationList", new StationList());
        log.info("sessionCreated(): Ending");
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        log.info("sessionDestroyed(): Starting");
        DBUtil.closeSessions();
        log.info("sessionDestroyed(): Ending");
    }
}


faces-config.xml

<faces-config>

     <referenced-bean>
<referenced-bean-name>stationList</referenced-bean-name>
<referenced-bean-class>com.m2g.stationtools.web.faces.model.StationList</ref
erenced-bean-class>
</referenced-bean>

...

Cheers,
-Steve
On 6/8/05, Claudio Tamietto <c....@poker.it> wrote:
>
> if i undestand your answer i have to subclass the class
> org.apache.myfaces.webapp.StartupServletContextListener
> and override contextInitialized. Correct ?
>
>
> ----- Original Message ----- 
> From: Pierpaolo Follia
> To: MyFaces Discussion
> Sent: Tuesday, June 07, 2005 11:00 AM
> Subject: Re: initialize a mangaged bean in application scope at
> applicationstartup
>
> No Claudio, you have to use a Servlet that initialize it (maybe simply
> referincing it via FacesContext).
> Another solution is to create a new ServletContextListener and use the
> contextInitialized() method to initialize your factory.
>
> bye
>
> Claudio Tamietto wrote:
>
> i'm tryng to use jpox and jsf and i have used a managed bean at
application
> scope for initialize the PersistenceManagerFactory.
> This activity is very time consuming and start only when the first user
> request reference the bean. is there a way to initialize the bean
> at application startup and not at the first reference ?
> --
> Pierpaolo Follia
>



Re: initialize a mangaged bean in application scope at applicationstartup

Posted by steve rock <st...@gmail.com>.
Actually you can have multiple application startup listeners, each
doing it's own thing. You don't need to override
StartupServletContextListener. Just make a new one and implement the
functionality that you need. For example I have 2 application scope
listeners, and one session scope listener. In my web.xml:

<listener>    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>    

<listener>		<listener-class>com.m2g.stationtools.web.listeners.StationToolsApplicationListener</listener-class>
</listener>

<listener>		<listener-class>com.m2g.stationtools.web.listeners.StationToolsSessionListener</listener-class>
</listener>

In StationToolsApplicationListener I load resources like config
parameters from the database, and make sure database connections are
closed when the application shuts down.

In StationToolsSessionListener I load a list of stations from the
database and put it in the session that is a JSF referenced-bean.

Class:
public class StationToolsSessionListener implements HttpSessionListener {

    private static Log log =
LogFactory.getLog(StationToolsSessionListener.class);

    public void sessionCreated(HttpSessionEvent event) {
        log.info("sessionCreated(): Starting");
        HttpSession session = event.getSession();
        session.setAttribute("stationList", new StationList());
        log.info("sessionCreated(): Ending");
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        log.info("sessionDestroyed(): Starting");
        DBUtil.closeSessions();
        log.info("sessionDestroyed(): Ending");
    }
}


faces-config.xml

<faces-config>

     <referenced-bean>         
		<referenced-bean-name>stationList</referenced-bean-name>		<referenced-bean-class>com.m2g.stationtools.web.faces.model.StationList</referenced-bean-class>
	</referenced-bean>

...

Cheers,
-Steve
On 6/8/05, Claudio Tamietto <c....@poker.it> wrote:
>  
> if i undestand your answer i have to subclass the class 
> org.apache.myfaces.webapp.StartupServletContextListener 
> and override contextInitialized. Correct ?
>  
>  
> ----- Original Message ----- 
> From: Pierpaolo Follia 
> To: MyFaces Discussion 
> Sent: Tuesday, June 07, 2005 11:00 AM 
> Subject: Re: initialize a mangaged bean in application scope at
> applicationstartup 
> 
> No Claudio, you have to use a Servlet that initialize it (maybe simply
> referincing it via FacesContext).
> Another solution is to create a new ServletContextListener and use the
> contextInitialized() method to initialize your factory.
> 
> bye
> 
> Claudio Tamietto wrote: 
>  
> i'm tryng to use jpox and jsf and i have used a managed bean at application
> scope for initialize the PersistenceManagerFactory. 
> This activity is very time consuming and start only when the first user
> request reference the bean. is there a way to initialize the bean 
> at application startup and not at the first reference ?
> --
> Pierpaolo Follia
>

Re: initialize a mangaged bean in application scope at applicationstartup

Posted by Jan Bols <ja...@ivpv.ugent.be>.
I think he means to subclass just a ServletContextListener like in  
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html

Greetings
Jan

On Wed, 08 Jun 2005 14:47:09 +0200, Claudio Tamietto <c....@poker.it>  
wrote:

> if i undestand your answer i have to subclass the class
> org.apache.myfaces.webapp.StartupServletContextListener
> and override contextInitialized. Correct ?
>   ----- Original Message -----
>   From: Pierpaolo Follia
>   To: MyFaces Discussion
>   Sent: Tuesday, June 07, 2005 11:00 AM
>   Subject: Re: initialize a mangaged bean in application scope at  
> applicationstartup
>
>
>   No Claudio, you have to use a Servlet that initialize it (maybe simply  
> referincing it via FacesContext).
>   Another solution is to create a new ServletContextListener and use the  
> contextInitialized() method to initialize your factory.
>
>   bye
>
>   Claudio Tamietto wrote:
>     i'm tryng to use jpox and jsf and i have used a managed bean at  
> application scope for initialize the PersistenceManagerFactory.
>     This activity is very time consuming and start only when the first  
> user request reference the bean. is there a way to initialize the bean
>     at application startup and not at the first reference ?
>
>   --
>   Pierpaolo Follia