tbodt
tbodt

Reputation: 16987

How do I specify a server to get an EJB from?

In java EE, the way you get an EJB from a remote server is by looking it up in JNDI. The specification defines the JNDI name for a given bean type.

However, this seems to be only if you want to get a bean off your local computer. I want to get the bean off a remote server, like most users would. How do I specify the server URL? Do I pass a map to the InitialContext constructor?

Note: There is another question that is pretty much the same, but that has become out of date since the definition of portable JNDI names by the specification.

Upvotes: 2

Views: 3001

Answers (2)

VGR
VGR

Reputation: 44318

You do JNDI lookups of remote EJBs using exactly the same code as you would use when running server-side:

Context context = new InitialContext();  // No properties needed
MyEJB myEjbInstance = (MyEJB) context.lookup("ejb/MyEJB");

Or, of course, you can inject it:

@EJB
private MyEJB myEjbInstance;

To make the naming context work, you must run your application as a Java EE application client. An application client is exactly like a regular standalone Java program, with a standard main method; the only difference is that it needs to be run in a different manner. That manner is not specified in the Java EE Spec, so each application server has its own way of doing it.

GlassFish, for example, requires an application client to include some special jars in the classpath, and set a couple system properties. Specifically, you must include lib/gf-installer.jar and all the jars referenced by its manifest in your classpath, and you must set the org.omg.CORBA.ORBInitialHost and org.omg.CORBA.ORBInitialPort system properties.

Upvotes: 2

Gabriel Aramburu
Gabriel Aramburu

Reputation: 2981

I want to get the bean off a remote server

Yes, you need specify the IP/port where the remote server (JNDI service) is running/listening.

How do I specify the server URL?

You have to set the propertie: java.naming.provider.url and make it available to the InitialConetxt.

This can be done in different ways:

  1. to use a jndi.properties file
  2. to use system properties
  3. passing the value in a Hashtable when you create a new instance of InitialContext object.

The concrete value of this and others necessary properties to instantiate the InitialConetct are vendor dependen. An example for JBoss could be:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://yourServer:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Keep in mind that there is no way that you can get the EJB's stub from a remote server if you don´t indicate the url. By "Remote" I mean the client and the server are running in different JVM.

Upvotes: 2

Related Questions