简体   繁体   中英

How to set JNDI lookup timeout on glassfish

From the code below I am trying to access EJB on a remote machine.

The code runs on Glassfish 4.1. in a web application.

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"); . 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:

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 .

            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();
                }               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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