如何在glassfish上设置JNDI查找超时

时间:2017-08-23 10:58:33

标签: java glassfish jndi lookup

从下面的代码我试图在远程机器上访问EJB。

代码在Glassfish 4.1上运行。在Web应用程序中。

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.enterprise.naming.SerialInitContextFactory");

props.setProperty("org.omg.CORBA.ORBInitialHost","X.X.X.X");
props.setProperty("org.omg.CORBA.ORBInitialPort","3700"); 
//timeouts
props.setProperty("sun.rmi.transport.connectionTimeout","5000");
props.setProperty("sun.rmi.transport.tcp.handshakeTimeout","5000");
props.setProperty("sun.rmi.transport.tcp.responseTimeout","5000");
props.setProperty("sun.rmi.transport.tcp.readTimeout","5000");


props.setProperty("com.sun.corba.ee.transport.ORBWaitForResponseTimeout","5000");
props.setProperty("com.sun.corba.ee.transport.ORBTCPConnectTimeouts", "100:500:100:500");
props.setProperty("com.sun.corba.ee.transport.ORBTCPTimeouts", "500:2000:50:1000");

System.setProperty("com.sun.corba.ee.transport.ORBWaitForResponseTimeout","5000");
System.setProperty("com.sun.corba.ee.transport.ORBTCPConnectTimeouts","100:500:100:500"); 
System.setProperty("com.sun.corba.ee.transport.ORBTCPTimeouts","500:2000:50:1000"); 

System.setProperty("sun.rmi.transport.connectionTimeout","5000");
System.setProperty("sun.rmi.transport.tcp.handshakeTimeout","5000");
System.setProperty("sun.rmi.transport.tcp.responseTimeout","5000");
System.setProperty("sun.rmi.transport.tcp.readTimeout","5000");
//timeout
InitialContext ctx = new InitialContext(props);
MyRemoteInterface bean = (MyRemoteInterface) 
ctx.lookup("ejbname#EjbName");

如果查找成功 - 一切正常。

然而,问题是如果远程计算机不可用,则代码将挂起ctx.lookup("ejbname#EjbName");。时间因原因而异,如果ip不存在则超过1 min 20 seconds,如果远程计算机位于防火墙之后超过10 minutes,则直到它抛出查找失败异常:

Severe:   org.omg.CORBA.COMM_FAILURE: FINE: 00410001: Connection failure: 
   ... 
   ...
Caused by: java.nio.channels.UnresolvedAddressException
    at sun.nio.ch.Net.checkAddress(Net.java:101)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622)
    at com.sun.corba.ee.impl.misc.ORBUtility.openSocketChannel(ORBUtility.java:110)
    at org.glassfish.enterprise.iiop.impl.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:329)

这是另一个原因:

Caused by: java.net.ConnectException: Connection timed out: connect
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:454)
    at sun.nio.ch.Net.connect(Net.java:446)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
    at com.sun.corba.ee.impl.misc.ORBUtility.openSocketChannel(ORBUtility.java:110)
    at org.glassfish.enterprise.iiop.impl.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:329)

我不想等待这么长时间才发现查找失败了。我在上面设置的所有超时都无济于事。如何设置查找超时或是否还有其他解决方法?

感谢。

1 个答案:

答案 0 :(得分:0)

我找到了一种解决方法,使用了java concurrent.Future

            MyRemoteInterface bean;
            java.util.concurrent.Future <Object> future; 
            java.util.concurrent.ExecutorService executorService = java.util.concurrent.Executors.newFixedThreadPool(1);
            future = executorService.submit(new java.util.concurrent.Callable<Object>() { 
               public Object call() throws Exception {
                   return ctx.lookup("ejbname#EjbName");
               }
             });

            try {
                  bean = (MyRemoteInterface) future.get(2L, java.util.concurrent.TimeUnit.SECONDS); 
                } catch (java.util.concurrent.ExecutionException ex) { 
                    ex.printStackTrace();
                    throw ex;
                }
                  catch (InterruptedException ee){
                      ee.printStackTrace();
                      throw ee;

                }
                   catch (TimeoutException te){
                       throw new Exception("Time Out!");
                }
                 finally {
                      future.cancel(true);
                      executorService.shutdown();
                }