You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ajay Patil <ap...@vertex.co.in> on 2003/07/17 05:32:47 UTC

Question on making Action classes thread-safe

Hello,

I found out that it is possible to make the Action class thread-safe 
by over-riding RequestProcessor (processActionCreate method) so that 
it creates a new Action instance for each request.

I would like to know the implications and possible problems ?
If anyone has tried this, please share your experiences.

Thanks,
Ajay


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


RE: Question on making Action classes thread-safe

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 17 Jul 2003, Mike Jasnowski wrote:

> Date: Thu, 17 Jul 2003 12:37:39 -0400
> From: Mike Jasnowski <mj...@bea.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: RE: Question on making Action classes thread-safe
>
> Do you think though that this would be a nice convenience or would just
> encourage bad
> practices too much to be worth it? Also assuming that the overhead was
> acceptable (define acceptable)

If we were going to do that, I think it would be better to combine an
ActionForm and an Action into a single concept, so that the Action would
have direct access to the form values as instance variables.  In essence,
this is how WebWorks does it on this particular issue, and it's a pretty
elegant design.  (It's also a pattern I expect to see in a lot of
JavaServer Faces based code -- it's really easy to set the valueRefs for
your form fields and the actionRefs for your actions to point into the
same back-end bean, so that the action methods can access the form field
data directly.)

The risk, of course, is that developers will become tempted to intertwine
business and presentation logic to the detriment of long term
maintainability ... but I think good design patterns can continue to
illustrate the right way to do this (you'd still delegate to separate
classes for the real business logic, for example, just like you should do
from your Action classes today).

When Struts was designed, the overhead of instantiating objects and the
subsequent GC was a much more important issue.  I suspect that it's not a
big deal on modern JVMs -- and we're already paying the per-request object
create overhead for form beans (if you use request scope), which doesn't
seem to be fatally flawed from a performance perspective.

The challenge for Struts, of course, is how to do things like this and
maintain backwards compatibility so that existing apps do not need to be
rewritten the next time the user switches revs.

Craig

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


RE: Question on making Action classes thread-safe

Posted by Mike Jasnowski <mj...@bea.com>.
Do you think though that this would be a nice convenience or would just
encourage bad
practices too much to be worth it? Also assuming that the overhead was
acceptable (define acceptable)

-----Original Message-----
From: Craig R. McClanahan [mailto:craigmcc@apache.org]
Sent: Thursday, July 17, 2003 12:12 PM
To: Struts Users Mailing List
Subject: Re: Question on making Action classes thread-safe




On Thu, 17 Jul 2003, Erik Price wrote:

> Date: Thu, 17 Jul 2003 08:39:00 -0400
> From: Erik Price <ep...@ptc.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Re: Question on making Action classes thread-safe
>
>
>
> Ajay Patil wrote:
> > Hello,
> >
> > I found out that it is possible to make the Action class thread-safe
> > by over-riding RequestProcessor (processActionCreate method) so that
> > it creates a new Action instance for each request.
> >
> > I would like to know the implications and possible problems ?
> > If anyone has tried this, please share your experiences.
>
> Is this similar to the "thread-safety" of SingleThreadedServlet?

Sort of.  Usually, the reason people might want this is so that they can
use instance variables in their Actions to store per-request state
information.  To me, this is normally a clue that you're trying to do too
much stuff in your Action in the first place -- you should really be
delegating business logic to business tier beans (and you can create
individual ones there if you need to).

And, just like with SingleThreadModel, you still have to worry about
simultaneous requests to the same session, whether or not there is a
single Action instance per request.

>  It
> sounds like it.  I would imagine that with a separate instance for each
> request, more memory would be consumed?
>

Yes.  And more GC because a new instance is created for every request, and
then thrown away.  Whether this actually has significant impact on the
performance of your app, though, is not something that you can give a
generalized answer to -- it depends on what the critical hotspots are in
your particular application.

>
>
> Erik
>

Craig

---------------------------------------------------------------------
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: Question on making Action classes thread-safe

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Thu, 17 Jul 2003, Erik Price wrote:

