You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/01/25 09:15:02 UTC

svn commit: r1438380 - in /camel/trunk/components/camel-quartz/src: main/java/org/apache/camel/component/quartz/CamelJob.java test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java

Author: davsclaus
Date: Fri Jan 25 08:15:02 2013
New Revision: 1438380

URL: http://svn.apache.org/viewvc?rev=1438380&view=rev
Log:
CAMEL-5994: Quartz trigger should reuse endpoint from route to use the same as the consumer. Thanks to Alexis Kinsella and Denis Delangle for code with fix and test case.

Added:
    camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java   (with props)
Modified:
    camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java

Modified: camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java?rev=1438380&r1=1438379&r2=1438380&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java (original)
+++ camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java Fri Jan 25 08:15:02 2013
@@ -19,17 +19,23 @@ package org.apache.camel.component.quart
 import java.io.Serializable;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Route;
 import org.quartz.Job;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
 import org.quartz.SchedulerContext;
 import org.quartz.SchedulerException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.camel.util.URISupport.normalizeUri;
 
 /**
  * @version 
  */
 public class CamelJob implements Job, Serializable {
 
+    private static final transient Logger LOG = LoggerFactory.getLogger(CamelJob.class);
     private static final long serialVersionUID = 26L;
 
     public void execute(JobExecutionContext context) throws JobExecutionException {
@@ -47,11 +53,38 @@ public class CamelJob implements Job, Se
         if (camelContext == null) {
             throw new JobExecutionException("No CamelContext could be found with name: " + camelContextName);
         }
-        QuartzEndpoint endpoint = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
+
+        QuartzEndpoint endpoint = lookupQuartzEndpoint(camelContext, endpointUri);
         if (endpoint == null) {
             throw new JobExecutionException("No QuartzEndpoint could be found with uri: " + endpointUri);
         }
         endpoint.onJobExecute(context);
     }
 
+    private QuartzEndpoint lookupQuartzEndpoint(CamelContext camelContext, String endpointUri) throws JobExecutionException {
+        try {
+            String targetUri = normalizeUri(endpointUri);
+
+            // check all active routes for the quartz endpoint this task matches
+            // as we prefer to use the existing endpoint from the routes
+            for (Route route : camelContext.getRoutes()) {
+                if (route.getEndpoint() instanceof QuartzEndpoint) {
+                    if (normalizeUri(route.getEndpoint().getEndpointUri()).equals(targetUri)) {
+                        return (QuartzEndpoint) route.getEndpoint();
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new JobExecutionException("Error lookup up existing QuartzEndpoint with uri: " + endpointUri, e);
+        }
+
+        // fallback and lookup existing from registry (eg maybe a @Consume POJO with a quartz endpoint, and thus not from a route)
+        if (camelContext.hasEndpoint(endpointUri) != null) {
+            return camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
+        } else {
+            LOG.warn("Cannot find existing QuartzEndpoint with uri: {}. Creating new endpoint instance.", endpointUri);
+            return camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
+        }
+    }
+
 }
\ No newline at end of file

Added: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java?rev=1438380&view=auto
==============================================================================
--- camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java (added)
+++ camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java Fri Jan 25 08:15:02 2013
@@ -0,0 +1,69 @@
+/**
+ * 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.quartz;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * Endpoints are stored in a LRU list with a default capacity of 1000. If the list is full,
+ * then endpoints are removed and should be recreated.
+ * <p/>
+ * We simulate this behavior with a capacity of 1 element.
+ */
+public class QuartzCronRouteWithSmallCacheTest extends CamelTestSupport {
+
+    private final CountDownLatch latch = new CountDownLatch(3);
+
+    @Test
+    public void testQuartzCronRouteWithSmallCache() throws Exception {
+        boolean wait = latch.await(10, TimeUnit.SECONDS);
+        assertTrue(wait);
+        assertTrue("Quartz should trigger at least 3 times", latch.getCount() <= 0);
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getProperties().put(Exchange.MAXIMUM_ENDPOINT_CACHE_SIZE, "1");
+        return context;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:foo").to("log:foo");
+
+                from("quartz://myGroup/myTimerName?cron=0/2+*+*+*+*+?").process(new Processor() {
+                    @Override
+                    public void process(Exchange exchange) throws Exception {
+                        latch.countDown();
+                        template.sendBody("direct:foo", "Quartz triggered");
+                    }
+                });
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date