Java 如何捕捉“无法将ViaPost发送到网址”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21987322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:51:37  来源:igfitidea点击:

How to catch "Unable to sendViaPost to url"?

javaweb-servicesexception-handlingaxis2axis

提问by Danijel

I am running two axis2 services which communicate with each other. On every service startup I get this error:

我正在运行两个相互通信的axis2服务。在每次服务启动时,我都会收到此错误:

2014-02-24 13:02:31,258 [INFO ]           HTTPSender  - Unable to sendViaPost to url[http://127.0.0.1:8081/axis2/services/MYSERVICE1.MYSERVICE1HttpSoap12Endpoint/]
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:140)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:621)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:193)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406)
at org.apache.axis2.description.OutInAxisOperationClient$NonBlockingInvocationWorker.run(OutInAxisOperation.java:446)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

Since this error is not important, I would like to catch it and to print some better error message instead of the whole stack trace. Where do I catch this error?

由于这个错误并不重要,我想捕获它并打印一些更好的错误消息而不是整个堆栈跟踪。我在哪里捕捉这个错误?

采纳答案by Jason Nichols

Looking at the stack trace, I don't think you can catch it. Catching it would require that you own code somewhere in the Thread where the exception is being thrown.

查看堆栈跟踪,我认为您无法抓住它。捕获它需要您在抛出异常的线程中的某处拥有代码。

Looking at the lowest stack in the trace shows this:

查看跟踪中最低的堆栈显示:

at java.lang.Thread.run(Thread.java:724)

To me this says that the exception is occurring in a thread most likely started by Axis. Because of this you can't catch it and show an error message.

对我来说,这表示异常发生在最有可能由 Axis 启动的线程中。因此,您无法捕获它并显示错误消息。

If this is expected behavior, the best you can do is to configure your logging framework not to show INFOs from Axis. Be aware that this may mean you'll also miss more useful error messages as well.

如果这是预期的行为,您能做的最好的事情就是配置您的日志框架不显示来自 Axis 的 INFO。请注意,这可能意味着您还会错过更多有用的错误消息。

All in all, I would focus on how to solve the "Unable to sendViaPost" from happening rather than suppressing the logging statement.

总而言之,我将专注于如何解决“无法发送ViaPost”的发生,而不是抑制日志语句。

To answer your comment question:As you can see from the stack trace, the exception is not caught by any client code but is bubbled up to Thread itself. This is the stopping point for an Exception and where it stops. If you were going to catch it you'd have to have code in its call stack (which you don't, since when the thread is created by Axis a new call stack is created for the new thread Axis starts).

回答您的评论问题:正如您从堆栈跟踪中看到的那样,异常没有被任何客户端代码捕获,而是向上冒泡到 Thread 本身。这是异常的停止点和停止点。如果您要捕获它,则必须在其调用堆栈中包含代码(您没有,因为当 Axis 创建线程时,会为新线程 Axis 启动创建新的调用堆栈)。

Read more here. The only difference in your case is that since the exception is not thrown on the main thread the program doesn't exit, but the thread where the exception occurs is terminated.

在这里阅读更多。您的情况唯一的区别是,由于主线程上没有抛出异常,程序不会退出,但发生异常的线程会终止。

To sum it up: You have no code in the call stack and therefore cannot catch the exception. The only other option is to turn of INFO statements for Axis.

总结一下:您在调用堆栈中没有代码,因此无法捕获异常。唯一的其他选择是关闭 Axis 的 INFO 语句。

回答by RGG

If I am understanding the question properly you're attempting to catch something that is not the exception that is being thrown.

如果我正确理解了这个问题,那么您正在尝试捕获不是抛出的异常的东西。

This:

这个:

