In spring integration, how do I catching different exceptions?

后端 未结 2 1273
自闭症患者
自闭症患者 2021-01-26 10:49

In spring integration I have a simple tcp client pipe: a gateway, a tcp outbound gateway a service activator plus an error channel. In the tcp-connection-factory there is a simp

相关标签:
2条回答
  • 2021-01-26 11:19

    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.

    0 讨论(0)
  • 2021-01-26 11:26

    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.

    0 讨论(0)
提交回复
热议问题