You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ex...@myrealbox.com on 2004/02/04 20:59:43 UTC

global data objects -- best practices?

hi,

I have a question regarding data objects that should be accessible from 
the entire application and which must be created at container startup time.

I need to either create and load an existing index or load a serialized 
index if one exists. This can be a time-consuming process, and I would 
like to do it once when the servlet container starts, then keep the 
index in memory thereafter and available to the entire web application.

I've seen a few different ways of doing this by searching the archives, 
but there wasn't a definitive best practice solution that I came across, 
and the struts API has also changed since many of the suggestions were 
offered.

At present, I am extending the ActionServlet and taking care of the 
initialization stuff in the subclass's init(). I place the generated 
index in the ServletContext. This ensures that the index creation will 
occur only once, at container startup, and that it will be available to 
the action classes of that web application, but I'm not sure if this is 
the best solution. Also, I have just come across plugins for the first 
time, and it seems that they also provide a way of doing 
application-specific initialization. Having the index creation code in 
the ActionServlet, as it is at present, seems messy to me, so perhaps 
that is not the best place for it.

In short, what is the best method for ensuring that a global index is 
created exactly once, and at container startup time? At present, I am 
subclassing ActionServlet, and doing it there, but is the plugin 
mechanism an alternate (better?) solution? Is the ServletContext the 
appropriate place for global data?

thanks in advance,

n.

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: global data objects -- best practices?

Posted by "Craig R. McClanahan" <cr...@apache.org>.
Ooofff ... off-by-one error warning ... :-)

Quoting "Craig R. McClanahan" <cr...@apache.org>:

> 
> If you are in a Servlet 2.4 environment, use a ServletContextListener -- this
> is
> exactly what they are defined to do.  Your contextInitialized() method is
> called once, before any requests are accepted by the app, and
> contextDestroyed() is called exactly once when the app is removed (or the
> server is shut down).

This actually works from Servlet 2.3 onward.

> If you are in a Servlet 2.3 environment, use a Struts PlugIn.  Under most
> scenarios, this will have the same effect (as long as you set the
> load-on-startup attribute in web.xml); however servlet containers are
> allowed
> to unload and reload a servlet during the lifetime of an application, which
> would cause the initialization to be redone at reload.  Fortunately,
> containers
> don't tend to do that -- especially not to a servlet that will get invoked
> as
> often as the Struts controller servlet does.
> 

This advice actually applies to Servlet 2.2 platforms, before
ServletContextListener was part of the standard Servlet spec.

> Craig McClanahan

Craig


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: global data objects -- best practices?

Posted by "Craig R. McClanahan" <cr...@apache.org>.
Quoting exnihilo@myrealbox.com:

> hi,
> 
> I have a question regarding data objects that should be accessible from 
> the entire application and which must be created at container startup time.
> 
> I need to either create and load an existing index or load a serialized 
> index if one exists. This can be a time-consuming process, and I would 
> like to do it once when the servlet container starts, then keep the 
> index in memory thereafter and available to the entire web application.
> 
> I've seen a few different ways of doing this by searching the archives, 
> but there wasn't a definitive best practice solution that I came across, 
> and the struts API has also changed since many of the suggestions were 
> offered.
> 
> At present, I am extending the ActionServlet and taking care of the 
> initialization stuff in the subclass's init(). I place the generated 
> index in the ServletContext. This ensures that the index creation will 
> occur only once, at container startup, and that it will be available to 
> the action classes of that web application, but I'm not sure if this is 
> the best solution. Also, I have just come across plugins for the first 
> time, and it seems that they also provide a way of doing 
> application-specific initialization. Having the index creation code in 
> the ActionServlet, as it is at present, seems messy to me, so perhaps 
> that is not the best place for it.
> 
> In short, what is the best method for ensuring that a global index is 
> created exactly once, and at container startup time? At present, I am 
> subclassing ActionServlet, and doing it there, but is the plugin 
> mechanism an alternate (better?) solution? Is the ServletContext the 
> appropriate place for global data?
> 
> thanks in advance,
> 
> n.
> 

