Announcement Announcement Module
No announcement yet.
Error handling through spring integration Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error handling through spring integration

    Hello All,
    Well i am having a problem while playing with string, long story short is that i wanted to build a sample code to know how can i handle Errors that might happen during the program execution and by errors i means Exceptions that are being thrown from my service activators .

    Program is as follows i have a client side and server side, client send a string message through a gateway to fanout exchange, message goes to the server side through rabbit mq inbound channel , then to a chain which hold service activator which simply check the length of the String message if it is less than 5 charachters it send message back to the client else it will through a custome made exception which is StringTooLongException , and this is where i am stuck, i don't know how to catch the exception through the error channels or error handlers i have tried most of the exaples out there none are working , after using Error handlers with the channel before the service activator i get a new Error "Failure occured in AMQP listener while attempting to convert and dispatch Message", i am really lost so any one can help me out with this, Attached is the project folder for your review .

    I am sorry if i over looked any thing that might help me and surely i am not looking for someone to feed me information with a spoon so any links will be appreciated too Thank you all and happy friday.

  • #2
    What do you want to do with the exception?

    Your messaging gateway returns void and you are sending the message to a one-way outbound adapter, so there's nothing waiting for a reply.

    If you have a request/reply scenario, use outbound/inbound gateways and, on the inbound gateway, add a service to examine the reply and, if it's an exception, throw the exception there, and the original thread will catch it while waiting for the reply.





    You don't need this

    			<int:error-channel ref="ServerErrorChannel" />
    on your server - the gateway already inserted the errorChannel header.


    • #3
      Thanks a lot Gary for your reply i really appreciate it,
      For your first question what do i want to do with the exception ?, i want to through the exception from the service activator and then it would be caught "Didn't what element can caught it exactly ", and then send the Exception.message to the client side.

      The way the message will be sent is through sending the Exception.Message to a channel->out bound channel adapter -> fan out exchange and in the client side there is an inbound channel adapter listening on the queue linked to that exchange , so on the client side it would be inbound channel adapter ->chain [service activator] which will sysout the error message send from the server side.

      i will test using this way, but is there is any sample code close enough to what i want to do so that i can go back to it since i notice that sample codes for exception handling is not that many ?

      Again Thank you very much for your reply it really helped me understand few points that i didn't know.


      • #4
        Here you go...

        Client side...

        <int-stream:stdin-channel-adapter id="consoleIn" channel="toRabbit" >
            <int:poller fixed-delay="1000" max-messages-per-poll="1" />
        <int:channel id="toRabbit" />
        <int-amqp:outbound-channel-adapter channel="toRabbit"
        		amqp-template="amqpTemplate" exchange-name="" 
        <!-- Errors coming back -->
        <int-amqp:inbound-channel-adapter channel="serverErrors" 
                                              connection-factory="connectionFactory" />
        <int:service-activator input-channel="serverErrors">
        		println "Exception on server:" + payload

        <int-amqp:inbound-channel-adapter channel="fromRabbit" 
                                              connection-factory="connectionFactory" />
        <int:channel id="fromRabbit" />
        <int:service-activator input-channel="fromRabbit" output-channel="consoleOut">
        		throw new RuntimeException("Some Exception Occurred")
        <int-stream:stdout-channel-adapter id="consoleOut" append-newline="true"/>
        <!-- Error Handling (errorChannel defined on inbound adapter) -->
        <int:service-activator input-channel="errorChannel" output-channel="errorToClient">
        		"eek:" + payload.cause.message
        <int:channel id="errorToClient" />
        <int-amqp:outbound-channel-adapter channel="errorToClient"
        		amqp-template="amqpTemplate" exchange-name="" 
        And the result...

        Exception on server:eek:Some Exception Occurred


        • #5

          Thank you very much for your reply Garry it really helped me alot, i discoved that the problem not just in how i used the integration elements but most the source of the problem actually was me using the rabbit mq channels <int-amqp:channel id=""></int-amqp:channel> not the normal integration channel.

          -in the amqp channels there is an element called Error handler i guess it is there to handle the error instead of bubbling the exception up to the inbound gateway, problem with it however is that it uses an ErrorHandler interface which the return value of the function that is to be implemented is void so i wasn't able to return the error message .

          -i changed the channel type to normal integration channel and it worked like magic i am attaching the new ode just incase if any one is interested to know or see a smiple example for error handling.

          -i am testing a new thing also to make the error handler throw the error back , i know not professional but it seams fun .

          Thank you Garry i know more now .