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