HTTPSender - Unable to sendViaPost to url[http://127.0.0.1:8081/axis2/services/MYSERVICE1.MYSERVICE1HttpSoap12Endpoint/]

HTTPSender - 无法将ViaPost 发送到 url[http://127.0.0.1:8081/axis2/services/MYSERVICE1.MYSERVICE1HttpSoap12Endpoint/]

is what is being attempted. When it failed it's throwing a ConnectException.

是正在尝试的。当它失败时,它会抛出一个 ConnectException。

Which you can simply catch with

你可以简单地抓住

try{
    //Code that Makes the Connection
}
catch (ConnectException e) 
{
    e.printStackTrace();//Or What ever your message may be
} 

Without seeing some code it's impossible to give a definitive answer. But this likely will solve the problem.

如果没有看到一些代码,就不可能给出明确的答案。但这可能会解决问题。

One Caveat, if you do catch a ConnectException to suppress it, you could suppress when there actually is a problem that would also throw a ConnectException.

一个警告,如果您确实捕获了 ConnectException 来抑制它,那么您可以在实际存在也会引发 ConnectException 的问题时进行抑制。

If this is happening when you are starting up the server you might want to check why this is happening before trying to suppress it.

如果在您启动服务器时发生这种情况,您可能需要在尝试抑制它之前检查为什么会发生这种情况。

If it's refusing the connection that you are attempting you might want to ensure where it is connecting to has an available socket to connect to.

如果它拒绝您正在尝试的连接,您可能需要确保它连接到的位置有一个可用的套接字可以连接。

回答by Shishir Kumar

2014-02-24 13:02:31,258 [INFO]HTTPSender - Unable to sendViaPost to url[http://127.0.0.1:8081/axis2/services/MYSERVICE1.MYSERVICE1HttpSoap12Endpoint/]

2014-02-24 13:02:31,258 [INFO]HTTPSender - 无法发送 ViaPost 到 url[http://127.0.0.1:8081/axis2/services/MYSERVICE1.MYSERVICE1HttpSoap12Endpoint/]

Well, if you look closely, the message which you are trying to catch isn't an ERRORat all. It's an INFOlog generated from HTTPSender. Only thing which you should catch in this entire stacktraceis java.net.ConnectExceptionand check for message Connection refused.

好吧,如果你仔细观察,你试图捕捉的信息根本不是一个ERROR。它是INFOHTTPSender. 在这整个stacktrace过程中,您唯一应该注意的是java.net.ConnectException并检查 message Connection refused

You can make it easier for your clients though and provide a message, by wrapping the java.net.ConnectExceptionwith message Connection refusedor throwing a custom exception with the original exception as the cause.

但是,您可以通过包装java.net.ConnectExceptionwith 消息Connection refused或以原始异常为原因抛出自定义异常,从而使您的客户更容易并提供消息。

UPDATE

更新

java.net.ConnectExceptionis an elementary exception in network transactions. Generally standard libraries do not catch them unless there is something specificto be done.

java.net.ConnectException是网络交易中的一个基本例外。通常标准库不会捕获它们,除非有特定的事情要做。

In this case, if you are unable to catch hold of java.net.ConnectException, then you can look out to catch AxisFaultthrown by org.apache.axis2.description.OutInAxisOperationClient.send.

在这种情况下,如果您无法抓住java.net.ConnectException,那么您可以注意抓住AxisFault抛出的org.apache.axis2.description.OutInAxisOperationClient.send

Below snippet may be useful for you.

下面的代码片段可能对您有用。

try {
    ...
} catch (RemoteException ex) {
    if(ex instanceof AxisFault){
        logger.error("Axis Fault error: " + ((AxisFault)ex).getFaultString());
        throw new CustomExcpetion(" Custom Message ");
    }
}

Also note that AxisFaultis a subclass of java.rmi.RemoteExceptionand this will not get caught when you use java.lang.Exceptionin a catchstatement.

另请注意,它AxisFault是 of 的子类,java.rmi.RemoteException当您java.lang.Exceptioncatch语句中使用时,它不会被捕获。

Shishir

石狮