You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "C.F. Scheidecker Antunes" <na...@antunes.eti.br> on 2005/08/10 00:16:13 UTC

Application context bean

Hello all,

I would like to have a bean with a data structure that gets populated 
when the application starts and then this bean is available to the 
entire application. The data structure will get populated via a data 
base. I need to call the database just once to populate the data 
structure. In case the database changes I would restart the server and 
have this bean populated again. Hence all the sessions can query the 
data structure within this bean. How can I achieve this?

Thanks in advance,

C.F.

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


Re: Application context bean

Posted by Dave Newton <ne...@pingsite.com>.
C.F. Scheidecker Antunes wrote:

> Hello all,
>
> I would like to have a bean with a data structure that gets populated 
> when the application starts and then this bean is available to the 
> entire application. The data structure will get populated via a data 
> base. I need to call the database just once to populate the data 
> structure. In case the database changes I would restart the server and 
> have this bean populated again. Hence all the sessions can query the 
> data structure within this bean. How can I achieve this?

At least two easy options:

(1) Create, populate, store from a servlet that is run on initialization
(2) Create, populate, store from a Struts plugin

Restarting the server seems a little drastic for a database change; you 
might want to considerr a more user-friendly option! :)

Dave



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


Re: Application context bean

Posted by Dave Newton <ne...@pingsite.com>.
Wendy Smoak wrote:

> Use a ServletContextListener, which will be notified when the context 
> starts and stops.

Or that ;)

As an aside, is there a good reason why this would be better than a 
startup servlet or plugin?

Dave



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


Re: Application context bean

Posted by Dave Newton <ne...@pingsite.com>.
Craig McClanahan wrote:

>Besides being more deterministic in this scenario, one quickly gets
>hooked on the ability to get notified when *attributes* are changed in
>request/session/application scope, as well as lifecycle events --
>without having to modify the classes of the attribute instances
>themselves.  Listeners are cool.
>  
>
Indeed--I've used them for other things, just never changed my 
initialization stuff. There's a lot of useful "lifecycle" type things 
that I've found tend to be under-used that can make development much easier.

Dave



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


Re: Application context bean

Posted by Craig McClanahan <cr...@gmail.com>.
On 8/10/05, Dave Newton <ne...@pingsite.com> wrote:
> Frank W. Zammetti wrote:
> 
> > Probably a better idea to be more deterministic and do it with a
> > listener though, for the reasons Craig expertly described.
> 
> I agree.
> 
> Thanks guys--I'll definitely make the switch!
> 

Besides being more deterministic in this scenario, one quickly gets
hooked on the ability to get notified when *attributes* are changed in
request/session/application scope, as well as lifecycle events --
without having to modify the classes of the attribute instances
themselves.  Listeners are cool.

> Dave

Craig

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


Re: Application context bean

Posted by Dave Newton <ne...@pingsite.com>.
Frank W. Zammetti wrote:

> Probably a better idea to be more deterministic and do it with a 
> listener though, for the reasons Craig expertly described.

I agree.

Thanks guys--I'll definitely make the switch!

Dave



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


Re: Application context bean

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
Craig beat me to it :)  That's exactly what I was going to say.

One point to make... if you do it with a servlet anyway and you read in 
your config and store it in a bean as static members, and one of the 
fields you set is a boolean configured field, and only populate the bean 
if its false, then the servlet being created and destroyed doesn't 
really matter, your code will still work as expected.

Probably a better idea to be more deterministic and do it with a 
listener though, for the reasons Craig expertly described.

Frank

Craig McClanahan wrote:
> On 8/9/05, Dave Newton <ne...@pingsite.com> wrote:
> 
>>Frank W. Zammetti wrote:
>>
>>
>>>P.S. - Someone else asked if a listener is better than a startup
>>>servlet of Struts plug-in... IMO, it's better than a plug-in because
>>>it's one less thing that is Struts-specific... Arguably it isn't any
>>>better than a startup servlet I suppose.
>>
>>I'm still using startup servlets out of habit and was just wondering if
>>there was any compelling reason to switch over to session listeners--any
>>differences under clustering or something? I dunno.
>>
> 
> 
> On most existing containers it probably doesn't matter, but you are
> relying on a non-required-by-the-specs behavior isse when you do this.
>  (The exact same issue affects Struts plugins, which are managed in
> the init() and destroy() methods of ActionServlet.)
> 
> The servlet container is *explicity* allowed (if it wants to) to
> unload a servlet at any point during the application's lifetime, which
> would include a call to destroy().  If a subsequent call for this
> servlet comes in, it will get reinitialized via init().  Why might a
> container want to do this?  Maybe it is seeing memory getting full,
> and no calls to your servlet (quite likely if it is solely used for
> initialization).  Just like an OS might want to swap out an idle
> process, the container might want to "swap out" an idle servlet.
> 
> If this happens to your initialization servlet, or to the Struts
> ActionServlet, this is not likely to be a Good Thing.
> 
> A ServletContextListener, on the other hand, is *guaranteed* that the
> contextInitialized() and contextDestroyed() methods are only called at
> those two points in the application's lifetime, no matter what happens
> in between.
> 
> 
>>Dave
> 
> 
> Craig
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


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


Re: Application context bean

Posted by Craig McClanahan <cr...@gmail.com>.
On 8/9/05, Dave Newton <ne...@pingsite.com> wrote:
> Frank W. Zammetti wrote:
> 
> > P.S. - Someone else asked if a listener is better than a startup
> > servlet of Struts plug-in... IMO, it's better than a plug-in because
> > it's one less thing that is Struts-specific... Arguably it isn't any
> > better than a startup servlet I suppose.
> 
> I'm still using startup servlets out of habit and was just wondering if
> there was any compelling reason to switch over to session listeners--any
> differences under clustering or something? I dunno.
> 

