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 master 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 master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new e67cfa8 CAMEL-13625: Fixed camel-quarz2 fireNow to let the quartz scheduler be starter after the consumer. Thanks to Deepak for the analsys.
e67cfa8 is described below
commit e67cfa82ee23d47c3194c05f31ebf50c722ecb1a
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.
---
.../apache/camel/component/quartz2/CamelJob.java | 2 +
.../camel/component/quartz2/QuartzComponent.java | 19 ++++++--
.../quartz2/QuartzRouteFireNowOnlyOnceTest.java | 50 ++++++++++++++++++++++
3 files changed, 68 insertions(+), 3 deletions(-)
diff --git a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/CamelJob.java b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/CamelJob.java
index 2f525d2..8131632 100644
--- a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/CamelJob.java
+++ b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/CamelJob.java
@@ -60,6 +60,8 @@ public class CamelJob implements Job {
try {
if (processor != null) {
processor.process(exchange);
+ } else {
+ LOG.debug("Cannot execute CamelJob as there are no active consumers.");
}
} catch (Throwable e) {
exchange.setException(e);
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 b819e34..f0fe409 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.spi.Metadata;
import org.apache.camel.spi.annotations.Component;
@@ -50,7 +51,7 @@ import org.quartz.impl.StdSchedulerFactory;
* fully.</p>
*/
@Component("quartz,quartz2")
-public class QuartzComponent extends DefaultComponent implements StartupListener {
+public class QuartzComponent extends DefaultComponent implements ExtendedStartupListener {
@Metadata(label = "advanced")
private Scheduler scheduler;
@@ -456,16 +457,28 @@ public class QuartzComponent extends DefaultComponent implements StartupListener
@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..286d92e
--- /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