> Date: Thu, 17 Jul 2003 08:39:00 -0400
> From: Erik Price <ep...@ptc.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Re: Question on making Action classes thread-safe
>
>
>
> Ajay Patil wrote:
> > Hello,
> >
> > I found out that it is possible to make the Action class thread-safe
> > by over-riding RequestProcessor (processActionCreate method) so that
> > it creates a new Action instance for each request.
> >
> > I would like to know the implications and possible problems ?
> > If anyone has tried this, please share your experiences.
>
> Is this similar to the "thread-safety" of SingleThreadedServlet?

Sort of.  Usually, the reason people might want this is so that they can
use instance variables in their Actions to store per-request state
information.  To me, this is normally a clue that you're trying to do too
much stuff in your Action in the first place -- you should really be
delegating business logic to business tier beans (and you can create
individual ones there if you need to).

And, just like with SingleThreadModel, you still have to worry about
simultaneous requests to the same session, whether or not there is a
single Action instance per request.

>  It
> sounds like it.  I would imagine that with a separate instance for each
> request, more memory would be consumed?
>

Yes.  And more GC because a new instance is created for every request, and
then thrown away.  Whether this actually has significant impact on the
performance of your app, though, is not something that you can give a
generalized answer to -- it depends on what the critical hotspots are in
your particular application.

>
>
> Erik
>

Craig

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


Re: Question on making Action classes thread-safe

Posted by Erik Price <ep...@ptc.com>.

Andrew Hill wrote:
> Yep more memory and more time for the garbage collector - but in fact its
> probably insignificant - your already creating an actionform (if its request
> scoped) , a request object, and all sorts of other things as by products of
> processing the request. This is just one more object.
> 
> JVMs now are much better at instantiation and garbage collection than they
> were back when struts (and the servlet api) were originally designed.
> Certainly to a point where if struts were being written from scratch now,
> caching the actions and making them thread-safe would be a decision of
> dubious merit.

SingleThreadedModel is deprecated in Servlet 2.4 IIRC.




Erik


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


RE: Question on making Action classes thread-safe

Posted by Andrew Hill <an...@gridnode.com>.
Yep more memory and more time for the garbage collector - but in fact its
probably insignificant - your already creating an actionform (if its request
scoped) , a request object, and all sorts of other things as by products of
processing the request. This is just one more object.

JVMs now are much better at instantiation and garbage collection than they
were back when struts (and the servlet api) were originally designed.
Certainly to a point where if struts were being written from scratch now,
caching the actions and making them thread-safe would be a decision of
dubious merit.

Im not sure however to what extent the number of superclasses and methods
affects the cost of instantiation and memory use of an object. Id be
interested to hear comments by those who do.


-----Original Message-----
From: Erik Price [mailto:eprice@ptc.com]
Sent: Thursday, 17 July 2003 20:39
To: Struts Users Mailing List
Subject: Re: Question on making Action classes thread-safe




Ajay Patil wrote:
> Hello,
>
> I found out that it is possible to make the Action class thread-safe
> by over-riding RequestProcessor (processActionCreate method) so that
> it creates a new Action instance for each request.
>
> I would like to know the implications and possible problems ?
> If anyone has tried this, please share your experiences.

Is this similar to the "thread-safety" of SingleThreadedServlet?  It
sounds like it.  I would imagine that with a separate instance for each
request, more memory would be consumed?



Erik


---------------------------------------------------------------------
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: Question on making Action classes thread-safe

Posted by Erik Price <ep...@ptc.com>.

Ajay Patil wrote:
> Hello,
> 
> I found out that it is possible to make the Action class thread-safe 
> by over-riding RequestProcessor (processActionCreate method) so that 
> it creates a new Action instance for each request.
> 
> I would like to know the implications and possible problems ?
> If anyone has tried this, please share your experiences.

Is this similar to the "thread-safety" of SingleThreadedServlet?  It 
sounds like it.  I would imagine that with a separate instance for each 
request, more memory would be consumed?



Erik


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