If you are in a Servlet 2.4 environment, use a ServletContextListener -- this is
exactly what they are defined to do.  Your contextInitialized() method is
called once, before any requests are accepted by the app, and
contextDestroyed() is called exactly once when the app is removed (or the
server is shut down).

If you are in a Servlet 2.3 environment, use a Struts PlugIn.  Under most
scenarios, this will have the same effect (as long as you set the
load-on-startup attribute in web.xml); however servlet containers are allowed
to unload and reload a servlet during the lifetime of an application, which
would cause the initialization to be redone at reload.  Fortunately, containers
don't tend to do that -- especially not to a servlet that will get invoked as
often as the Struts controller servlet does.

Craig McClanahan


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: global data objects -- best practices?

Posted by am...@pgmsystems.com.




I use a plugin (implements org.apache.struts.action.PlugIn) and put things
I need application-wide in the ServletContext.



                                                                           
             exnihilo@myrealbo                                             
             x.com                                                         
                                                                        To 
             02/04/2004 02:59          struts-user@jakarta.apache.org      
             PM                                                         cc 
                                                                           
                                                                   Subject 
             Please respond to         global data objects -- best         
               "Struts Users           practices?                          
               Mailing List"                                               
             <struts-user@jaka                                             
              rta.apache.org>                                              
                                                                           
                                                                           
                                                                           




hi,

I have a question regarding data objects that should be accessible from
the entire application and which must be created at container startup time.

I need to either create and load an existing index or load a serialized
index if one exists. This can be a time-consuming process, and I would
like to do it once when the servlet container starts, then keep the
index in memory thereafter and available to the entire web application.

I've seen a few different ways of doing this by searching the archives,
but there wasn't a definitive best practice solution that I came across,
and the struts API has also changed since many of the suggestions were
offered.

At present, I am extending the ActionServlet and taking care of the
initialization stuff in the subclass's init(). I place the generated
index in the ServletContext. This ensures that the index creation will
occur only once, at container startup, and that it will be available to
the action classes of that web application, but I'm not sure if this is
the best solution. Also, I have just come across plugins for the first
time, and it seems that they also provide a way of doing
application-specific initialization. Having the index creation code in
the ActionServlet, as it is at present, seems messy to me, so perhaps
that is not the best place for it.

In short, what is the best method for ensuring that a global index is
created exactly once, and at container startup time? At present, I am
subclassing ActionServlet, and doing it there, but is the plugin
mechanism an alternate (better?) solution? Is the ServletContext the
appropriate place for global data?

thanks in advance,

n.

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: global data objects -- best practices?

Posted by si...@siberian.org.
Thats an interesting question.

Specifically to Tomcat and struts, does the JNDI stuff 
accomplish this similiarly to how it handles pooled db 
connections?

http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html

So, if you create a new resource in the config does it 
create that resource at tomcat start-up, on first use or 
on every use?

New to it all sorry if its a stupid question. I've been 
seeing a need for this in my apps as well.

John-

On Wed, 04 Feb 2004 11:59:43 -0800
  exnihilo@myrealbox.com wrote:
