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 2019/06/09 11:44:39 UTC

[camel] branch camel-2.23.x updated: CAMEL-13625: Fixed camel-quarz2 fireNow to let the quartz scheduler be starter after the consumer. Thanks to Deepak for the analsys.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.23.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.23.x by this push:
     new 6ea284c  CAMEL-13625: Fixed camel-quarz2 fireNow to let the quartz scheduler be starter after the consumer. Thanks to Deepak for the analsys.
6ea284c is described below

commit 6ea284c6cfe9f0cd193241ea21be9c46cd3dad26
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Jun 9 12:39:35 2019 +0200

    CAMEL-13625: Fixed camel-quarz2 fireNow to let the quartz scheduler be starter after the consumer. Thanks to Deepak for the analsys.
---
 .../camel/component/quartz2/QuartzComponent.java   | 19 ++++++--
 .../quartz2/QuartzRouteFireNowOnlyOnceTest.java    | 50 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzComponent.java b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzComponent.java
index c6342c53..bbe48c0 100644
--- a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzComponent.java
+++ b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzComponent.java
@@ -25,6 +25,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.ExtendedStartupListener;
 import org.apache.camel.StartupListener;
 import org.apache.camel.impl.UriEndpointComponent;
 import org.apache.camel.spi.Metadata;
@@ -50,7 +51,7 @@ import org.slf4j.LoggerFactory;
  * of the code, but mostly has been re-written in attempt to be more easier to maintain, and use Quartz more
  * fully.</p>
  */
-public class QuartzComponent extends UriEndpointComponent implements StartupListener {
+public class QuartzComponent extends UriEndpointComponent implements ExtendedStartupListener {
     private static final Logger LOG = LoggerFactory.getLogger(QuartzComponent.class);
     @Metadata(label = "advanced")
     private Scheduler scheduler;
@@ -457,16 +458,28 @@ public class QuartzComponent extends UriEndpointComponent implements StartupList
 
     @Override
     public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception {
+        if (alreadyStarted) {
+            // a route may have been added or starter after CamelContext is started so ensure we startup the scheduler
+            doStartScheduler();
+        }
+    }
+
+    @Override
+    public void onCamelContextFullyStarted(CamelContext context, boolean alreadyStarted) throws Exception {
+        doStartScheduler();
+    }
+
+    protected void doStartScheduler() throws Exception {
         // If Camel has already started and then user add a route dynamically, we need to ensure
         // to create and init the scheduler first.
         if (scheduler == null) {
             createAndInitScheduler();
         } else {
-            // in case custom scheduler was injected (i.e. created elsewhere), we may need to add 
+            // in case custom scheduler was injected (i.e. created elsewhere), we may need to add
             // current camel context to quartz context so jobs have access
             storeCamelContextInQuartzContext();
         }
-        
+
         // Now scheduler is ready, let see how we should start it.
         if (!autoStartScheduler) {
             LOG.info("Not starting scheduler because autoStartScheduler is set to false.");
diff --git a/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzRouteFireNowOnlyOnceTest.java b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzRouteFireNowOnlyOnceTest.java
new file mode 100644
index 0000000..6cddefc
--- /dev/null
+++ b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzRouteFireNowOnlyOnceTest.java
@@ -0,0 +1,50 @@
+/**
+ * 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.quartz2;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class QuartzRouteFireNowOnlyOnceTest extends BaseQuartzTest {
+
+    protected MockEndpoint resultEndpoint;
+
+    @Test
+    public void testQuartzRoute() throws Exception {
+        resultEndpoint = getMockEndpoint("mock:result");
+        resultEndpoint.expectedMessageCount(1);
+        resultEndpoint.message(0).header("triggerName").isEqualTo("myTimerName");
+        resultEndpoint.message(0).header("triggerGroup").isEqualTo("myGroup");
+
+        // lets test the receive worked
+        resultEndpoint.assertIsSatisfied();
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: example
+                from("quartz2://myGroup/myTimerName?fireNow=true&trigger.repeatInterval=2000&trigger.repeatCount=0")
+                        .to("log:quartz")
+                        .to("mock:result");
+                // END SNIPPET: example
+            }
+        };
+    }
+}
\ No newline at end of file