Obtaining initial context from remote client

一笑奈何 提交于 2020-01-17 05:21:07

问题


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

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