Updated Branches:
  refs/heads/master 9ecc122b4 -> 2affc3b7a

CAMEL-4928: Timer component supports async routing engine. The consumer is by 
default async now. Thanks to Sergey Zhemzhitsky for patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/561a38ce
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/561a38ce
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/561a38ce

Branch: refs/heads/master
Commit: 561a38ce9f2b194728099fe9756a07ed03c8a91e
Parents: d5f4299
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Aug 7 17:12:39 2013 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Aug 7 18:33:33 2013 +0200

----------------------------------------------------------------------
 .../camel/component/timer/TimerConsumer.java    | 44 +++++++++---
 .../camel/component/timer/TimerAsyncTest.java   | 71 ++++++++++++++++++++
 2 files changed, 106 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/561a38ce/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java 
b/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
index b8a7c09..00148be 100644
--- 
a/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
+++ 
b/camel-core/src/main/java/org/apache/camel/component/timer/TimerConsumer.java
@@ -21,6 +21,7 @@ import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.camel.AsyncCallback;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
@@ -94,6 +95,16 @@ public class TimerConsumer extends DefaultConsumer {
         return endpoint.getCamelContext().getStatus().isStarted() && 
isRunAllowed() && !isSuspended();
     }
 
+    @Override
+    protected void doSuspend() throws Exception {
+        doStop();
+    }
+
+    @Override
+    protected void doResume() throws Exception {
+        doStart();
+    }
+
     protected void configureTask(TimerTask task, Timer timer) {
         if (endpoint.isFixedRate()) {
             if (endpoint.getTime() != null) {
@@ -119,7 +130,7 @@ public class TimerConsumer extends DefaultConsumer {
     }
 
     protected void sendTimerExchange(long counter) {
-        Exchange exchange = endpoint.createExchange();
+        final Exchange exchange = endpoint.createExchange();
         exchange.setProperty(Exchange.TIMER_COUNTER, counter);
         exchange.setProperty(Exchange.TIMER_NAME, endpoint.getTimerName());
         exchange.setProperty(Exchange.TIMER_TIME, endpoint.getTime());
@@ -130,16 +141,31 @@ public class TimerConsumer extends DefaultConsumer {
         // also set now on in header with same key as quartz to be consistent
         exchange.getIn().setHeader("firedTime", now);
 
-        LOG.trace("Timer {} is firing #{} count", endpoint.getTimerName(), 
counter);
-        try {
-            getProcessor().process(exchange);
-        } catch (Exception e) {
-            exchange.setException(e);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Timer {} is firing #{} count", endpoint.getTimerName(), 
counter);
         }
 
-        // handle any thrown exception
-        if (exchange.getException() != null) {
-            getExceptionHandler().handleException("Error processing exchange", 
exchange, exchange.getException());
+        if (!endpoint.isSynchronous()) {
+            getAsyncProcessor().process(exchange, new AsyncCallback() {
+                @Override
+                public void done(boolean doneSync) {
+                    // handle any thrown exception
+                    if (exchange.getException() != null) {
+                        getExceptionHandler().handleException("Error 
processing exchange", exchange, exchange.getException());
+                    }
+                }
+            });
+        } else {
+            try {
+                getProcessor().process(exchange);
+            } catch (Exception e) {
+                exchange.setException(e);
+            }
+
+            // handle any thrown exception
+            if (exchange.getException() != null) {
+                getExceptionHandler().handleException("Error processing 
exchange", exchange, exchange.getException());
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/561a38ce/camel-core/src/test/java/org/apache/camel/component/timer/TimerAsyncTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/timer/TimerAsyncTest.java 
b/camel-core/src/test/java/org/apache/camel/component/timer/TimerAsyncTest.java
new file mode 100644
index 0000000..9c590b5
--- /dev/null
+++ 
b/camel-core/src/test/java/org/apache/camel/component/timer/TimerAsyncTest.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.timer;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ThreadPoolRejectedPolicy;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version
+ */
+public class TimerAsyncTest extends ContextTestSupport {
+
+    public void testSync() throws Exception {
+        TimerEndpoint endpoint = 
context.getEndpoint("timer:foo?synchronous=true", TimerEndpoint.class);
+        assertTrue("Timer endpoint must be synchronous, but it isn't", 
endpoint.isSynchronous());
+    }
+
+    public void testAsync() throws Exception {
+        TimerEndpoint endpoint = context.getEndpoint("timer:foo", 
TimerEndpoint.class);
+        assertFalse("Timer endpoint must be asynchronous, but it isn't", 
endpoint.isSynchronous());
+    }
+
+    public void testAsyncRouting() throws Exception {
+        final int threads = 5;
+
+        // should trigger many tasks as we are async
+        getMockEndpoint("mock:task").expectedMinimumMessageCount(20);
+
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                
from("timer://foo?fixedRate=true&delay=0&period=200").id("timer")
+                        .threads(threads, 
threads).maxQueueSize(1).rejectedPolicy(ThreadPoolRejectedPolicy.CallerRuns)
+                        .to("log:task")
+                        .to("mock:task")
+                        .process(new Processor() {
+                            public void process(Exchange exchange) throws 
Exception {
+                                // simulate long task
+                                TimeUnit.SECONDS.sleep(1);
+                            }
+                        });
+            }
+        });
+        context.start();
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+}

Reply via email to