>hi,
>
>I have a question regarding data objects that should be 
>accessible from the entire application and which must be 
>created at container startup time.
>
>I need to either create and load an existing index or 
>load a serialized index if one exists. This can be a 
>time-consuming process, and I would like to do it once 
>when the servlet container starts, then keep the index in 
>memory thereafter and available to the entire web 
>application.
>
>I've seen a few different ways of doing this by searching 
>the archives, but there wasn't a definitive best practice 
>solution that I came across, and the struts API has also 
>changed since many of the suggestions were offered.
>
>At present, I am extending the ActionServlet and taking 
>care of the initialization stuff in the subclass's 
>init(). I place the generated index in the 
>ServletContext. This ensures that the index creation will 
>occur only once, at container startup, and that it will 
>be available to the action classes of that web 
>application, but I'm not sure if this is the best 
>solution. Also, I have just come across plugins for the 
>first time, and it seems that they also provide a way of 
>doing application-specific initialization. Having the 
>index creation code in the ActionServlet, as it is at 
>present, seems messy to me, so perhaps that is not the 
>best place for it.
>
>In short, what is the best method for ensuring that a 
>global index is created exactly once, and at container 
>startup time? At present, I am subclassing ActionServlet, 
>and doing it there, but is the plugin mechanism an 
>alternate (better?) solution? Is the ServletContext the 
>appropriate place for global data?
>
>thanks in advance,
>
>n.
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: 
>struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: 
>struts-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: global data objects -- best practices?

Posted by ex...@myrealbox.com.
Robert Taylor wrote:
> Yes, the ServletContext is the most appropriate place to put it if you
> want your data to be available during the life time of the application.
> 
> If your container conforms to Servlet spec. 2.3 or higher, a
> solution is to implement one or more ServletContextListener(s). 
> Is the a best practice? I think that may be subjective, but it does
> not bind you to any third party solutions.
> 
> The container must invoke these upon application start up before it
> processes any requests.
> 
> For more information refer to the Servlet 2.3 spec or higher.
> http://java.sun.com/aboutJava/communityprocess/first/jsr053/index.html
> 
> robert

Thanks, Robert. I looked at the spec (which is very well written and 
easy to follow, btw), and a ServletContextListener seems perfect for my 
purposes, and was designed for exactly my use case.

cheers,

n.

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: global data objects -- best practices?

Posted by Robert Taylor <rt...@mulework.com>.
Yes, the ServletContext is the most appropriate place to put it if you
want your data to be available during the life time of the application.

If your container conforms to Servlet spec. 2.3 or higher, a
solution is to implement one or more ServletContextListener(s). 
Is the a best practice? I think that may be subjective, but it does
not bind you to any third party solutions.

The container must invoke these upon application start up before it
processes any requests.

For more information refer to the Servlet 2.3 spec or higher.
http://java.sun.com/aboutJava/communityprocess/first/jsr053/index.html

robert

> -----Original Message-----
> From: exnihilo@myrealbox.com [mailto:exnihilo@myrealbox.com]
> Sent: Wednesday, February 04, 2004 3:00 PM
> To: struts-user@jakarta.apache.org
> Subject: global data objects -- best practices?
> 
> 
> hi,
> 
> I have a question regarding data objects that should be accessible from 
> the entire application and which must be created at container 
> startup time.
> 
> I need to either create and load an existing index or load a serialized 
> index if one exists. This can be a time-consuming process, and I would 
> like to do it once when the servlet container starts, then keep the 
> index in memory thereafter and available to the entire web application.
> 
> I've seen a few different ways of doing this by searching the archives, 
> but there wasn't a definitive best practice solution that I came across, 
> and the struts API has also changed since many of the suggestions were 
> offered.
> 
> At present, I am extending the ActionServlet and taking care of the 
> initialization stuff in the subclass's init(). I place the generated 
> index in the ServletContext. This ensures that the index creation will 
> occur only once, at container startup, and that it will be available to 
> the action classes of that web application, but I'm not sure if this is 
> the best solution. Also, I have just come across plugins for the first 
> time, and it seems that they also provide a way of doing 
> application-specific initialization. Having the index creation code in 
> the ActionServlet, as it is at present, seems messy to me, so perhaps 
> that is not the best place for it.
> 
> In short, what is the best method for ensuring that a global index is 
> created exactly once, and at container startup time? At present, I am 
> subclassing ActionServlet, and doing it there, but is the plugin 
> mechanism an alternate (better?) solution? Is the ServletContext the 
> appropriate place for global data?
> 
> thanks in advance,
> 
> n.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org