Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Any Interceptor that <em>does not use</em> instance fields or other shared state is thread-safe:</p> <p>For examples, look at <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.apache.struts.xwork/xwork-core/2.3.1.1/com/opensymphony/xwork2/interceptor/AbstractInterceptor.java?av=h#AbstractInterceptor" rel="noreferrer">all the built-in interceptors</a> that parse HTTP request parameters and cookies, do logging, access checks, exception handling: They do not use instance fields for mutable state(*) but just operate on the <code>ActionInvocation</code> instance they get as parameters.</p> <p>(*) some do have instance fields for configuration parameters which are set when Struts starts up (in a single thread), like <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.apache.struts.xwork/xwork-core/2.3.1.1/com/opensymphony/xwork2/interceptor/ExceptionMappingInterceptor.java?av=h#ExceptionMappingInterceptor" rel="noreferrer"><code>ExceptionMappingInterceptor</code></a>, or thread-safe instance fields like the <code>Logger</code> in <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.apache.struts.xwork/xwork-core/2.3.1.1/com/opensymphony/xwork2/interceptor/LoggingInterceptor.java?av=h#60" rel="noreferrer"><code>LoggingInterceptor</code></a>.</p> <p>If you plan on writing your own <code>Interceptor</code>, work just with the <code>ActionInvocation</code> parameter you get passed in and with local variables in your <code>intercept()</code> method. Avoid the temptation to make your intercept method <code>synchronized</code> or put things into a <code>synchronized{}</code> block -- this will create a bottleneck with Struts' single-instance approach to Interceptors.</p> <p>To answer the questions from the comments:</p> <blockquote> <p>(1) How come creating an instance of action for every thread doesn't affect the performance?or does it?</p> </blockquote> <p>With modern JVMs, the cost of creating an object is negligible. There should be no noticeable effect on performance if you keep your actions light-weight by avoiding expensive initializtion, e.g. by not creating database connections inside an action but using a connection pool.</p> <blockquote> <p>(2) Do u recommend NEVER to use the default interceptor stack, and always use custom interceptor stack (where all the unused interceptors which use instance variables are removed) so that it will be thread safe?</p> </blockquote> <p>I don't think any of the default interceptors that are shipped and configured with Struts 2 are not thread-safe; even if they use instance fields (because they're either used for configuration only or itself thread-safe like <code>Logger</code>).</p> <p>From my personal experience, you should only ever touch/change the interceptor stack if you have a good reason (thread-safety of the built-in interceptors isn't one). A lot of things behave/break in unexpected ways if you change the stacks -- running one of the built-in stacks like "default" or "paramPrepareParam" saves a lot of frustration in the long run. Adding your own custom interceptors is usually less disruptive than removing/rearranging interceptors from an existing stack.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload