支柱动作单身 [英] struts action singleton

查看:196
本文介绍了支柱动作单身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么struts动作类是单例?



其实我得到的点是它是多线程的。但是当数千个请求打到同一个动作的时候,我们把同步的防止线程问题放在同一时间,那么它不会让bcoz线程处于等待状态,而且需要时间来处理。



有没有办法从动作类中删除单例?



了解更多信息请访问: http://rameshsengani.in

解决方案

您正在问为什么Action类是一个单例,但我认为你也有一些问题了解线程安全性,所以我将尝试解释两者。



首先,Struts Action类没有实现为单例,框架只是使用一个实例。但是由于只有一个实例用于处理所有传入的请求,因此必须注意不要在Action类中执行某些不是线程安全的操作。但是事情是:默认情况下,Struts Action类不是线程安全的



线程安全意味着可以在多线程环境中安全地使用一段代码或对象。一个Action类可以安全地用在一个多线程的环境中,并且可以让它在一千个线程中同时使用,没有任何问题,这是,如果你正确地实现。 p>

Action class JavaDoc


操作必须以线程安全的方式进行编程,因为控制器将为多个同时发送的请求共享相同的实例。这意味着您应该设计以下项目:




  • 实例和静态变量不得用于存储与特定请求的状态。他们可以用于跨同样操作的请求共享全局资源。


  • 访问其他资源(JavaBeans,会话变量等)必须同步这些资源需要保护。 (通常,资源类别应设计为在必要时提供自己的保护。



您可以通过派生并创建自己的Struts Action来执行此操作,当您执行此操作时,您必须注意遵守上述规则,这就意味着这样的一个例子:

  public class MyAction extends Action {
private Object someInstanceField;
public ActionForward execute(...){
//修改someInstanceField这里没有正确的同步 - >> BAD
}
}

你不需要同步Action类,除非你像上面的代码那样做了一些错误的事情,事情是执行到你的动作的入口点是 execute 方法



此方法接收所有需要的参数,您可以在$ code> execute 方法没有问题,因为每个线程都有自己的方法调用的执行堆栈,而不是堆中的数据(例如 someInstanceField )在所有线程之间共享。



修改 someInstanceField 时没有正确的同步,所有线程都将按照他们的要求进行操作。 / p>

所以是的,Struts 1 Action类不是线程安全的,但这是在你无法安全地存储状态(iemake他们statefulf)或如果你这样做必须正确同步。



但是,如果保持您的Action类实现无状态,则可以,不需要同步,线程不要等待彼此。 p>


为什么struts动作类是单例?


按设计。 JavaDoc再次解释:


一个Action是传入HTTP请求的内容和应该执行的相应业务逻辑之间的适配器处理此请求


请求参数与Web层相关联,您不想将该类型的数据发送到业务逻辑类,因为这将在两层之间创建一个紧耦合的
,这将使得不可能轻松地重用您的业务层。



因为转换Web对象成为模型对象(而我并不意味着ActionForm bean在这里)应该是Action类的主要目的,他们不需要维护任何状态(并且不会),也没有理由有更多的实例这些家伙,都做同样的事情。只需一个。



如果您希望您可以通过将信息持久化到数据库来安全维护模型,或者您可以使用http会话。在Action类中维护状态是错误的,因为这将介绍如上所述的syncronization的需要。


有没有办法删除单例从动作类?


我猜你可以扩展Struts并覆盖 RequestProcessor.processActionCreate 为每个请求创建一个Action
,但这意味着在Struts之上添加另一个层来更改其正常行为。我已经看到像这样的东西在一些应用程序中坏了,所以我不会推荐它。



我的建议是保持你的Action类无状态,为它创建的实例。



如果你的应用程序是新的,你绝对需要statefull动作,我想你可以去Struts 2(他们改变了设计,而Action实例现在是每个请求一个)。
但是Struts 2与Struts 1 ,所以如果你的应用程序是旧的,可​​能很难迁移到Struts 2。



希望这样可以清除。


Why is the struts action class is singleton ?

Actually I am getting point that it is multithreaded. but at time when thousand of request hitting same action, and we put synchronized for preventing threading issue, then it not give good performance bcoz thread going in wait state and it take time to proced.

Is that any way to remove singleton from action class ?

for more info Please visit : http://rameshsengani.in

解决方案

You are asking about why the Action class is a singleton but I think you also have some issues understanding thread safety so I will try to explain both.

First of all, a Struts Action class is not implemented as a singleton, the framework just uses one instance of it. But because only one instance is used to process all incoming requests, care must be taken not to do something with in the Action class that is not thread safe. But the thing is: by default, Struts Action classes are not thread safe.

Thread safety means that a piece of code or object can be safely used in a multi-threaded environment. An Action class can be safely used in a multi-threaded environment and you can have it used in a thousand threads at the same time with no issues... that is if you implement it correctly.

From the Action class JavaDoc:

Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:

  • Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.

  • Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.

You use the Struts Action by deriving it and creating your own. When you do that, you have to take care to respect the rules above. That means something like this is a NO-NO:

public class MyAction extends Action {
   private Object someInstanceField;
   public ActionForward execute(...) {
      // modify someInstanceField here without proper synchronization ->> BAD
   }
}

You don't need to synchronize Action classes unless you did something wrong with them like in the code above. The thing is that the entry point of execution into your action is the execute method.

This method receives all it needs as parameters. You can have a thousand threads executed at the same time in the execute method with no issues because each thread has its own execution stack for the method call but not for data that is in the heap (like someInstanceField) which is shared between all threads.

Without proper synchronization when modifying someInstanceField all threads will do as they please with it.

So yes, Struts 1 Action classes are not thread safe but this is in the sense that you can't safely store state in them (i.e.make them statefulf) or if you do it must be properly synchronized.

But if you keep your Action class implementation stateless you are OK, no synchronization needed and threads don't wait for one another.

Why is the struts action class is singleton ?

It's by design. Again the JavaDoc explains it:

An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request

The request parameters are tied to the web tier and you don't want to send that type of data into your business logic classes because that will create a tight coupling between the two layers which will then make it impossible to easily reuse your business layer.

Because transforming web objects into model objects (and I don't mean the ActionForm beans here) should be the main purpose of Action classes, they don't need to maintain any state (and shoudn't) and also, there is no reason to have more instances of these guys, all doing the same thing. Just one will do.

If you want you can safely maintain state in your model by persisting info to a database for example, or you can maintain web state by using the http session. It is wrong to maintain state in the Action classes as this introduces the need for syncronisation as explained above.

Is there a way to remove singleton from action class?

I guess you could extend Struts and override the default behavior of RequestProcessor.processActionCreate to create yourself an Action per request but that means adding another layer on top of Struts to change its "normal" behavior. I've already seen stuff like this go bad in a few applications so I would not recommend it.

My suggestion is to keep your Action classes stateless and go for the single instance that is created for it.

If your app is new and you absolutely need statefull Actions, I guess you could go for Struts 2 (they changed the design there and the Action instances are now one per request). But Struts 2 is very different from Struts 1 so if you app is old it might be difficult to migrate to Struts 2.

Hope this makes it clear now.

这篇关于支柱动作单身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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