Struts2中的拦截器线程是否为UNsafe? [英] Are Interceptors in Struts2 Thread UNsafe?

查看:185
本文介绍了Struts2中的拦截器线程是否为UNsafe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Struts2 Action类是线程安全的,因为操作放在Value Stack中。值栈依次是Action Context的一部分。由于Action Context是线程本地的,因此存储在Action Context(包括值栈)中的值对于每个线程都是唯一的。因此,Actions是线程安全的。

I understand that Struts2 Action classes are thread-safe, because the actions are put in the Value Stack. The Value Stack in turn is one part of the Action Context. Since the Action Context is thread-local, the values stored in the Action Context (including the value stack) are unique per thread. So, Actions are thread-safe.

但是考虑一下拦截器:它们非常有用,它们为程序员完成所有那些繁琐的小工作......比如验证,获取参数值等等。但需要考虑的是:拦截器可以在多个请求之间共享。那么这会让拦截器线程不安全吗?

But consider the interceptors: They are really useful, they do all those tedious little jobs for the programmer... like validations, fetching the param values, etc. But the thing to consider is that: Interceptors can be shared between multiple requests. So does that make interceptors thread unsafe?

考虑到这个问题,我试图在网上浏览一些与此问题相关的好文章。我发现了一篇非常好的文章,他们已经清楚地提到了一个示例拦截器如何不是线程安全的。

With this question in mind, I tried to surf the net for some good articles related to this problem. And I found a very good article, where they have clearly mentioned with an example How interceptors are NOT thread safe.

网页是:http://www.bullraider.com/java/struts2/tutorials/interceptors-and-thread-safety

我从这篇文章中得知,拦截器线程不安全的主要原因是拦截器只创建了一次。即每个拦截器只有一个对象。因此,当线程之间共享Interceptor的相同实例时,实例字段是不安全的。

What I got to know from this article is, the major reason behind Interceptors being thread un-safe is that the interceptors are created only once. i.e. each interceptor has only one object. So, the instance fields are not safe, when the same instance of an Interceptor is shared between threads.

在文章的最后,它是提到有些情况,甚至拦截器都是线程安全的。但他们没有提到任何此类案件。我上网寻找答案......但是徒劳无功:(

At the end of the article it's mentioned that there are cases, where even the interceptors are thread safe. But they didn't mentioned any such cases. I surfed the net to find the answer... but in vain :(

任何人都可以告诉我或提供一个链接,我可以在那里找到答案如何使拦截器线程安全(或拦截器是线程安全的情况)?

推荐答案

不使用实例字段或其他共享状态的任何拦截器都是线程安全的:

Any Interceptor that does not use instance fields or other shared state is thread-safe:

例如,查看解析HTTP请求参数和cookie的所有内置拦截器,执行日志记录,访问检查,异常处理:它们不使用实例字段用于可变状态(*)但只是操作他们作为参数获得的 ActionInvocation 实例。

For examples, look at all the built-in interceptors 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 ActionInvocation instance they get as parameters.

(*)有些实例具有配置参数的实例字段Struts启动时(在单个线程中)设置,如 ExceptionMappingInterceptor ,或者安全实例字段,如 Logger /xwork-core/2.3.1.1/com/opensymphony/xwork2/interceptor/LoggingInterceptor.java?av=h#60\"rel =noreferrer> LoggingInterceptor

(*) some do have instance fields for configuration parameters which are set when Struts starts up (in a single thread), like ExceptionMappingInterceptor, or thread-safe instance fields like the Logger in LoggingInterceptor.

如果您打算编写自己的拦截器,只需使用 ActionInvocation 您传入的参数以及 intercept()方法中的局部变量。避免诱惑你的拦截方法同步或将东西放入 synchronized {} 块 - 这将创建一个使用Struts针对拦截器的单实例方法的瓶颈。

If you plan on writing your own Interceptor, work just with the ActionInvocation parameter you get passed in and with local variables in your intercept() method. Avoid the temptation to make your intercept method synchronized or put things into a synchronized{} block -- this will create a bottleneck with Struts' single-instance approach to Interceptors.

回答评论中的问题:


(1)如何为每个线程创建一个动作实例不会影响性能?还是这样?

(1) How come creating an instance of action for every thread doesn't affect the performance?or does it?

使用现代JVM,创建对象的成本可以忽略不计。如果你通过避免昂贵的初始化来保持你的行动轻量化,那么对性能应该没有明显的影响。通过不在操作中创建数据库连接但使用连接池。

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.


(2)你建议不要使用默认的拦截器堆栈,并始终使用自定义拦截器堆栈(其中删除所有使用实例变量的未使用的拦截器),以便它是线程安全的?

(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?

我不认为使用Struts 2发布和配置的任何默认拦截器都不是线程安全的;即使他们使用实例字段(因为它们仅用于配置或本身是线程安全的,如 Logger )。

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 Logger).

根据我的个人经验,如果你有充分的理由(内置拦截器的线程安全性不是一个),你应该只触摸/更改拦截器堆栈。如果你改变堆栈,许多事情会以意想不到的方式表现/破坏 - 运行其中一个内置堆栈,如default或paramPrepareParam,从长远来看可以节省很多挫败感。添加自己的自定义拦截器通常比从现有堆栈中删除/重新排列拦截器更具破坏性。

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.

这篇关于Struts2中的拦截器线程是否为UNsafe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