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

↘锁芯ラ 提交于 2020-01-01 17:26:13

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/18937514/how-do-i-specify-a-server-to-get-an-ejb-from

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