[英]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. 在Java EE中,从远程服务器获取EJB的方法是在JNDI中查找它。 The specification defines the JNDI name for a given bean type. 规范定义了给定bean类型的JNDI名称。
However, this seems to be only if you want to get a bean off your local computer. 但是,这似乎仅是要从本地计算机上获取Bean时才使用。 I want to get the bean off a remote server, like most users would. 我想像大多数用户一样将bean从远程服务器上删除。 How do I specify the server URL? 如何指定服务器URL? Do I pass a map to the InitialContext
constructor? 是否将映射传递给InitialContext
构造函数?
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. 注意: 另一个问题几乎相同,但是自从规范定义可移植JNDI名称以来,这个问题已经过时。
You do JNDI lookups of remote EJBs using exactly the same code as you would use when running server-side: 您可以使用与运行服务器端时完全相同的代码来进行远程EJB的JNDI查找:
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 . 要使命名上下文起作用,您必须将应用程序作为Java EE 应用程序客户端运行 。 An application client is exactly like a regular standalone Java program, with a standard main
method; 应用程序客户端与常规的独立Java程序完全一样,具有标准的main
方法; 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. Java EE Spec中没有指定这种方式,因此每个应用程序服务器都有其自己的方式。
GlassFish, for example, requires an application client to include some special jars in the classpath , and set a couple system properties . 例如,GlassFish要求应用程序客户端在类路径中包括一些特殊的jar ,并设置几个系统属性 。 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. 具体来说,您必须在目录路径中包含lib/gf-installer.jar
及其清单所引用的所有jar,并且必须设置org.omg.CORBA.ORBInitialHost
和org.omg.CORBA.ORBInitialPort
系统属性。
I want to get the bean off a remote server 我想从远程服务器上获取bean
Yes, you need specify the IP/port where the remote server (JNDI service) is running/listening. 是的,您需要指定运行/监听远程服务器(JNDI服务)的IP /端口。
How do I specify the server URL? 如何指定服务器URL?
You have to set the propertie: java.naming.provider.url and make it available to the InitialConetxt. 您必须设置属性: java.naming.provider.url并将其提供给InitialConetxt使用。
This can be done in different ways : 这可以通过不同的方式完成:
The concrete value of this and others necessary properties to instantiate the InitialConetct are vendor dependen. 实例化InitialConetct的此属性和其他必要属性的具体值取决于供应商。 An example for JBoss could be: JBoss的示例可能是:
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. 请记住,如果不指定URL,则无法从远程服务器获取EJB存根。 By "Remote" I mean the client and the server are running in different JVM. “远程”是指客户端和服务器在不同的JVM中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.