Is it possible to use @WebService, @Stateless and @Singleton altogether in one EJB 3 bean?

帅比萌擦擦* 提交于 2019-12-12 16:08:47

问题


I'm using EJB 3 and JBoss AS 6.0.0.Final. I have a stateless session bean with the annotations @Stateless and @WebService. When I add the annotation @Singleton, the deployment is in error showing the message:

...name=ServiceBean, service=ejb3 is already installed

What can I do to avoid the deployment error?


回答1:


You can use @WebService and @Stateless or @WebService and @Singleton in the same bean which makes perfectly sense if you want to expose a POJO as both a web service and EJB.

Don't see much sense in using @Stateless and @Singleton in the same bean. When you use @Singleton, you are creating an EJB with all the EJB capabilities (transaction management, security, etc) exactly as with @Stateless. The only difference is how the container manages the EJB lifecycle:

  • @Stateless: the EJB instance is created immediately after the first request and when the request ends, the EJB is pooled and ready for reuse if another request comes in. However, if all the pooled instances are being used in the moment another request comes in for the same bean, the container creates a new instance of the same to serve that new request.
  • @Singleton: the EJB instance is created after the first request (by default - see @Startup to override this behavior) comes in and that will be the only instance created by the container. If another request wants to use the same EJB, the container will never create a new instance of it - the instance previously created will be used. It is like a @Stateless EJB with a pool size of 1 :) Aspects like concurrency are important when using these, but this is probably out of the scope of this post.


来源:https://stackoverflow.com/questions/7849738/is-it-possible-to-use-webservice-stateless-and-singleton-altogether-in-one-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!