Announcement Announcement Module
No announcement yet.
How does error channel work in TCP adapters? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How does error channel work in TCP adapters?


    I am trying to understand how the wiring works.

    my code is as under.
    <int:gateway id="downloadServiceGateway"
    	<int:channel downloadErrorChannel />
    	<int:transformer ref="downloadErrorToTransformExceptionToXML"
    input-channel="downloadErrorChannel" output-channel="errorTCPChannel" />
    How will Spring know that it has to now put the message to errorChannel? If the exception is thrown in the bean defined in inputchannel, is it then that the error message is sent to error channel?

    In my scenario, when a certain action fails, I need to capture the error message, add certain additional text and send an xml over TCP. I have an XML format defined. I also need to send another XML to let the receiver know that the system is alive every few seconds. How can I achieve this?

  • #2
    If downloadInputChannel is a direct channel, and all downstream channels from there are direct channels then, yes, any downstream exception will be caught by the gateway and an ErrorMessage will be put on the error-channel.

    The payload of the ErrorMessage is a MessagingException which has two properties:

    * failedMessage - the message that failed
    * cause - the original exception

    To send keepalives use something like

    <int:inbound-channel-adapter id="heartbeat" expression="'<ok/>'" channel="toReceiver" >
      <int:poller fixed-delay="5000" />
    Notice the single quotes around the literal SpEL expression.

    You can, of course, reference a bean and method to get the content, instead of using an expression.


    • #3
      Thanks Gary. That was quick and a great help. Appreciate it.

      So, if I have a schema defined for the error XML and the heartbeat XML, can I use the bean to manipulate the message object to add nodes as in the schema that has been defined?

      For eg,

      <int:transformer ref="downloadErrorToTransformExceptionToXML"
      input-channel="downloadErrorChannel" output-channel="errorTCPChannel" />
      can the downloadErrorToTransfornExceptionToXML bean have a method that gets the ErrorMessage object, updates it with the nodes that are defined in the schema and then it is put on the error channel?


      • #4
        You can do whatever you want in a transformer. It doesn't "manipulate" the message; messages are immutable.

        A transformer gets an input message (preferably just its payload) and returns some other message.

        Something like this...

        public String errorToXML(MessagingException e) {
            Object payload = e.getFailedMessage().getPayload();
            Exception actualException = e.getCause();
            return someString;
        The new message out of the transformer will have someString as its payload.


        • #5
          Thanks Gary. Appreciate your help.


          • #6
            So, is this logging through error channel is asynchronous? I mean when the downloadInputChannel has certain operations to perform, and when errors are encountered, are those errors put on error channel and the error channel processing them happen asynchronously? Can my input channel continue with its own operations while error channel is processing the errors?


            • #7
              No; it's synchronous; the exception handling is on the caller's thread by default.

              To hand off the error handling to another thread, just make the error channel a queue channel and add a poller.