可以使用RMI或SOAP-RESTful端点访问EJB。我想从另一个计算机/ IP地址访问远程EJB,例如在独立应用程序中。我可以使用Web服务端点访问EJB,然后我不知道与RMI联系。我该如何实现这个想法。我正在使用Glassfish 3.1。
答案 0 :(得分:0)
查看How do I access a Remote EJB component from a stand-alone java client?文档。代码片段依赖于EJB 2(Home
接口),您应该直接查找@Remote
接口。当然,它们必须在客户端提供。
基于:Creating EJB3 Sessions Beans using Netbeans 6.1 and Glassfish:
jndi.properties
java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs = com.sun.enterprise.naming
java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
org.omg.CORBA.ORBInitialHost = localhost
org.omg.CORBA.ORBInitialPort = 3700
Main.java
package testclient;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.InitialContext;
import stateless.TestEJBRemote;
public class Main {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.load(new FileInputStream("jndi.properties"));
InitialContext ctx = new InitialContext(props);
TestEJBRemote testEJB = (TestEJBRemote) ctx.lookup("stateless.TestEJBRemote");
System.out.println(testEJB.getMessage());
} catch (NamingException nex) {
nex.printStackTrace();
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
}