You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2015/07/20 21:10:53 UTC
camel git commit: CAMEL-8988 - can't manually trigger a quartz2 job
Repository: camel
Updated Branches:
refs/heads/master a93800e6e -> 32b5c8923
CAMEL-8988 - can't manually trigger a quartz2 job
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/32b5c892
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/32b5c892
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/32b5c892
Branch: refs/heads/master
Commit: 32b5c8923e6207e987b1234ef795c498254852e5
Parents: a93800e
Author: Jonathan Anstey <ja...@gmail.com>
Authored: Mon Jul 20 16:39:10 2015 -0230
Committer: Jonathan Anstey <ja...@gmail.com>
Committed: Mon Jul 20 16:40:00 2015 -0230
----------------------------------------------------------------------
.../camel/component/quartz2/CamelJob.java | 19 ++++++
.../quartz2/QuartzManuallyTriggerJobTest.java | 66 ++++++++++++++++++++
2 files changed, 85 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/32b5c892/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/CamelJob.java
----------------------------------------------------------------------
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 3e74abc..ff97347 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
@@ -16,12 +16,16 @@
*/
package org.apache.camel.component.quartz2;
+import java.util.Collection;
+import java.util.Map;
+
import org.apache.camel.CamelContext;
import org.apache.camel.CamelExchangeException;
import org.apache.camel.DelegateEndpoint;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Route;
+import org.apache.camel.util.EndpointHelper;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
@@ -121,6 +125,7 @@ public class CamelJob implements Job {
// fallback and lookup existing from registry (eg maybe a @Consume POJO with a quartz endpoint, and thus not from a route)
String endpointUri = quartzContext.getMergedJobDataMap().getString(QuartzConstants.QUARTZ_ENDPOINT_URI);
+
QuartzEndpoint result = null;
// Even though the same camelContext.getEndpoint call, but if/else display different log.
@@ -129,6 +134,10 @@ public class CamelJob implements Job {
LOG.debug("Getting Endpoint from camelContext.");
}
result = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
+ } else if ((result = searchForEndpointMatch(camelContext, endpointUri)) != null) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Found match for endpoint URI = " + endpointUri + " by searching endpoint list.");
+ }
} else {
LOG.warn("Cannot find existing QuartzEndpoint with uri: {}. Creating new endpoint instance.", endpointUri);
result = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
@@ -139,4 +148,14 @@ public class CamelJob implements Job {
return result;
}
+
+ protected QuartzEndpoint searchForEndpointMatch(CamelContext camelContext, String endpointUri) {
+ Collection<Endpoint> endpoints = camelContext.getEndpoints();
+ for (Endpoint endpoint : endpoints) {
+ if (endpointUri.equals(endpoint.getEndpointUri())) {
+ return (QuartzEndpoint) endpoint;
+ }
+ }
+ return null;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/32b5c892/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzManuallyTriggerJobTest.java
----------------------------------------------------------------------
diff --git a/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzManuallyTriggerJobTest.java b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzManuallyTriggerJobTest.java
new file mode 100644
index 0000000..4b8991b
--- /dev/null
+++ b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzManuallyTriggerJobTest.java
@@ -0,0 +1,66 @@
+/**
+ * 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 java.util.ArrayList;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+import org.quartz.JobDataMap;
+import org.quartz.JobKey;
+import org.quartz.Scheduler;
+import org.quartz.impl.matchers.GroupMatcher;
+
+/**
+ * This test the CronTrigger as a timer endpoint in a route.
+ * @version
+ */
+public class QuartzManuallyTriggerJobTest extends BaseQuartzTest {
+
+ @Test
+ public void testQuartzCronRoute() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMinimumMessageCount(1);
+
+ QuartzComponent component = context.getComponent("quartz2", QuartzComponent.class);
+ Scheduler scheduler = component.getScheduler();
+
+ // collect all jobKeys of this route (ideally only one).
+ ArrayList<JobKey> jobKeys = new ArrayList<JobKey>();
+ for (String group : scheduler.getJobGroupNames()) {
+ for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(group))) {
+ jobKeys.add(jobKey);
+ }
+ }
+
+ JobDataMap jobDataMap = scheduler.getJobDetail(jobKeys.get(0)).getJobDataMap();
+
+ // trigger job manually
+ scheduler.triggerJob(jobKeys.get(0), jobDataMap);
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("quartz2://MyTimer?cron=05+00+00+*+*+?").to("mock:result");
+ }
+ };
+ }
+}
\ No newline at end of file