Author: davsclaus
Date: Fri Oct  1 13:16:11 2010
New Revision: 1003527

URL: http://svn.apache.org/viewvc?rev=1003527&view=rev
Log:
CAMEL-3187: Fixed PublishEventNotifier to not emit events during events.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierToRouteTest.java
      - copied, changed from r1003501, 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/management/PublishEventNotifier.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/EventHelper.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=1003527&r1=1003526&r2=1003527&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Fri Oct 
 1 13:16:11 2010
@@ -135,6 +135,8 @@ public interface Exchange {
     String MULTICAST_INDEX         = "CamelMulticastIndex";
     String MULTICAST_COMPLETE      = "CamelMulticastComplete";
 
+    String NOTIFY_EVENT = "CamelNotifyEvent";
+
     String ON_COMPLETION = "CamelOnCompletion";
 
     String REDELIVERED          = "CamelRedelivered";

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/PublishEventNotifier.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/PublishEventNotifier.java?rev=1003527&r1=1003526&r2=1003527&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/PublishEventNotifier.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/management/PublishEventNotifier.java
 Fri Oct  1 13:16:11 2010
@@ -29,6 +29,10 @@ import org.apache.camel.util.ServiceHelp
 /**
  * A {...@link org.apache.camel.spi.EventNotifier} which publishes the 
{...@link EventObject} to some
  * {...@link org.apache.camel.Endpoint}.
+ * <p/>
+ * This notifier is only enabled when {...@link CamelContext} is started. This 
avoids problems when
+ * sending notifications during start/shutdown of {...@link CamelContext} 
which causes problems by
+ * sending those events to Camel routes by this notifier.
  *
  * @version $Revision$
  */
@@ -40,10 +44,35 @@ public class PublishEventNotifier extend
     private Producer producer;
 
     public void notify(EventObject event) throws Exception {
+        // only notify when we are started
+        if (!isStarted()) {
+            if (log.isDebugEnabled()) {
+                log.debug("Cannot publish event as notifier is not started: " 
+ event);
+            }
+            return;
+        }
+
+        // only notify when camel context is running
+        if (!camelContext.getStatus().isStarted()) {
+            if (log.isDebugEnabled()) {
+                log.debug("Cannot publish event as CamelContext is not 
started: " + event);
+            }
+            return;
+        }
+
         Exchange exchange = producer.createExchange();
         exchange.getIn().setBody(event);
 
-        producer.process(exchange);
+        // make sure we don't send out events for this as well
+        // mark exchange as being published to event, to prevent creating new 
events
+        // for this as well (causing a endless flood of events)
+        exchange.setProperty(Exchange.NOTIFY_EVENT, Boolean.TRUE);
+        try {
+            producer.process(exchange);
+        } finally {
+            // and remove it when its done
+            exchange.removeProperty(Exchange.NOTIFY_EVENT);
+        }
     }
 
     public boolean isEnabled(EventObject event) {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/EventHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/EventHelper.java?rev=1003527&r1=1003526&r2=1003527&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/EventHelper.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/EventHelper.java 
Fri Oct  1 13:16:11 2010
@@ -273,6 +273,11 @@ public final class EventHelper {
     }
 
     public static void notifyExchangeCreated(CamelContext context, Exchange 
exchange) {
+        if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) 
{
+            // do not generate events for an notify event
+            return;
+        }
+
         List<EventNotifier> notifiers = 
context.getManagementStrategy().getEventNotifiers();
         if (notifiers == null || notifiers.isEmpty()) {
             return;
@@ -296,6 +301,11 @@ public final class EventHelper {
     }
 
     public static void notifyExchangeDone(CamelContext context, Exchange 
exchange) {
+        if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) 
{
+            // do not generate events for an notify event
+            return;
+        }
+
         List<EventNotifier> notifiers = 
context.getManagementStrategy().getEventNotifiers();
         if (notifiers == null || notifiers.isEmpty()) {
             return;
@@ -319,6 +329,11 @@ public final class EventHelper {
     }
 
     public static void notifyExchangeFailed(CamelContext context, Exchange 
exchange) {
+        if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) 
{
+            // do not generate events for an notify event
+            return;
+        }
+
         List<EventNotifier> notifiers = 
context.getManagementStrategy().getEventNotifiers();
         if (notifiers == null || notifiers.isEmpty()) {
             return;
@@ -343,6 +358,11 @@ public final class EventHelper {
 
     public static void notifyExchangeFailureHandled(CamelContext context, 
Exchange exchange, Processor failureHandler,
                                                     boolean deadLetterChannel) 
{
+        if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) 
{
+            // do not generate events for an notify event
+            return;
+        }
+
         List<EventNotifier> notifiers = 
context.getManagementStrategy().getEventNotifiers();
         if (notifiers == null || notifiers.isEmpty()) {
             return;
@@ -366,6 +386,11 @@ public final class EventHelper {
     }
 
     public static void notifyExchangeRedelivery(CamelContext context, Exchange 
exchange, int attempt) {
+        if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) 
{
+            // do not generate events for an notify event
+            return;
+        }
+
         List<EventNotifier> notifiers = 
context.getManagementStrategy().getEventNotifiers();
         if (notifiers == null || notifiers.isEmpty()) {
             return;
@@ -389,6 +414,11 @@ public final class EventHelper {
     }
 
     public static void notifyExchangeSent(CamelContext context, Exchange 
exchange, Endpoint endpoint, long timeTaken) {
+        if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) 
{
+            // do not generate events for an notify event
+            return;
+        }
+
         List<EventNotifier> notifiers = 
context.getManagementStrategy().getEventNotifiers();
         if (notifiers == null || notifiers.isEmpty()) {
             return;

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java?rev=1003527&r1=1003526&r2=1003527&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java
 Fri Oct  1 13:16:11 2010
@@ -46,7 +46,7 @@ public class PublishEventNotifierTest ex
 
     public void testExchangeDone() throws Exception {
         getMockEndpoint("mock:result").expectedMessageCount(1);
-        getMockEndpoint("mock:event").expectedMessageCount(9);
+        getMockEndpoint("mock:event").expectedMessageCount(6);
 
         template.sendBody("direct:start", "Hello World");
 
@@ -54,7 +54,7 @@ public class PublishEventNotifierTest ex
     }
 
     public void testExchangeFailed() throws Exception {
-        getMockEndpoint("mock:event").expectedMessageCount(7);
+        getMockEndpoint("mock:event").expectedMessageCount(4);
 
         try {
             template.sendBody("direct:fail", "Hello World");

Copied: 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierToRouteTest.java
 (from r1003501, 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierToRouteTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierToRouteTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java&r1=1003501&r2=1003527&rev=1003527&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/management/PublishEventNotifierToRouteTest.java
 Fri Oct  1 13:16:11 2010
@@ -24,7 +24,7 @@ import org.apache.camel.impl.DefaultCame
 /**
  * @version $Revision$
  */
-public class PublishEventNotifierTest extends ContextTestSupport {
+public class PublishEventNotifierToRouteTest extends ContextTestSupport {
 
     @Override
     protected boolean useJmx() {
@@ -37,7 +37,7 @@ public class PublishEventNotifierTest ex
 
         PublishEventNotifier notifier = new PublishEventNotifier();
         notifier.setCamelContext(context);
-        notifier.setEndpointUri("mock:event");
+        notifier.setEndpointUri("seda:event");
 
         context.getManagementStrategy().addEventNotifier(notifier);
 
@@ -46,7 +46,7 @@ public class PublishEventNotifierTest ex
 
     public void testExchangeDone() throws Exception {
         getMockEndpoint("mock:result").expectedMessageCount(1);
-        getMockEndpoint("mock:event").expectedMessageCount(9);
+        getMockEndpoint("mock:event").expectedMinimumMessageCount(6);
 
         template.sendBody("direct:start", "Hello World");
 
@@ -54,7 +54,7 @@ public class PublishEventNotifierTest ex
     }
 
     public void testExchangeFailed() throws Exception {
-        getMockEndpoint("mock:event").expectedMessageCount(7);
+        getMockEndpoint("mock:event").expectedMessageCount(4);
 
         try {
             template.sendBody("direct:fail", "Hello World");
@@ -75,6 +75,8 @@ public class PublishEventNotifierTest ex
                 from("direct:start").to("log:foo").to("mock:result");
 
                 from("direct:fail").throwException(new 
IllegalArgumentException("Damn"));
+
+                from("seda:event").log("Event ${body}").to("mock:event");
             }
         };
     }


Reply via email to