Announcement Announcement Module
Collapse
No announcement yet.
How to handle Jaxb exception in transformer? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to handle Jaxb exception in transformer?

    I have configured:

    Code:
    <int-xml:unmarshalling-transformer unmarshaller="oxm" input-channel="payloadChannel" 
    		output-channel="pojos"/>
    
    <bean id="oxm" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    		<property name="contextPath" value="com.example"/>
    </bean>
    What is the proper way to handle exception, for example when the payload is not a XML document?

    Thanks,
    Khoa

  • #2
    Put an error-channel on whatever the entry point to your flow is (gateway, inbound-channel-adapter, etc).

    The error message will have a payload that has the failedMessage and cause as properties.

    Comment


    • #3
      Originally posted by Gary Russell View Post
      Put an error-channel on whatever the entry point to your flow is (gateway, inbound-channel-adapter, etc).

      The error message will have a payload that has the failedMessage and cause as properties.
      Thanks Gary for your response. However my entry point:
      Code:
      <int:inbound-channel-adapter channel="fromAws" ref="sqs" method="getMessages">
      	<int:poller fixed-rate="1000" max-messages-per-poll="10"/>
      </int:inbound-channel-adapter>
      doesn't seem to take error-channel as a parameter.

      Please advice.

      Thanks,
      Khoa

      Comment


      • #4
        For polled message sources, the error-channel goes on the poller.

        Comment


        • #5
          Thanks Gary for the tip. It caught the exception, but I am still facing a deeper issue: there's a pub-sub channel mid-stream, and the Jaxb transformer above is one of the subscribers. The 2nd subscriber won't get the incoming message if the transformer throws an exception. Am I missing something?

          Thanks,
          Khoa

          Comment


          • #6
            I really depends on what you want to do with the error.

            You can set ignore-failures="true" on the pub-sub channel, and the error will just be logged and we'll still deliver it to the other subscriber(s).

            If you want to handle the exception in some way (and do more than log it), you'll have to split the flow by inserting a <gateway/> between the pub-sub-channel and the oxm transformer.

            Code:
            <publish-subscribe-channel id="psc" />
            
            <service-activator input-channel="psc" output-channel="nullChannel" ref="errorTrappingGateway" />
            
            <gateway id="errorTrappingGateway" default-request-channel="toOxmTtransformer" error-channel="oxmErrorChannel" />
            This is similar to inserting a try{} catch {} within java code; just be sure to return something (anything) from error handler. Or, define an interface with a method that returns void and then the errorTrappingGateway won't be expecting a reply. In that case, you can leave off the nullChannel...

            Code:
            <publish-subscribe-channel id="psc" />
            
            <service-activator input-channel="psc" ref="errorTrappingGateway" method="oneWay" />
            
            <gateway id="errorTrappingGateway" service-interface="foo.MyGw"
                default-request-channel="toOxmTtransformer" error-channel="oxmErrorChannel" />
            Code:
            public interface MyGw {
                void oneWay(Message<?> message);
            }

            Comment


            • #7
              Thanks! Since I only need to log the exception for now, "ignore-failures" option works well. I will record other solutions in my notes for future opportunities.

              Thanks again for your quick responses and a wonderful product.

              Khoa

              Comment

              Working...
              X