公开Web服务作为一个无状态会话bean的任何线程安全的好处? [英] Any thread safety benefits of exposing a web service as a stateless session bean?

查看:119
本文介绍了公开Web服务作为一个无状态会话bean的任何线程安全的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有公开的Web服务作为一个无状态会话bean的任何线程安全相关的好处是什么?结果
(纠正我,如果我错了),但我认为Web服务不是线程安全,如servlet是由服务器(而不是每个请求一个实例)创建的Web服务类的一个实例。

Are there are any thread safety related benefits of exposing a web service as a stateless session bean?
(Correct me if i am wrong) but I think web services are not thread safe and like Servlets only one instance of a web service class is created by the server (not one instance per request).

我不知道什么他们是否是从象无状态豆豆池分配是 - 由应用服务器。
我试图找到,如果我使用@Stateless Generated注释与已经与@WebService annotation-意志,迫使应用服务器启动为每个传入请求池中分配他们注释的Web服务。这样,我知道肯定每个传入请求我将在一个单独的实例创建?

What I don't know whether they are assigned from a pool of beans like stateless beans are - by the app server. I am trying to find if I use @Stateless annoation with a web service that's already annotated with @WebService annotation- will that force the app server to start assigning them from a pool for each incoming request. That way I know for sure I will one separate instance created for each incoming request?

推荐答案

简介

由Web服务端点使用实例的数量取决于您所使用的框架。

The number of instances used by the web service endpoint depends on the framework you are using.

如果你用一个简单的端点(即JAX-WS与Apache CXF或WebServices的春天),您将拥有所有线程/请求一个服务实例(如你所说,Servlet的)。因此,通过定义这种服务的,目的是要无状态的。但是如果你需要一些状态添加到服务,你可以这样做,但要由开发商来使服务线程安全的。

If you use a simple endpoint (i.e. JAX-WS with Apache CXF or Spring webservices) you will have a single service instance for all the threads/requests (as you said, Servlets). So, by definition this kind of services are meant to be stateless. But if you need to add some state to the service, you can do it, but is up to the developer to make the service thread safe.

当你使用EJB,你有更多的灵活性:如果你使用无状态豆你将有实例池来管理所有的请求(如果您的poolSize = 1你的Apache CXF同样的行为)。同样,你可以添加一些状态,无状态Bean,但要线程安全的,更是难上加难,因为你有实例池来管理。但是,如果你需要一个状态,你可以使用一个状态bean有一个框架,让你的生活就线程安全更加容易。

When you use EJB you have more flexibility: if you use stateless beans you will have a pool of instances to manage all the request (if your poolSize=1 you get the same behaviour of Apache CXF). Again, you can add some state to a stateless bean, but make it thread safe is even harder because you have a pool of instances to manage. But if you need an state you can use an stateful bean to have a framework that make your life easier regarding thread safety.

有关服务状态的一些提示

如果你不保持一个状态,你的服务您的Web服务是线程安全的。换句话说,一个无状态的服务,如果线程定义安全的。

If you do not keep an state in your service your web service is thread safe. In other words, an stateless service if thread safe by definition.

如果你需要一些状态,应该是份额由所有线程/请求您可以添加一些状态到一个无状态的服务(JAX-WS或poolSize = 1无状态会话bean),但你需要使它线程安全的,加入sycn块(请不要同步您 @WebMethod )。 重要:在理论(和实践),你应该是状态绝不添加到一个无状态会话bean,因为池可以摧毁/创建实例时,要

If you need some state that should be share by ALL the thread/request you can add some state to an stateless service (JAX-WS or a Stateless session bean with poolSize=1) but you need to make it thread safe, adding sycn blocks (please, do not sync your @WebMethod). IMPORTANT: In theory (and in practice), you should NEVER add an state to a stateless session bean, because the pool can destroy/create instances when it "wants to".

如果您需要保留,将只由当前线程使用状态/请求您可以使用的ThreadLocal 变量添加一些状态到一个无状态的服务,或者更容易使用有状态会话Bean。

If you need to keep an state that will be use only by the current thread/request you can add some state to a stateless service using ThreadLocal variables, or more easily using stateful session beans.

现在,终于,回答你的问题


  1. 无状态会话bean是线程安全的,当且仅当你让他们线程安全的:他们不应该有一个状态: - )

  2. 经典网络服务和无状态会话bean web服务,如果该第一个将有一个单独的实例,而第二个将使用实例的池之间的唯一不同。如果你的poolSize = 1,你得到相同的效果,但在理论上,池可以摧毁你的实例时,要。

希望能有帮助,

这篇关于公开Web服务作为一个无状态会话bean的任何线程安全的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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