Hi guys,

Back in Aug 2013 Claus implemented a change to allow event notifications in
the producer template to be turned on or off: 
https://issues.apache.org/jira/browse/CAMEL-6628
<https://issues.apache.org/jira/browse/CAMEL-6628>  

But in fact this change isn't quite right. The test case below illustrates
the problem.

The problem seems to be that the ProducerTemplate uses a producer cache, and
both the producer and it's cache are firing events (if events are enabled),
when in fact only one of them should be firing the event.

So with notifications enabled on the ProducerTemplate I get one too many
events, while with notifications disabled I get one event too few!

The fix seems to be either remove the event firing from the producer cache,
or add a new method to enable/disable notifications for the producer cache
(i.e. a new method:
ProducerTemplate::setCacheEventNotifierEnabled(boolean)).


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.Assert;
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 {
    private int firedEventCount = 0;


    @Test
    public void testCamel() throws Exception {
        CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {
            public void configure() {
                from("direct://testRoute1")
                        .process(new Processor() {
                            public void process(Exchange exchange) {};
                        });

                from("direct://testRoute2")
                        .process(new Processor() {
                            public void process(Exchange exchange) {};
                        });

                from("direct://MASTER-ROUTE")
                        .to("direct://testRoute1")
                        .to("direct://testRoute2");
            }
        });

        context.getManagementStrategy().addEventNotifier(
                new EventNotifierSupport() {
                    public void notify(final EventObject event) throws
Exception {
                        System.out.println("Count: " + (++firedEventCount) +
", event: "
                                + ((ExchangeSentEvent)
event).getEndpoint());
                    }

                    public boolean isEnabled(final EventObject event) {
                        return (event instanceof ExchangeSentEvent);
                    }
                });

        context.start();
        ProducerTemplate producer = context.createProducerTemplate();

        producer.setEventNotifierEnabled(true);
        Exchange testExchange = new DefaultExchange(context);
        producer.send("direct://MASTER-ROUTE", testExchange);
        *Assert.assertEquals("This should REALLY be 3!!", 4,
firedEventCount);*

        producer.setEventNotifierEnabled(false);
        producer.send("direct://MASTER-ROUTE", testExchange);
        producer.send("direct://MASTER-ROUTE", testExchange);
        *Assert.assertEquals("This should REALLY be 9!", 8,
firedEventCount);*

        context.stop();
    }
}



--
View this message in context: 
http://camel.465427.n5.nabble.com/Closed-issue-CAMEL-6628-in-2-12-0-doesn-t-fix-the-problem-tp5746953.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to