camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1091768 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/component/timer/ test/java/org/apache/camel/component/timer/
Date Wed, 13 Apr 2011 13:00:58 GMT
Author: davsclaus
Date: Wed Apr 13 13:00:57 2011
New Revision: 1091768

URL: http://svn.apache.org/viewvc?rev=1091768&view=rev
Log:
CAMEL-3688: Added repeatCount option to timer, to limit the number of time it fires.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerRepeatCountTest.java
      - copied, changed from r1091754, camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerEndpoint.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.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=1091768&r1=1091767&r2=1091768&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 Wed Apr 13 13:00:57
2011
@@ -164,6 +164,7 @@ public interface Exchange {
     String SPLIT_COMPLETE     = "CamelSplitComplete";
     String SPLIT_SIZE         = "CamelSplitSize";
 
+    String TIMER_COUNTER         = "CamelTimerCounter";
     String TIMER_FIRED_TIME      = "CamelTimerFiredTime";
     String TIMER_NAME            = "CamelTimerName";
     String TIMER_PERIOD          = "CamelTimerPeriod";

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java?rev=1091768&r1=1091767&r2=1091768&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
Wed Apr 13 13:00:57 2011
@@ -19,6 +19,7 @@ package org.apache.camel.component.timer
 import java.util.Date;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
@@ -44,9 +45,21 @@ public class TimerConsumer extends Defau
     @Override
     protected void doStart() throws Exception {
         task = new TimerTask() {
+            // counter
+            private final AtomicLong counter = new AtomicLong();
+
             @Override
             public void run() {
-                sendTimerExchange();
+                long count = counter.incrementAndGet();
+
+                boolean fire = endpoint.getRepeatCount() <= 0 || count <= endpoint.getRepeatCount();
+                if (fire) {
+                    sendTimerExchange(count);
+                } else {
+                    // no need to fire anymore as we exceeded repeat count
+                    LOG.debug("Cancelling {} timer as repeat count limit reached after {}
counts.", endpoint.getTimerName(), endpoint.getRepeatCount());
+                    cancel();
+                }
             }
         };
 
@@ -86,8 +99,9 @@ public class TimerConsumer extends Defau
         }
     }
 
-    protected void sendTimerExchange() {
+    protected void sendTimerExchange(long counter) {
         Exchange exchange = endpoint.createExchange();
+        exchange.setProperty(Exchange.TIMER_COUNTER, counter);
         exchange.setProperty(Exchange.TIMER_NAME, endpoint.getTimerName());
         exchange.setProperty(Exchange.TIMER_TIME, endpoint.getTime());
         exchange.setProperty(Exchange.TIMER_PERIOD, endpoint.getPeriod());
@@ -97,7 +111,7 @@ public class TimerConsumer extends Defau
         // also set now on in header with same key as quartz to be consistent
         exchange.getIn().setHeader("firedTime", now);
 
-        LOG.trace("Timer {} is firing", endpoint.getTimerName());
+        LOG.trace("Timer {} is firing #{} count", endpoint.getTimerName(), counter);
         try {
             getProcessor().process(exchange);
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerEndpoint.java?rev=1091768&r1=1091767&r2=1091768&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerEndpoint.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/timer/TimerEndpoint.java
Wed Apr 13 13:00:57 2011
@@ -42,6 +42,7 @@ public class TimerEndpoint extends Defau
     private boolean fixedRate;
     private boolean daemon = true;
     private Timer timer;
+    private long repeatCount;
 
     public TimerEndpoint() {
     }
@@ -129,6 +130,16 @@ public class TimerEndpoint extends Defau
         this.period = period;
     }
 
+    @ManagedAttribute(description = "Repeat Count")
+    public long getRepeatCount() {
+        return repeatCount;
+    }
+
+    @ManagedAttribute(description = "Repeat Count")
+    public void setRepeatCount(long repeatCount) {
+        this.repeatCount = repeatCount;
+    }
+
     public Date getTime() {
         return time;
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java?rev=1091768&r1=1091767&r2=1091768&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java
Wed Apr 13 13:00:57 2011
@@ -36,6 +36,7 @@ public class TimerFiredTimeTest extends 
         assertEquals("hello", exchange.getProperty(Exchange.TIMER_NAME));
         assertNotNull(exchange.getProperty(Exchange.TIMER_FIRED_TIME));
         assertNotNull(exchange.getIn().getHeader("firedTime"));
+        assertEquals(Long.valueOf(1), exchange.getProperty(Exchange.TIMER_COUNTER));
     }
 
     @Override

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerRepeatCountTest.java
(from r1091754, camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerRepeatCountTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerRepeatCountTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java&r1=1091754&r2=1091768&rev=1091768&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerFiredTimeTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/timer/TimerRepeatCountTest.java
Wed Apr 13 13:00:57 2011
@@ -24,25 +24,26 @@ import org.apache.camel.component.mock.M
 /**
  * Unit test for fired time exchange property
  */
-public class TimerFiredTimeTest extends ContextTestSupport {
+public class TimerRepeatCountTest extends ContextTestSupport {
 
-    public void testFired() throws Exception {
+    public void testRepeatCount() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMinimumMessageCount(1);
+        mock.expectedMessageCount(3);
+        mock.setAssertPeriod(500);
+        mock.message(0).header(Exchange.TIMER_COUNTER).isEqualTo(1);
+        mock.message(1).header(Exchange.TIMER_COUNTER).isEqualTo(2);
+        mock.message(2).header(Exchange.TIMER_COUNTER).isEqualTo(3);
 
-        assertMockEndpointsSatisfied();
+        // we should only get 3 messages as we have a repeat count limit at 3
 
-        Exchange exchange = mock.getExchanges().get(0);
-        assertEquals("hello", exchange.getProperty(Exchange.TIMER_NAME));
-        assertNotNull(exchange.getProperty(Exchange.TIMER_FIRED_TIME));
-        assertNotNull(exchange.getIn().getHeader("firedTime"));
+        assertMockEndpointsSatisfied();
     }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from("timer://hello").to("mock:result");
+                from("timer://hello?repeatCount=3&period=10").to("mock:result");
             }
         };
     }



Mime
View raw message