Repository: camel Updated Branches: refs/heads/camel-2.15.x 8a0cd0d0f -> cb0935e3a
CAMEL-8950 - make CamelContext available from injected schedulers too Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cb0935e3 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cb0935e3 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cb0935e3 Branch: refs/heads/camel-2.15.x Commit: cb0935e3af05b333b5c85a4fb3b1846836218f11 Parents: 8a0cd0d Author: Jonathan Anstey <jans...@gmail.com> Authored: Fri Jul 10 11:46:55 2015 -0230 Committer: Jonathan Anstey <jans...@gmail.com> Committed: Fri Jul 10 11:47:18 2015 -0230 ---------------------------------------------------------------------- .../component/quartz2/QuartzComponent.java | 21 +++-- ...omponentCamelContextSharedSchedulerTest.java | 94 ++++++++++++++++++++ 2 files changed, 109 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cb0935e3/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzComponent.java ---------------------------------------------------------------------- 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 a6cab8f..4ac630f 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 @@ -320,11 +320,7 @@ public class QuartzComponent extends UriEndpointComponent implements StartupList LOG.info("Create and initializing scheduler."); scheduler = createScheduler(); - // Store CamelContext into QuartzContext space - SchedulerContext quartzContext = scheduler.getContext(); - String camelContextName = QuartzHelper.getQuartzContextName(getCamelContext()); - LOG.debug("Storing camelContextName={} into Quartz Context space.", camelContextName); - quartzContext.put(QuartzConstants.QUARTZ_CAMEL_CONTEXT + "-" + camelContextName, getCamelContext()); + SchedulerContext quartzContext = storeCamelContextInQuartzContext(); // Set camel job counts to zero. We needed this to prevent shutdown in case there are multiple Camel contexts // that has not completed yet, and the last one with job counts to zero will eventually shutdown. @@ -335,6 +331,15 @@ public class QuartzComponent extends UriEndpointComponent implements StartupList } } + private SchedulerContext storeCamelContextInQuartzContext() throws SchedulerException { + // Store CamelContext into QuartzContext space + SchedulerContext quartzContext = scheduler.getContext(); + String camelContextName = QuartzHelper.getQuartzContextName(getCamelContext()); + LOG.debug("Storing camelContextName={} into Quartz Context space.", camelContextName); + quartzContext.put(QuartzConstants.QUARTZ_CAMEL_CONTEXT + "-" + camelContextName, getCamelContext()); + return quartzContext; + } + private Scheduler createScheduler() throws SchedulerException { return getSchedulerFactory().getScheduler(); } @@ -361,8 +366,12 @@ public class QuartzComponent extends UriEndpointComponent implements StartupList // 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 + // 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."); http://git-wip-us.apache.org/repos/asf/camel/blob/cb0935e3/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzComponentCamelContextSharedSchedulerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzComponentCamelContextSharedSchedulerTest.java b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzComponentCamelContextSharedSchedulerTest.java new file mode 100644 index 0000000..a4b0301 --- /dev/null +++ b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzComponentCamelContextSharedSchedulerTest.java @@ -0,0 +1,94 @@ +/** + * 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.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.quartz.CronTrigger; +import org.quartz.JobDetail; +import org.quartz.Scheduler; +import org.quartz.Trigger; + +public class QuartzComponentCamelContextSharedSchedulerTest { + + private DefaultCamelContext camel1; + private DefaultCamelContext camel2; + + @Before + public void setUp() throws Exception { + camel1 = new DefaultCamelContext(); + camel1.setName("camel-1"); + camel1.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("quartz2://myGroup/myTimerName?cron=0/2+*+*+*+*+?").to("mock:one"); + } + }); + camel1.start(); + + camel2 = new DefaultCamelContext(); + camel2.setName("camel-2"); + + Scheduler camel1Scheduler = camel1.getComponent("quartz2", QuartzComponent.class).getScheduler(); + QuartzComponent camel2QuartzComponent = camel2.getComponent("quartz2", QuartzComponent.class); + camel2QuartzComponent.setScheduler(camel1Scheduler); + + camel2.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("quartz2://myOtherGroup/myOtherTimerName?cron=0/1+*+*+*+*+?").to("mock:two"); + } + }); + + camel2.start(); + } + + @After + public void tearDown() throws Exception { + camel1.stop(); + camel2.stop(); + } + + @Test + public void testTwoCamelContext() throws Exception { + MockEndpoint mock1 = camel1.getEndpoint("mock:one", MockEndpoint.class); + mock1.expectedMinimumMessageCount(2); + + MockEndpoint mock2 = camel2.getEndpoint("mock:two", MockEndpoint.class); + mock2.expectedMinimumMessageCount(6); + mock1.assertIsSatisfied(); + + JobDetail detail = mock1.getReceivedExchanges().get(0).getIn().getHeader("jobDetail", JobDetail.class); + Assert.assertThat(detail.getJobDataMap().get(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION).equals("0/2 * * * * ?"), CoreMatchers.is(true)); + + camel1.stop(); + + mock2.assertIsSatisfied(); + + detail = mock2.getReceivedExchanges().get(0).getIn().getHeader("jobDetail", JobDetail.class); + Assert.assertThat(detail.getJobDataMap().get(QuartzConstants.QUARTZ_TRIGGER_CRON_EXPRESSION).equals("0/1 * * * * ?"), CoreMatchers.is(true)); + + camel2.stop(); + } +}