【问题标题】:Catching error caused by InitialContext.lookup由 InitialContext.lookup 引起的捕获错误
【发布时间】:2012-10-31 09:56:08
【问题描述】:

我正在开发一个命令行客户端 (Java SE6),它现在需要与 Glassfish 2.1 服务器通信。 建立这个连接的代码是

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

我现在在没有运行服务器的情况下测试它,而客户端(从 Eclipse 4.2 运行时)只是用炸弹

31.10.2012 10:40:09 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl 警告:“IOP00410201:(COMM_FAILURE)连接失败:socketType:IIOP_CLEAR_TEXT;主机名:localhost;端口:3700” org.omg.CORBA.COMM_FAILURE:vmcid:SUN 次要代码:201 已完成:否 在 com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2783) 在 com.sun.corba.ee.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2804) 在 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:261) 在 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:274) 在 com.sun.corba.ee.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:130) 在 com.sun.corba.ee.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:192) 在 com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:184) 在 com.sun.corba.ee.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:328) 在 org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:112) 在 org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69) 在 com.sun.enterprise.naming.SerialContext.narrowProvider(SerialContext.java:134) 在 com.sun.enterprise.naming.SerialContext.getCachedProvider(SerialContext.java:259) 在 com.sun.enterprise.naming.SerialContext.getRemoteProvider(SerialContext.java:204) 在 com.sun.enterprise.naming.SerialContext.getProvider(SerialContext.java:159) 在 com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:409) 在 javax.naming.InitialContext.lookup(InitialContext.java:392) 在 com.werkii.latex.generator.Generator.main(Generator.java:344) 引起:java.lang.RuntimeException:java.net.ConnectException:连接被拒绝:连接 在 com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:347) 在 com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.(SocketOrChannelConnectionImpl.java:244) ... 14 更多 引起:java.net.ConnectException:连接被拒绝:连接 在 sun.nio.ch.Net.connect(本机方法) 在 sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:532) 在 com.sun.corba.ee.impl.orbutil.ORBUtility.openSocketChannel(ORBUtility.java:105) 在 com.sun.enterprise.iiop.IIOPSSLSocketFactory.createSocket(IIOPSSLSocketFactory.java:332) ... 15 更多

没关系。现在(虽然我仍在开发中)它会爆炸,但它会反复执行此操作,并且永远不会达到 catch 子句(即使我正在捕捉 Throwable) - 消息没有打印出来。

那么如何在我的程序中处理lookup 期间的连接错误?

【问题讨论】:

    标签: java error-handling glassfish ejb jndi


    【解决方案1】:

    由于您正在与 JVM 之外的 EJB 通信,因此您可能希望为初始上下文提供额外的详细信息。对于 Glassfish,请尝试

    Properties props = new Properties();
    props.setProperty(“java.naming.factory.initial”, “com.sun.enterprise.naming.SerialInitContextFactory”);
    props.setProperty(“java.naming.factory.url.pkgs”, “com.sun.enterprise.naming”);
    props.setProperty(“java.naming.factory.state”, “com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl”);
    props.setProperty(“org.omg.CORBA.ORBInitialHost”, “127.0.0.1″);
    props.setProperty(“org.omg.CORBA.ORBInitialPort”, “3700″);
    InitialContext ctx = new InitialContext (props);
    

    【讨论】:

    • 我知道我需要这个 - 但是当出现问题时我如何捕捉错误?
    • 我猜你已经用你的 catch 块捕获了错误。关于问题的快速建议 - 您希望避免像 catch (Throwable t) 中那样的一揽子异常并捕获特定异常并相应地处理它们。
    • 知道 Throwable 风格不好。我试过Exception,但没有被抓住。而且我很确定即使现在也没有到达catch
    • 有趣。我看到跟踪没有错误,只有异常。所以,catch (Exception) 应该处理它。您能否尝试注释掉t.printStackTrace(System.err); 并检查是否打印了跟踪。另外,Could not call server: 是否会打印在您的客户日志中?
    • 这(将属性传递给 InitialConext 的构造函数)对于 Glassfish 实际上是错误:请参阅 FAQ
    猜你喜欢
    • 2013-10-27
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2011-04-18
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多