On most existing containers it probably doesn't matter, but you are
relying on a non-required-by-the-specs behavior isse when you do this.
 (The exact same issue affects Struts plugins, which are managed in
the init() and destroy() methods of ActionServlet.)

The servlet container is *explicity* allowed (if it wants to) to
unload a servlet at any point during the application's lifetime, which
would include a call to destroy().  If a subsequent call for this
servlet comes in, it will get reinitialized via init().  Why might a
container want to do this?  Maybe it is seeing memory getting full,
and no calls to your servlet (quite likely if it is solely used for
initialization).  Just like an OS might want to swap out an idle
process, the container might want to "swap out" an idle servlet.

If this happens to your initialization servlet, or to the Struts
ActionServlet, this is not likely to be a Good Thing.

A ServletContextListener, on the other hand, is *guaranteed* that the
contextInitialized() and contextDestroyed() methods are only called at
those two points in the application's lifetime, no matter what happens
in between.

> Dave

Craig

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


Re: Application context bean

Posted by Dave Newton <ne...@pingsite.com>.
Frank W. Zammetti wrote:

> P.S. - Someone else asked if a listener is better than a startup 
> servlet of Struts plug-in... IMO, it's better than a plug-in because 
> it's one less thing that is Struts-specific... Arguably it isn't any 
> better than a startup servlet I suppose.

I'm still using startup servlets out of habit and was just wondering if 
there was any compelling reason to switch over to session listeners--any 
differences under clustering or something? I dunno.

Dave



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


Re: Application context bean

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
You can save some time by using the AppConfigContextListener in Java Web 
Parts:

http://javawebparts.sourceforge.net/javadocs/javawebparts/listener/AppConfigContextListener.html

In it's simplest configuration it should take all of a few minutes to 
get it done, there are more advanced modes of operation if you need them.

Frank

P.S. - Someone else asked if a listener is better than a startup servlet 
of Struts plug-in... IMO, it's better than a plug-in because it's one 
less thing that is Struts-specific... Arguably it isn't any better than 
a startup servlet I suppose.

Wendy Smoak wrote:
> From: "C.F. Scheidecker Antunes" <na...@antunes.eti.br>
> 
>> I would like to have a bean with a data structure that gets populated 
>> when the application starts and then this bean is available to the 
>> entire application.
> 
> 
> Use a ServletContextListener, which will be notified when the context 
> starts and stops.
> 
> http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html 
> 
> 
> public class MyContextListener implements ServletContextListener {
> 
>   public void contextInitialized( ServletContextEvent event ) {
>      ServletContext context = event.getServletContext();
>      // create your bean by reading from the database
>      context.setAttribute( "beanName" , bean );
>   }
> }
> 
> In web.xml:
> <listener>
>      <listener-class>edu.example.MyContextListener</listener-class>
> </listener>
> 
> Now you can use context.getAttribute( "beanName"); in Java code, or 
> expressions such as ${beanName.propName} to get access to it.
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


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


Re: Application context bean

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "C.F. Scheidecker Antunes" <na...@antunes.eti.br>

> I would like to have a bean with a data structure that gets populated when 
> the application starts and then this bean is available to the entire 
> application.

Use a ServletContextListener, which will be notified when the context starts 
and stops.

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html

public class MyContextListener implements ServletContextListener {

   public void contextInitialized( ServletContextEvent event ) {
      ServletContext context = event.getServletContext();
      // create your bean by reading from the database
      context.setAttribute( "beanName" , bean );
   }
}

In web.xml:
<listener>
      <listener-class>edu.example.MyContextListener</listener-class>
</listener>

Now you can use context.getAttribute( "beanName"); in Java code, or 
expressions such as ${beanName.propName} to get access to it.

-- 
Wendy Smoak 


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


Re: Application context bean

Posted by Michael Jouravlev <jm...@gmail.com>.
Search Struts mail archives for keywords: application, servlet,
context, servletContext, global, storing.

On 8/9/05, C.F. Scheidecker Antunes <na...@antunes.eti.br> wrote:
> Hello all,
> 
> I would like to have a bean with a data structure that gets populated
> when the application starts and then this bean is available to the
> entire application. The data structure will get populated via a data
> base. I need to call the database just once to populate the data
> structure. In case the database changes I would restart the server and
> have this bean populated again. Hence all the sessions can query the
> data structure within this bean. How can I achieve this?

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


Re: Application context bean

Posted by Joe Germuska <Jo...@Germuska.com>.
At 4:16 PM -0600 8/9/05, C.F. Scheidecker Antunes wrote:
>Hello all,
>
>I would like to have a bean with a data structure that gets 
>populated when the application starts and then this bean is 
>available to the entire application. The data structure will get 
>populated via a data base. I need to call the database just once to 
>populate the data structure. In case the database changes I would 
>restart the server and have this bean populated again. Hence all the 
>sessions can query the data structure within this bean. How can I 
>achieve this?

If you are using Servlet 2.3 or newer, you should probably use 
javax.servlet.ServletContextListener; If you are still using Servlet 
2.2, or if your initialization process needs access to a specific 
Struts ModuleConfig, then use org.apache.struts.action.PlugIn

Details of ServletContextListener should be in any decent Servlet 
Reference.   Details on PlugIn can be found here:

http://struts.apache.org/userGuide/building_controller.html#plugin_classes
http://struts.apache.org/userGuide/configuration.html#plugin_config
http://struts.apache.org/api/org/apache/struts/action/PlugIn.html

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

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