Java 捕获InitialContext.lookup引起的错误
我正在开发一个命令行客户机(JavaSE6),现在需要与GlassFish2.1服务器通信。 设置此连接的代码为Java 捕获InitialContext.lookup引起的错误,java,error-handling,glassfish,ejb,jndi,Java,Error Handling,Glassfish,Ejb,Jndi,我正在开发一个命令行客户机(JavaSE6),现在需要与GlassFish2.1服务器通信。 设置此连接的代码为 try { final InitialContext context = new InitialContext(); final String ejbName = GeneratorCancelledRemote.class.getName(); generatorCancelled = (GeneratorCancelledRemote) context.l
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;
}
我现在在没有运行服务器的情况下测试它,而客户机(从Eclipse4.2运行时)只是使用
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
31.10.2012 10:40:09 com.sun.corba.ee.impl.transport.socketorchannelconnection impl
警告:“IOP0041201:(通信故障)连接故障:socketType:IIOP\U CLEAR\u TEXT;主机名:localhost;端口:3700”
org.omg.CORBA.COMM_故障: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.socketorchannelcontactinfo.createConnection(socketorchannelcontactinfo.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._是_a(ObjectImpl.java:112)
位于org.omg.CosNaming.NamingContextHelper.slow(NamingContextHelper.java:69)
位于com.sun.enterprise.naming.SerialContext.窄带提供者(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.iiopslsocketfactory.createSocket上(iiopssslsocketfactory.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.iiopslsocketfactory.createSocket上(iiopssslsocketfactory.java:332)
... 还有15个
现在还可以(我还在开发中),它会轰炸,但它会反复轰炸,而且永远不会到达catch
子句(即使我正在捕获可丢弃的)-消息不会打印
那么,在我的程序中查找期间如何处理连接错误呢?由于您正在与JVM外部的EJB进行对话,您可能需要提供初始上下文的其他详细信息。玻璃鱼,试试看
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
是一种糟糕的风格。我尝试了异常
,但没有被捕获。而且我很确定,即使是现在也没有达到捕获量。我看到跟踪没有错误,只有例外。因此,catch(Exception)
应该处理它。你能试着注释掉t.printStackTrace(System.err)
并检查跟踪是否正在打印。另外,是否无法调用服务器:
正在客户端日志中打印?这(将属性传递给InitialConext
的构造函数)对于Glassfish来说实际上是错误的:请参阅。