问题
Please see the code below:
Context ctx = null;
ctx=new InitialContext();
TestEJBRemote t = (TestEJBRemote) ctx.lookup("java:global/EJBTest/EJBTest-ejb/TestEJB");
System.out.println(t.getName("Ian"));
The output is what I expect i.e. Hello Ian.
The code above assumes that the client is installed on the same computer as the Glassfish instance. How do I get the same result from a remote application client. I have tried this:
Context ic = new InitialContext();
TestEJBRemote t = (TestEJBRemote) ic.lookup("corbaname:computer:4848#/a/b/TestEJB");
System.out.println(t.getName("Ian"));
which produces an error. I assume that the port is the port that Glassfish is installed on.
回答1:
For remote clients connecting to GlassFish and Payara, I normally use the following:
Properties props = new Properties();
props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
MyBeanRemote bean = (MyBeanRemote) ctx.lookup("com.example.MyBean");
I would imagine, from your example, that your original lookup would work in this scenario:
TestEJBRemote t = (TestEJBRemote) ctx.lookup("java:global/EJBTest/EJBTest-ejb/TestEJB");
If you have multiple remote endpoints, you can load balance between them with the following:
Hashtable env = new Hashtable();
env.put("com.sun.appserv.iiop.endpoints","host1:port1,host2:port2,...");
InitialContext ctx = new InitialConext(env);
Ref: https://docs.oracle.com/cd/E26576_01/doc.312/e24930/java-clients.htm#GSDVG00075
来源:https://stackoverflow.com/questions/39768754/obtaining-initial-context-from-remote-client