@EJB injection vs lookup - performance issue

耗尽温柔 提交于 2019-12-19 19:49:31

问题


I have a question related with possible performance issue while using @EJB annotation. Imagine following scenario

public class MyBean1 implements MyBean1Remote{
 @EJB
 private MyBean2Remote myBean2;
 @EJB
 private MyBean2Remote myBean3;
 ...
 @EJB
 private MyBean20Remote myBean20;
}  

There is a bean with many dependencies to other beans. According to EJB spec if I would like to inject MyBean1Remote to some other bean, container would have to take all required dependencies from its pool inject it into MyBean1Remote and then inject reference to MyBean1Remote stub.

so in following scenario container needs to reserved 20 ejbs (myBean1 and its 19 dependencies)

public class MyAnotherBean implement MyAnotherRemote{
  @EJB
  private MyBean1Remote myBean1
}

Let say that in most cases we will use only single dependency per each business method of myBean1. As a result each time we want to inject that bean we force container to reserves many unnecessery EJBs. Lets also assume that we are operating on remote beans so probably container would also need to perform some load balancing algorithm prior injecting dependent beans.

Questions:

  1. Wouldn't that cause unnecessary resource reservation and more over performance issue while operating in cluster environment?

  2. Maybe good old ServiceLocator could be better solution because with this approach we would ask for specific EJB when we really need it ?


回答1:


The container does not inject an instance of the EJB; it injects an instance of a lightweight container-generated proxy object that implements the desired interface.

public class MyBean1 implements MyBean1Remote {
   ...
}

public class MyAnotherBean implement MyAnotherRemote {
   @EJB
   private MyBean1Remote myBean1;
}

In your example, MyAnotherBean.myBean1 will be injected with a proxy object that implements the MyBean1Remote interface.

Assuming a stateless session bean (since you mention pooling), the container does not allocate an actual EJB instance from the method-ready pool until a method is called on the proxy, and the instance is returned to the pool before the proxy method call returns.




回答2:


In most cases and especially when using stateless session beans, your bean instances will be pooled. One of the rationales behind pooling is that dependency injection lookups might be relatively expensive, so the bean is pooled with (stubs for) all its dependencies already injected.

So every time you call a method on MyAnotherBean, This bean with its 20 transitive dependencies isn't created with all those dependencies resolved on the fly. Instead, a fully instantiated instance is selected from the pool and the method call is directed to that.

Also note that unless you are doing JNDI federation you normally can't easily inject remote EJBs.



来源:https://stackoverflow.com/questions/4753449/ejb-injection-vs-lookup-performance-issue

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