6 Replies Latest reply on Mar 13, 2014 4:26 PM by Keith Babo

    Handle Exception in MessageComposer

    John DeStefano Apprentice

      Hi,

       

      I have a MessageComposer class that can and does throw an exception. How does one access the exception? I can see it being set on the exchange but it is never re-thrown so I can capture it?

       

       

      Thanks

        • 1. Re: Handle Exception in MessageComposer
          Keith Babo Master

          Can you provide a bit more detail:

           

          • when the exception is thrown?  compose() or decompose()
          • service binding or reference binding? 
          • which binding are you using, as composition is specific to the binding being used


          Also, if you could let us know what your expected outcome is as a result of throwing an exception in a composer.

          • 2. Re: Re: Handle Exception in MessageComposer
            John DeStefano Apprentice

            Hi Keith,

             

            I've attached a sample project. The exception is thrown in the decompose method. Basically (but not demonstrated in the sample) I have a service which reads from a queue. The message on the queue is wrapped by the sending service. The wrapper has the source of the message, since this queue receives messages from multiple sources, the date it was queued up, and the message content. What I'd like to do is output the message to a file, but have the name and date be concatenated together to form the file name to write the message content to. The sample shows one of the problems I'm having, that being an exception thrown in the message composer. The sample contains a producer file gateway and a consumer file gateway. If you have any suggestions as to how I might approach this problem they would be greatly appreciated. As far as expected outcome, if I used a ReferenceInvoker to invoke the File outbound interface I'd like to be able to retrieve any exceptions that occurred.

             

            Thanks

            • 3. Re: Handle Exception in MessageComposer
              Keith Babo Master

              If your composer throws an exception in decompose(), I would expect the exchange to fault with the exception as the message content.  You should be able to get to the exception using ReferenceInvoker. 

               

              Questions:

              1) Are you saying the exception is not being reported when using ReferenceInvoker?

              2) What do you want to do if decompose() fails?  Do you plan to compensate in some way and invoke the service again?

               

              ~ keith

              • 4. Re: Re: Handle Exception in MessageComposer
                John DeStefano Apprentice

                Hi Keith,

                 

                Thanks for responding. I've attached another sample project that illustrates the issues I'm having. In the decompose method I'm forcing and exception to be thrown. I would expect to see that caught in the service that invokes the call (using ReferenceInvoker). It is not caught there. Secondly, I have a ContextMapper configuration with includes "CamelFileName" that I would expect the CamelFileName property, set as a property on the invocation, to be passed along to the file binding and actually write out a file with the associated name. If I run the sample and remove the custom MessageComposer ( in this scenario the exception will not be thrown) this does not happen. The file name is the one pre-configured on the file binding.

                 

                 

                Thanks

                • 5. Re: Re: Re: Handle Exception in MessageComposer
                  John DeStefano Apprentice

                  Hi Keith,

                   

                  I got this working sort of the way I want. I used a reference invoker to invoke the file binding as follows:

                   

                  @Override

                    public void process(String msg) {

                    try {

                    ReferenceInvocation invocation = _fileOut.newInvocation("process");

                    invocation.setProperty("CamelFileName", "otherNewfile.txt");

                    invocation.invoke(msg);

                    Message outMsg = invocation.getMessage();

                    Object obj = outMsg.getContent();

                    } catch (Exception e) {

                    e.printStackTrace();

                    }

                    }

                   

                  The _fileOut interface has a method with a signature of "String process(String message);" When an exception occurs in the MessageComposer the exception is caught in the Exception block. Setting the CamelFileName property on the invocation also correctly writes out the file with the name provided. This only works if the file name attribute on the binding definition (switchyard.xml) is not present. If the element is there but not set or has a value then the CamelFileName does not seem to "override" that.

                   

                  One other thing, you indicated that the exception could be seen in the returned RefenceInvocation object. If I change the interface for _fileOut to have a method with a signature of "void process(String msg)" and throw an exception in the MessageComposer the file is not written out and the code never gets into the Exception block as above. I would expect this as its and IN_ONLY invocation. However I can't find the exception in the returned ReferenceInvocation object. I can see an exception being set in org.apache.camel.impl.ProducerCache on the org.apache.camel.Exchange object but it does not appear to propogate? Any ideas?

                   

                  Thanks

                  • 6. Re: Handle Exception in MessageComposer
                    Keith Babo Master

                    Lack of exception availability on the in-only exchange could be a bug.  Can you attach the latest version of your app?  Sorry for the slow replies this week, but we're working hard to get our initial SwitchYard on Karaf support done.