1
votes

Why only JNDI is used to access remote EJB (different JVM, differente host)? why not use the @ EJB annotation?

In all EJB books mention that you can access remote EJB using @EJB annotation.

Example: http://docs.oracle.com/javaee/7/tutorial/doc/ejb-intro004.htm

32.4.4 Remote Clients

A remote client of an enterprise bean has the following traits.

  • It can run on a different machine and a different JVM from the enterprise bean it accesses. (It is not required to run on a different JVM.)

  • It can be a web component, an application client, or another enterprise bean

  • It can be a web component, an application client, or another enterprise bean.

  • To a remote client, the location of the enterprise bean is transparent.

  • The enterprise bean must implement a business interface. That is, remote clients may not access an enterprise bean through a no-interface view.

To create an enterprise bean that allows remote access, you must either:

  • Decorate the business interface of the enterprise bean with the @Remote annotation:

    @Remote public interface InterfaceName { ... }

  • Or decorate the bean class with @Remote, specifying the business interface or interfaces:

    @Remote(InterfaceName.class) public class BeanName implements InterfaceName { ... }

Client access to an enterprise bean that implements a remote business interface is accomplished through either dependency injection or JNDI lookup.

  • To obtain a reference to the remote business interface of an enterprise bean through dependency injection, use the javax.ejb.EJB annotation and specify the enterprise bean's remote business interface name:

    @EJB Example example;

  • To obtain a reference to a remote business interface of an enterprise bean through JNDI lookup, use the javax.naming.InitialContext interface's lookup method:

    ExampleRemote example = (ExampleRemote) InitialContext.lookup("java:global/myApp/ExampleRemote");

The text above is incorrect? I have not seen any code that uses Dependency Injection (@EJB) to access a remote EJB. It is not possible?

Several post say it is not possible to use the @ EJB annotation for calls to remote ejb:

PD: Excuse me. My English is basic.

1
As your second link states, "@EJB annotation can only be used if the applications are deployed in the same sever instance.".user207421
It is contradicted by the following: "A remote client of an enterprise bean has the following traits: It can run on a different machine and a different JVM from the enterprise bean it accesses."DavidJesus03
also: "Client access to an enterprise bean that implements a remote business interface is accomplished through either dependency injection (@EJB) or JNDI lookup."DavidJesus03

1 Answers

0
votes

I believe the main reason is that injection of remote (different JVM) EJB instances through

@EJB(lookup = "jndi_name")

is only supported by some Application Servers and only with specific configuration.

i.e. JBoss 7+ supports it only when you define the remote-outbound-connection in the standalone file (and add a jboss-ejb-jar.xml in the META-INF folder of deployed packages containing the reference to that connection).

Furthermore:

  • @EJB can only work in CDI managed beans

  • Using this approach forces you to define connection properties once per connection, while with the programmatic lookup you may vary them on every request (and you are free to handle contexts and connections manually, if needed).