简体   繁体   English

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

[英]How to set JNDI lookup timeout on glassfish

From the code below I am trying to access EJB on a remote machine. 从下面的代码中,我试图访问远程计算机上的EJB。

The code runs on Glassfish 4.1. 该代码在Glassfish 4.1上运行。 in a web application. 在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");

If the lookup is successful - everything works fine. 如果查找成功-一切正常。

However, the problem is that if the remote machine is not available then the code hangs on ctx.lookup("ejbname#EjbName"); 但是,问题在于,如果远程计算机不可用,则代码会挂在ctx.lookup("ejbname#EjbName"); . The time differs on cause, for over 1 min 20 seconds if the ip is non existent and over 10 minutes if the remote machine is behind firewall until it throws look up failure exception: 的时间不同,原因,超过1 min 20 seconds如果IP是不存在的,用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)

And here is another cause: 这是另一个原因:

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)

I don't want to wait so long to find out that look up failed. 我不想等那么久才发现查找失败。 All the timeouts I set above didn't help. 我在上面设置的所有超时都无济于事。 How can I set a lookup timeout or is there any other workaround? 如何设置查找超时或有其他解决方法?

Thanks. 谢谢。

I found a workaround, using java's concurrent.Future . 我找到了一种解决方法,使用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();
                }               

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM