Catching error caused by InitialContext.lookup

Posted by Martin Schröder on Stack Overflow See other posts from Stack Overflow or by Martin Schröder
Published on 2012-10-31T09:56:08Z Indexed on 2012/10/31 11:00 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

I'm developing a command line client (Java SE6) that now needs to talk to a Glassfish 2.1 server. The code for setting up this connection is

try {
    final InitialContext context = new InitialContext();
    final String ejbName = GeneratorCancelledRemote.class.getName();
    generatorCancelled = (GeneratorCancelledRemote) context.lookup(ejbName);
}
catch (Throwable t) {
    System.err.println("--> Could not call server:");
    t.printStackTrace(System.err);
    runWithOutEJB = true;
}

I'm now testing it without a running server and the client (when run from Eclipse 4.2) just bombs with

31.10.2012 10:40:09 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl 
WARNUNG: "IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 3700"
org.omg.CORBA.COMM_FAILURE:   vmcid: SUN  minor code: 201  completed: No
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2783)
    at com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2804)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274)
    at com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130)
    at com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:184)
    at com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:328)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:134)
    at com.sun.enterprise.naming.SerialContext.getCachedProvider(SerialContext.java:259)
    at com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:204)
    at com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:159)
    at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:409)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.werkii.latex.generator.Generator.main(Generator.java:344)
Caused by: java.lang.RuntimeException: java.net.ConnectException: Connection refused: connect
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347)
    at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244)
    ... 14 more
Caused by: java.net.ConnectException: Connection refused: connect
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:532)
    at com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105)
    at com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332)
    ... 15 more

It's o.k. for now (while I'm still in development) that it bombs, but it does this repeatedly and the catch clause is never reached (even though I'm catching Throwable) - the message is not printed.

So how can I handle connection errors during lookup in my program?

© Stack Overflow or respective owner

Related posts about java

Related posts about error-handling