2017-08-23 58 views
0

從下面的代碼我試圖訪問遠程計算機上的EJB。如何在glassfish上設置JNDI查找超時

該代碼在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");。的時間不同,原因,超過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) 

這裏有另外一個原因:

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) 

我不想等那麼久才發現查找失敗。我上面設置的所有超時都沒有幫助。如何設置查找超時或是否有其他解決方法?

謝謝。

+0

您的意思是** JNDI **查找超時?如果是這樣,你的標題和標籤不正確。 – EJP

+0

@EJP是的JNDI - 謝謝 - 修正它。 – Plirkee

回答

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