Remember you are doing a test and in real life you wont use producer
template to send a message to a direct Camel route. Your routes would
be other components such as files / http / cxf / activemq etc.



On Mon, Aug 12, 2013 at 2:38 PM, pmcb55 <mcbenne...@dnb.com> wrote:
> Hi Claus - thanks again for the response, but I'm still unclear. I've been
> playing with my test code, and I think this version might illustrate the
> problem better (I now define endpoints instead of my simpler original that
> only had a 'Processor()', and I've added 'Thread.sleep()' to the endpoints
> to show timings).
>
> Notice that the output is *almost* perfect - the only problem is that I get
> one extra (spurious to my mind!) event being fired (by the ProducerTemplate,
> as your first reply made clear), which of course screws up invocation counts
> and average timings for the overall route [direct://testRouteTest].
>
> I know I could probably jump to JMX for all this, but first is there any
> easy way to eliminate this last event being fired? As you can see from the
> output, everything would be perfect otherwise - i.e. this code is extremely
> simple and intuitive, and I'd like to keep it that way if possible.
>
>
> OUTPUT (perfect except for last event, in *bold*):
> --------
>
> Route 1
> ID-UKDB1764398-61884-1376310614400-0-2 exchange Exchange[Message: [Body is
> null]] sent to: Endpoint[direct://testRoute1] took: 307 ms.
> Route 2
> ID-UKDB1764398-61884-1376310614400-0-2 exchange Exchange[Message: [Body is
> null]] sent to: Endpoint[direct://testRoute2] took: 0 ms.
> Route 3
> ID-UKDB1764398-61884-1376310614400-0-2 exchange Exchange[Message: [Body is
> null]] sent to: Endpoint[direct://testRoute3] took: 200 ms.
> ID-UKDB1764398-61884-1376310614400-0-2 exchange Exchange[Message: [Body is
> null]] sent to: Endpoint[direct://testRouteTest] took: 530 ms.
> *ID-UKDB1764398-61884-1376310614400-0-2 exchange Exchange[Message: [Body is
> null]] sent to: Endpoint[direct://testRouteTest] took: 530 ms.*
>
>
> package com.dnb.core.integration.utils.eip;
>
> import java.util.EventObject;
>
> import org.apache.camel.CamelContext;
> import org.apache.camel.Exchange;
> import org.apache.camel.Processor;
> import org.apache.camel.ProducerTemplate;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.impl.DefaultCamelContext;
> import org.apache.camel.impl.DefaultExchange;
> import org.apache.camel.management.event.ExchangeSentEvent;
> import org.apache.camel.support.EventNotifierSupport;
> import org.junit.Test;
>
> /**
>  * @author McBennettP
>  *
>  * I just created this test case to post to the Camel user list - looks like
>  * a bug to me...!?
>  */
> public class SentEventTest {
>         @Test
>         public void testCamel() throws Exception {
>         CamelContext context = new DefaultCamelContext();
>
>         context.addRoutes(new RouteBuilder() {
>             public void configure() {
>                 from("direct://testRoute1")
>                 .routeId("direct://testRoute1")
>                 .process(new Processor() {
>                         public void process(Exchange exchange) {
>                                 System.out.println("Route 1");
>                                 try { Thread.sleep(300); } catch 
> (InterruptedException e)
> { }
>                         };
>                 });
>
>                 from("direct://testRoute2")
>                 .routeId("direct://testRoute2")
>                 .process(new Processor() {
>                         public void process(Exchange exchange) {
>                                 System.out.println("Route 2");
>                         };
>                 });
>
>                 from("direct://testRoute3")
>                 .routeId("direct://testRoute3")
>                 .process(new Processor() {
>                         public void process(Exchange exchange) {
>                                 System.out.println("Route 3");
>                                 try { Thread.sleep(200); } catch 
> (InterruptedException e)
> { }
>                         };
>                 });
>
>                 from("direct://testRouteTest")
>                 .routeId("direct://testRouteTest")
>                 .to("direct://testRoute1")
>                 .to("direct://testRoute2")
>                 .to("direct://testRoute3");
>             }
>         });
>
>         context.getManagementStrategy().addEventNotifier(
>                         new EventNotifierSupport() {
>                                 @Override
>                             public void notify(final EventObject event) 
> throws Exception {
>                                         final ExchangeSentEvent sent = 
> (ExchangeSentEvent) event;
>                                         System.out.println(sent);
>                             }
>
>                             public boolean isEnabled(final EventObject event) 
> {
>                                 return (event instanceof ExchangeSentEvent);
>                             }
>
>                             protected void doStart() throws Exception {
>                             }
>
>                             protected void doStop() throws Exception {
>                             }
>                         });
>
>         context.start();
>         ProducerTemplate producer = context.createProducerTemplate();
>         Exchange testExchange = new DefaultExchange(context);
>         producer.send("direct://testRouteTest", testExchange);
>         context.stop();
>         }
> }
>
>
>
> --
> View this message in context: 
> http://camel.465427.n5.nabble.com/Bug-with-addEventNotifier-fires-two-exchange-sent-events-for-each-Exchange-sent-tp5737086p5737143.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cib...@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Reply via email to