2

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.

Community
  • 1
  • 1
tbodt
  • 16,609
  • 6
  • 58
  • 83

2 Answers2

2

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.

tbodt
  • 16,609
  • 6
  • 58
  • 83
VGR
  • 40,506
  • 4
  • 48
  • 63
  • How big is the `gf-client` jar file? I wouldn't want something too big to distribute. – tbodt Sep 22 '13 at 00:03
  • Only about 20K, but its manifest pulls in about 55 megabytes of .jars. I believe GlassFish allows an application client packaged in an .ear to be made available via Java WebStart, which would allow lazy loading of those .jars, but I've never actually tried using that. – VGR Sep 22 '13 at 13:57
  • @VGR for remote JNDI service discovering, your code will work only if you specify the ip and port of such service, I mean that you can use the same code but still is necessary to provide some extra configuration to the InitialContext . I think that it's necessary to clarify this point. – Gabriel Aramburu Sep 22 '13 at 16:05
  • The honor and riches will have to wait until I can try out each solution. – tbodt Sep 22 '13 at 19:13
  • @Gabriel, the Java EE contract for an application client container specifically states that it must make a no-argument InitialContext fully functional. The host and port are specified outside of the code. In the case of GlassFish, it is done with the two system properties I mentioned in the last sentence of my answer. – VGR Sep 22 '13 at 20:34
  • you can just install `appclient` on the client machine...yes, kinda sucks, but easier than external dependencies? – Thufir Mar 09 '15 at 00:54
2

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.

Gabriel Aramburu
  • 2,951
  • 2
  • 16
  • 20