Interceptor (Thread Safety)

2009-06-02 Thread ZeckoN

If two copies of the same action is run at the same time parallelly (by ajax,
iframe, etc.) in the same session, possibly with different parameters, do
these actions call the same instance of an interceptor's intercept() method?
If this is the case, how to avoid problems that can arise?

For example,

there is an intercept method like

public String intercept(ActionInvocation actionInvocation) throws Exception
{
.Do some pre work
String result = actionInvocation.invoke();
.Do some after work
return result;  
}

when the first copy of action is run, it does the pre work, then invoke() is
called, at this time action2 is run so it runs pre work, calls it's invoke()
then while doing after work, first action returns from invoke() thus
changing the value of result variable. So the result is now first action's
result and both actions return to the result from the first action, right?

is defining the intercept method as synchronized a solution?

thanks for the help.
-- 
View this message in context: 
http://www.nabble.com/Interceptor-%28Thread-Safety%29-tp23836386p23836386.html
Sent from the Struts - User mailing list archive at Nabble.com.


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



Re: Interceptor (Thread Safety)

2009-06-02 Thread Dave Newton

ZeckoN wrote:

public String intercept(ActionInvocation actionInvocation) throws Exception
{
.Do some pre work
String result = actionInvocation.invoke();
.Do some after work
return result;  
}


Local variables aren't shared across method invocations.

Dave

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



Re: Interceptor (Thread Safety)

2009-06-02 Thread Wes Wannemacher
On Tue, Jun 2, 2009 at 12:45 PM, ZeckoN zec...@gmail.com wrote:

 If two copies of the same action is run at the same time parallelly (by ajax,
 iframe, etc.) in the same session, possibly with different parameters, do
 these actions call the same instance of an interceptor's intercept() method?
 If this is the case, how to avoid problems that can arise?

 For example,

 there is an intercept method like

 public String intercept(ActionInvocation actionInvocation) throws Exception
 {
 .Do some pre work
 String result = actionInvocation.invoke();
 .Do some after work
 return result;
 }

 when the first copy of action is run, it does the pre work, then invoke() is
 called, at this time action2 is run so it runs pre work, calls it's invoke()
 then while doing after work, first action returns from invoke() thus
 changing the value of result variable. So the result is now first action's
 result and both actions return to the result from the first action, right?

 is defining the intercept method as synchronized a solution?

 thanks for the help.


String result = actionInvocation.invoke() ;

This is thread-safe, another thread will invoke the intercept method,
but the result string is scoped.

I think you might be misunderstanding how threading works. If your
pre-work and post-work only deal with variables locally scoped within
the method, then I don't think there is a problem. Here is an example
of a problem -

public class MyInterceptor extends whatever {

 public String classScopedResultString;

 public String intercept(ActionInvocation actionInvocation) throws Exception
 {
 classScopedResultString = gettingReadyToInvoke;
 classScopedResultString = actionInvocation.invoke();
 classScopedResultString = justGotDoneInvoking;
 return classScopedResultString;
 }
}

-Wes

-- 
Wes Wannemacher
Author - Struts 2 In Practice
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher

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