In spring integration, how do I catching different exceptions?

主宰稳场 提交于 2019-12-02 10:52:27

Actually <tcp-connection-event-inbound-channel-adapter> send to the channel a Message with TcpConnectionExceptionEvent (in your case) as payload.

Therefore your subscriber (your TcpErrorHandler) can accepts TcpConnectionExceptionEvent as a method argument.

In that method you can do further logic, e.g. extract the original Exception from that IntegrationEvent.

There are several places in the IP module when TcpConnectionSupport.publishConnectionExceptionEvent is used.

If you say that you don't catch time out exception, it will great if you share the logs on the matter. I wonder which place we don't try...catch on the SocketTimeoutException...

UPDATE

<int-ip:tcp-connection-event-inbound-channel-adapter channel="events" 
    event-types="org.springframework.integration.ip.tcp.connection.TcpConnectionExceptionEvent"/>

<service-activator input-channel="events" ref="tcpErrorHandler"/>

public class TcpErrorHandler {

   public void onException(TcpConnectionExceptionEvent event) {
       System.out.println("Exception!!! ");
       event.getCause();
       ....
   } 

}

This should work.

UPDATE2

According to your code:

try {
   super.send(message);
}
catch (Exception e) {
    System.out.println("catched_send_exception");
}

Don't you think that it is bad to suffocate an Exception there?

From other side: would you mind switching on DEBUG logging level for the org.springframework.integration category and share here the logs, when you are sure that your tcpErrorHandler should be invoked?

From other side try <int-ip:tcp-connection-event-inbound-channel-adapter> without event-types at all. I mean let's see, if it handle any IpIntegrationEvent.

You can define an error channel, that you provide to your inbound adapter. Here is an example:

    <int:channel id="error-channel"></int:channel>
    <int-ws:inbound-gateway id="gateway" error-channel="error-channel"
    request-channel="in"  marshaller="marshaller" unmarshaller="marshaller"
    reply-channel="out" />

Now all exception that are thrown downstream will be catched by this error-channel. You can then define a service activator with this error channel as an input:

 <int:service-activator  input-channel="error-channel"
        ref="exceptionHandler" method="handleError" output-channel="outError"></int:service-activator>

And this activator refers to a bean that defines error handling logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!