From d7bf0e788d4a9f749c7ec4b1f97dfb8f9446fadd Mon Sep 17 00:00:00 2001 From: Denis Delangle Date: Tue, 22 Jan 2013 15:10:37 +0100 Subject: [PATCH 1/2] FIX #CAMEL-5993 : put endpoint in quartz job map as we can't be sure endpoint is present in camelContext endpoints --- .../camel/component/quartz/CamelJob.java | 3 +- .../component/quartz/QuartzComponent.java | 37 ++++--- .../component/quartz/QuartzConstants.java | 1 + .../component/quartz/QuartzEndpoint.java | 2 + .../QuartzCronRouteWithSmallCacheTest.java | 104 ++++++++++++++++++ .../quartz/QuartzNameCollisionTest.java | 16 +++ 6 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java diff --git a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java index ffc4599049ca2..9a0511b24cc3a 100644 --- a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java +++ b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java @@ -35,6 +35,7 @@ public class CamelJob implements Job, Serializable { public void execute(JobExecutionContext context) throws JobExecutionException { String camelContextName = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME); String endpointUri = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_ENDPOINT_URI); + SchedulerContext schedulerContext; try { @@ -47,7 +48,7 @@ public void execute(JobExecutionContext context) throws JobExecutionException { if (camelContext == null) { throw new JobExecutionException("No CamelContext could be found with name: " + camelContextName); } - QuartzEndpoint endpoint = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class); + QuartzEndpoint endpoint = (QuartzEndpoint) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_ENDPOINT); if (endpoint == null) { throw new JobExecutionException("No QuartzEndpoint could be found with uri: " + endpointUri); } diff --git a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java index 901d91bb6ab8e..c5a120f2320a2 100644 --- a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java +++ b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java @@ -127,18 +127,10 @@ protected QuartzEndpoint createEndpoint(final String uri, final String remaining Trigger trigger; boolean stateful = "true".equals(parameters.get("stateful")); + QuartzEndpoint answer = new QuartzEndpoint(uri, this); + setProperties(answer.getJobDetail(), jobParameters); - // if we're starting up and not running in Quartz clustered mode or not stateful then check for a name conflict. - if (!isClustered() && !stateful) { - // check to see if this trigger already exists - trigger = getScheduler().getTrigger(name, group); - if (trigger != null) { - String msg = "A Quartz job already exists with the name/group: " + name + "/" + group; - throw new IllegalArgumentException(msg); - } - } - - // create the trigger either cron or simple + // create the trigger either cron or simple if (ObjectHelper.isNotEmpty(cron)) { trigger = createCronTrigger(cron); } else { @@ -151,13 +143,25 @@ protected QuartzEndpoint createEndpoint(final String uri, final String remaining } } } - - QuartzEndpoint answer = new QuartzEndpoint(uri, this); - setProperties(answer.getJobDetail(), jobParameters); - setProperties(trigger, triggerParameters); trigger.setName(name); trigger.setGroup(group); + + // if we're starting up and not running in Quartz clustered mode or not stateful then check for a name conflict. + if (!isClustered() && !stateful) { + // check to see if this trigger already exists + Trigger oldTrigger = getScheduler().getTrigger(name, group); + if (oldTrigger != null + && hasTriggerChanged(oldTrigger, trigger) + ) { + String msg = "A Quartz job already exists with the name/group: " + name + "/" + group; + throw new IllegalArgumentException(msg); + } + if(oldTrigger != null) { + trigger = oldTrigger; + } + } + answer.setTrigger(trigger); return answer; @@ -255,6 +259,9 @@ private void doAddJob(JobDetail job, Trigger trigger) throws SchedulerException private boolean hasTriggerChanged(Trigger oldTrigger, Trigger newTrigger) { if (oldTrigger instanceof CronTrigger && oldTrigger.equals(newTrigger)) { + if(! (newTrigger instanceof CronTrigger)) { + return true; + } CronTrigger oldCron = (CronTrigger) oldTrigger; CronTrigger newCron = (CronTrigger) newTrigger; return !oldCron.getCronExpression().equals(newCron.getCronExpression()); diff --git a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzConstants.java b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzConstants.java index 725f4ad255d95..408f91ace3452 100644 --- a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzConstants.java +++ b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzConstants.java @@ -22,6 +22,7 @@ public final class QuartzConstants { public static final String QUARTZ_ENDPOINT_URI = "CamelQuartzEndpoint"; + public static final String QUARTZ_ENDPOINT = "CamelQuartzEndpointObject"; // Note: using the CamelContext management name to ensure its unique in the JVM public static final String QUARTZ_CAMEL_CONTEXT_NAME = "CamelQuartzCamelContextName"; 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 78099747ebb46..dfef2d96f40aa 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 @@ -72,6 +72,8 @@ public void addTrigger(final Trigger trigger, final JobDetail detail) throws Sch detail.getJobDataMap().put(QuartzConstants.QUARTZ_ENDPOINT_URI, getEndpointUri()); // must use management name as it should be unique in the same JVM detail.getJobDataMap().put(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME, getCamelContext().getManagementName()); + detail.getJobDataMap().put(QuartzConstants.QUARTZ_ENDPOINT, this); + if (detail.getJobClass() == null) { detail.setJobClass(isStateful() ? StatefulCamelJob.class : CamelJob.class); } diff --git a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java new file mode 100644 index 0000000000000..d1fdda4f90de6 --- /dev/null +++ b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java @@ -0,0 +1,104 @@ +/** + * 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.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * Endpoints are stored in a LRU list with a default capacity of 1000. If the list is full, + * then endpoints are removed and should be recreated. + * + * We simulate this behavior with a capacity of 1 element. + * + * @version + */ +public class QuartzCronRouteWithSmallCacheTest extends CamelTestSupport { + + private List list = new ArrayList(); + + @Test + public void testQuartzCronRouteWithSmallCache() throws Exception { + synchronized (this) { + for(int i = 0; i< 100; i++) { + wait(100); + if(list.size() > 4) { + break; + } + + } + } + + assertTrue("Cron was triggered only " + list.size() + " times", list.size() > 4); + } + + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = new DefaultCamelContext(){ + @Override + public Map getProperties() { + Map res = super.getProperties(); + res.put(Exchange.MAXIMUM_ENDPOINT_CACHE_SIZE, "1"); + return res; + } + }; + + return context; + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + // START SNIPPET: e1 + // triggers every 2th second at precise 00,02,04,06..58 + // notice we must use + as space when configured using URI parameter + try { + from("quartz://myGroup/myTimerName?cron=0/2+*+*+*+*+?").process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + context().createProducerTemplate().send("direct:a", exchange); + context().createProducerTemplate().send("direct:a", exchange); + list.add(new Object()); + } + }); + from("direct:a").process(new Processor() { + + @Override + public void process(Exchange arg0) throws Exception { + } + }); + } catch(Exception e) { + e.printStackTrace(); + } + // END SNIPPET: e1 + } + }; + } +} \ No newline at end of file diff --git a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java index df002f9242695..d0cbab25b8b28 100644 --- a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java +++ b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzNameCollisionTest.java @@ -51,6 +51,22 @@ public void configure() throws Exception { Assert.assertEquals("A Quartz job already exists with the name/group: myTimerName/myGroup", e.getMessage()); } } + + @Test + public void testDupeNameSameExpression() throws Exception { + camel1 = new DefaultCamelContext(); + camel1.setName("camel-1"); + camel1.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("quartz://myGroup/myTimerName?cron=0/1+*+*+*+*+?").to("log:one", "mock:one"); + } + }); + camel1.start(); + + QuartzComponent component2 = new QuartzComponent(camel1); + component2.createEndpoint("quartz://myGroup/myTimerName?cron=0/1+*+*+*+*+?"); + } @Test public void testDupeNameMultiContext() throws Exception { From 623933c111d050abffb0871200a4e01a2ea4692c Mon Sep 17 00:00:00 2001 From: Denis Delangle Date: Wed, 23 Jan 2013 14:45:39 +0100 Subject: [PATCH 2/2] CAMEL-5993 : based on patch in CAMEL-5994 + unit test : avoid depending on LRU cache --- .../camel/component/quartz/CamelJob.java | 30 ++++- .../QuartzCronRouteWithSmallCacheTest.java | 104 ++++++++++++++++++ 2 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java diff --git a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java index ffc4599049ca2..9a0e5bba9181b 100644 --- a/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java +++ b/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/CamelJob.java @@ -19,22 +19,25 @@ import java.io.Serializable; import org.apache.camel.CamelContext; +import org.apache.camel.Route; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.SchedulerContext; import org.quartz.SchedulerException; +import static org.apache.camel.util.URISupport.normalizeUri; + /** * @version */ public class CamelJob implements Job, Serializable { - private static final long serialVersionUID = 26L; + private static final long serialVersionUID = 27L; public void execute(JobExecutionContext context) throws JobExecutionException { String camelContextName = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME); - String endpointUri = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_ENDPOINT_URI); + String expectedQuartzEndpointUri = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_ENDPOINT_URI); SchedulerContext schedulerContext; try { @@ -47,11 +50,26 @@ public void execute(JobExecutionContext context) throws JobExecutionException { if (camelContext == null) { throw new JobExecutionException("No CamelContext could be found with name: " + camelContextName); } - QuartzEndpoint endpoint = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class); - if (endpoint == null) { - throw new JobExecutionException("No QuartzEndpoint could be found with uri: " + endpointUri); + + getExpectedQuartzEndpoint(expectedQuartzEndpointUri, camelContext).onJobExecute(context); + } + + private QuartzEndpoint getExpectedQuartzEndpoint(String expectedQuartzEndpointUri, CamelContext camelContext) throws JobExecutionException { + try { + for (Route route : camelContext.getRoutes()) { + if (route.getEndpoint() instanceof QuartzEndpoint) { + QuartzEndpoint quartzEndpoint = (QuartzEndpoint)route.getEndpoint(); + if (normalizeUri(quartzEndpoint.getEndpointUri()).equals(normalizeUri(expectedQuartzEndpointUri)) && quartzEndpoint.isStarted()) { + return quartzEndpoint; + } + } + } + } + catch (Exception e) { + throw new JobExecutionException(e); } - endpoint.onJobExecute(context); + + throw new JobExecutionException("No QuartzEndpoint could be found with uri: " + expectedQuartzEndpointUri); } } \ No newline at end of file diff --git a/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java new file mode 100644 index 0000000000000..d1fdda4f90de6 --- /dev/null +++ b/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzCronRouteWithSmallCacheTest.java @@ -0,0 +1,104 @@ +/** + * 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.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +/** + * Endpoints are stored in a LRU list with a default capacity of 1000. If the list is full, + * then endpoints are removed and should be recreated. + * + * We simulate this behavior with a capacity of 1 element. + * + * @version + */ +public class QuartzCronRouteWithSmallCacheTest extends CamelTestSupport { + + private List list = new ArrayList(); + + @Test + public void testQuartzCronRouteWithSmallCache() throws Exception { + synchronized (this) { + for(int i = 0; i< 100; i++) { + wait(100); + if(list.size() > 4) { + break; + } + + } + } + + assertTrue("Cron was triggered only " + list.size() + " times", list.size() > 4); + } + + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = new DefaultCamelContext(){ + @Override + public Map getProperties() { + Map res = super.getProperties(); + res.put(Exchange.MAXIMUM_ENDPOINT_CACHE_SIZE, "1"); + return res; + } + }; + + return context; + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + // START SNIPPET: e1 + // triggers every 2th second at precise 00,02,04,06..58 + // notice we must use + as space when configured using URI parameter + try { + from("quartz://myGroup/myTimerName?cron=0/2+*+*+*+*+?").process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + context().createProducerTemplate().send("direct:a", exchange); + context().createProducerTemplate().send("direct:a", exchange); + list.add(new Object()); + } + }); + from("direct:a").process(new Processor() { + + @Override + public void process(Exchange arg0) throws Exception { + } + }); + } catch(Exception e) { + e.printStackTrace(); + } + // END SNIPPET: e1 + } + }; + } +} \ No newline at end of file