[camel] branch master updated: CAMEL-14577: QuartzEndpoint returns no trigger parameters. Thansk to Dennis for the test case.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel] branch master updated: CAMEL-14577: QuartzEndpoint returns no trigger parameters. Thansk to Dennis for the test case.

davsclaus-2
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 31ab54c  CAMEL-14577: QuartzEndpoint returns no trigger parameters. Thansk to Dennis for the test case.
31ab54c is described below

commit 31ab54c858b38df591aa9622788294a1f445ed9e
Author: Claus Ibsen <[hidden email]>
AuthorDate: Mon Feb 17 09:59:42 2020 +0100

    CAMEL-14577: QuartzEndpoint returns no trigger parameters. Thansk to Dennis for the test case.
---
 .../camel/component/quartz/QuartzEndpoint.java     | 27 ++++++-----
 .../quartz/QuartzTriggerParametersTest.java        | 54 ++++++++++++++++++++++
 2 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
index 908b683..9b518c3 100644
--- a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
+++ b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.quartz;
 
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.TimeZone;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -49,7 +50,6 @@ import org.quartz.TriggerKey;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 import static org.quartz.CronScheduleBuilder.cronSchedule;
 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
 
@@ -438,6 +438,9 @@ public class QuartzEndpoint extends DefaultEndpoint {
     }
 
     private Trigger createTrigger(JobDetail jobDetail) throws Exception {
+        // use a defensive copy to keep the trigger parameters on the endpoint
+        Map<String, Object> copy = new HashMap<>(triggerParameters);
+
         Trigger result;
         Date startTime = new Date();
         if (getComponent().getScheduler().isStarted()) {
@@ -445,7 +448,7 @@ public class QuartzEndpoint extends DefaultEndpoint {
         }
         if (cron != null) {
             LOG.debug("Creating CronTrigger: {}", cron);
-            String timeZone = (String)triggerParameters.get("timeZone");
+            String timeZone = (String)copy.get("timeZone");
             if (timeZone != null) {
                 if (ObjectHelper.isNotEmpty(customCalendar)) {
                     result = TriggerBuilder.newTrigger()
@@ -491,20 +494,20 @@ public class QuartzEndpoint extends DefaultEndpoint {
         } else {
             LOG.debug("Creating SimpleTrigger.");
             int repeat = SimpleTrigger.REPEAT_INDEFINITELY;
-            String repeatString = (String) triggerParameters.get("repeatCount");
+            String repeatString = (String) copy.get("repeatCount");
             if (repeatString != null) {
                 repeat = EndpointHelper.resolveParameter(getCamelContext(), repeatString, Integer.class);
                 // need to update the parameters
-                triggerParameters.put("repeatCount", repeat);
+                copy.put("repeatCount", repeat);
             }
 
             // default use 1 sec interval
             long interval = 1000;
-            String intervalString = (String) triggerParameters.get("repeatInterval");
+            String intervalString = (String) copy.get("repeatInterval");
             if (intervalString != null) {
                 interval = EndpointHelper.resolveParameter(getCamelContext(), intervalString, Long.class);
                 // need to update the parameters
-                triggerParameters.put("repeatInterval", interval);
+                copy.put("repeatInterval", interval);
             }
             TriggerBuilder<SimpleTrigger> triggerBuilder;
             if (ObjectHelper.isNotEmpty(customCalendar)) {
@@ -533,9 +536,9 @@ public class QuartzEndpoint extends DefaultEndpoint {
             jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_SIMPLE_REPEAT_INTERVAL, interval);
         }
 
-        if (triggerParameters != null && triggerParameters.size() > 0) {
-            LOG.debug("Setting user extra triggerParameters {}", triggerParameters);
-            setProperties(result, triggerParameters);
+        if (copy != null && copy.size() > 0) {
+            LOG.debug("Setting user extra triggerParameters {}", copy);
+            setProperties(result, copy);
         }
 
         LOG.debug("Created trigger={}", result);
@@ -563,8 +566,10 @@ public class QuartzEndpoint extends DefaultEndpoint {
 
         // Let user parameters to further set JobDetail properties.
         if (jobParameters != null && jobParameters.size() > 0) {
-            LOG.debug("Setting user extra jobParameters {}", jobParameters);
-            setProperties(result, jobParameters);
+            // need to use a copy to keep the parameters on the endpoint
+            Map<String, Object> copy = new HashMap<>(jobParameters);
+            LOG.debug("Setting user extra jobParameters {}", copy);
+            setProperties(result, copy);
         }
 
         LOG.debug("Created jobDetail={}", result);
diff --git a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTriggerParametersTest.java b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTriggerParametersTest.java
new file mode 100644
index 0000000..9d474c1
--- /dev/null
+++ b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTriggerParametersTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.Map;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class QuartzTriggerParametersTest extends BaseQuartzTest {
+
+    @Test
+    public void testTriggerParameters() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Europe/Berlin");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                String cron = String.format("quartz://job?cron=0+%s+%s+?+*+*&trigger.timeZone=%s&trigger.misfireInstruction=2", 59, 23, "Europe/Berlin");
+
+                from(cron).to("mock:cron");
+
+                from("timer://foo?repeatCount=1")
+                        .process(exchange -> {
+                            QuartzEndpoint endPoint = getContext().getEndpoint(cron, QuartzEndpoint.class);
+                            Map<String, Object> triggers = endPoint.getTriggerParameters();
+                            exchange.getIn().setBody(triggers.get("timeZone"));
+                        })
+                        .to("mock:result");
+            }
+        };
+    }
+
+}