You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicemix.apache.org by Guillaume Nodet <gn...@gmail.com> on 2008/03/01 20:51:01 UTC
Re: Exception Listener in servicemix
Maybe we should put this example on a wiki page ?
On Sat, Mar 1, 2008 at 4:55 PM, Bruce Snyder <br...@gmail.com> wrote:
>
> On Fri, Feb 29, 2008 at 3:27 PM, Tropi Geek <tr...@gmail.com> wrote:
> > Guys, things seem to be working very well with servicemix but one of the
> > challenges I am facing is to track errors happening at component level.
> >
> > Is there a generic listener that I can use/implement to listen to all
> > exceptions and then send it to a common exception queue component. I need to
> > report issues back to the requesting application. I can do that with most
> > bean/pojo components in a try/catch block but not with any components.
> >
> > Seems like a generic problem but I dont seem to be getting the right pointer
> > for this. Kindly help.
>
> Well, you could create an ExchangeListener to capture every message
> exchange flowing through the NMR. This gives you the ability to
> manipulate the message exchanges however you like including checking
> for errors on the exchange. Below is an example of something I wrote
> to enhance the error messages on an exchange:
>
> package org.apache.servicemix.jbi.exceptions;
>
> import javax.jbi.messaging.MessageExchange;
>
> import org.apache.servicemix.jbi.event.ExchangeEvent;
> import org.apache.servicemix.jbi.event.ExchangeListener;
>
> /**
> * An {@link org.apache.servicemix.jbi.event.ExchangeListener} implementation
> * to handle exception customization.
> *
> * @org.apache.xbean.XBean element="exceptionListenerService"
> * @version $Revision$
> * @author bsnyder
> */
> public class ExceptionListenerService implements ExchangeListener {
>
> public void exchangeAccepted(ExchangeEvent event) {
> // TODO Auto-generated method stub
>
> }
>
> public void exchangeSent(ExchangeEvent event) {
> MessageExchange me = event.getExchange();
> Exception exception = null;
>
> if (me.getError() != null) {
> exception = analyzeException(me);
> }
>
> me.setError(exception);
> }
>
> /**
> * This method abstracts any special exception handling behavior.
> *
> * @TODO Abstract this further using pluggable strategies to hold the
> * custom functionality. Then we just stuff the strategies that are
> * named in the servicemix.xml config into an array and just walk the
> * array, invoking the execute method on each strategy.
> *
> * @param error The exception that was thrown
> * @param endpointName The name of the endpoint that threw the exception
> * @return
> */
> private Exception analyzeException(MessageExchange me) {
> Exception error = me.getError();
> String serviceName = me.getEndpoint().getServiceName().toString();
> String endpointName = me.getEndpoint().getEndpointName();
> String errorMessage = error.getMessage();
>
> // Add calls to custom processing here
>
> StringBuilder newErrorMessage =
> new StringBuilder("The following error was caused by service: [");
> newErrorMessage.append(serviceName != null ? serviceName : "null");
> newErrorMessage.append("] and endpoint: [");
> newErrorMessage.append(endpointName != null ? endpointName : "null");
> newErrorMessage.append("] ");
> newErrorMessage.append("Original error: ");
> newErrorMessage.append(errorMessage);
>
> return new Exception(newErrorMessage.toString(), error);
> }
>
> }
>
> To use this with ServiceMIx, you simply register it as a service in
> the servicemix.xml file using the XBean element in the class level
> XBean annotation (exceptionListenerService).
>
> Bruce
> --
> perl -e 'print unpack("u30","D0G)U8V4\@4VYY9&5R\"F)R=6-E+G-N>61E<D\!G;6%I;\"YC;VT*"
> );'
>
> Apache ActiveMQ - http://activemq.org/
> Apache Camel - http://activemq.org/camel/
> Apache ServiceMix - http://servicemix.org/
> Apache Geronimo - http://geronimo.apache.org/
>
> Blog: http://bruceblog.org/
>
--
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
Re: Exception Listener in servicemix
Posted by Bruce Snyder <br...@gmail.com>.
On Sat, Mar 1, 2008 at 12:51 PM, Guillaume Nodet <gn...@gmail.com> wrote:
> Maybe we should put this example on a wiki page ?
Yeah I was thinking that as well. Let me enhance it a bit and I'll
commit it and document it on the wiki.
Bruce
--
perl -e 'print unpack("u30","D0G)U8V4\@4VYY9&5R\"F)R=6-E+G-N>61E<D\!G;6%I;\"YC;VT*"
);'
Apache ActiveMQ - http://activemq.org/
Apache Camel - http://activemq.org/camel/
Apache ServiceMix - http://servicemix.org/
Apache Geronimo - http://geronimo.apache.org/
Blog: http://bruceblog.org/