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

查看:26
本文介绍了将 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).

我不知道它们是否是从像无状态 bean 这样的 bean 池中分配的 - 由应用程序服务器分配.我试图找出是否将 @Stateless 注释与已经用 @WebService 注释注释的 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.

如果您使用一个简单的端点(即带有 Apache CXF 或 Spring webservices 的 JAX-WS),您将拥有一个用于所有线程/请求的服务实例(如您所说的 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 时,您具有更大的灵活性:如果您使用无状态 bean,您将拥有一个实例池来管理所有请求(如果您的 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. 经典 Web 服务和无状态会话 Bean Web 服务之间的唯一区别是,第一个 Web 服务将具有单个实例,而第二个将使用实例池.如果您的 poolSize=1,您会得到相同的效果,但理论上,池可以在想要"时销毁您的实例.

希望能帮到你,

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

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