Fix #222: second attempt using runtime listener
diff --git a/camel-k-runtime-cron/pom.xml b/camel-k-runtime-cron/pom.xml
index b9611f1..43d30e3 100644
--- a/camel-k-runtime-cron/pom.xml
+++ b/camel-k-runtime-cron/pom.xml
@@ -44,12 +44,6 @@
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-timer</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.apache.camel</groupId>
- <artifactId>camel-quartz</artifactId>
- <optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.camel.k</groupId>
diff --git a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronQuartzContextCustomizer.java b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronQuartzContextCustomizer.java
deleted file mode 100644
index ad5a55f..0000000
--- a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronQuartzContextCustomizer.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.k.cron;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.Endpoint;
-import org.apache.camel.component.quartz.QuartzEndpoint;
-import org.apache.camel.k.ContextCustomizer;
-import org.apache.camel.support.LifecycleStrategySupport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class CronQuartzContextCustomizer implements ContextCustomizer {
-
- public CronQuartzContextCustomizer() {
- }
-
- @Override
- public void apply(CamelContext camelContext) {
- camelContext.addLifecycleStrategy(new CronQuartzLifecycleStrategy());
- camelContext.addRoutePolicyFactory(new CronRoutePolicyFactory());
- }
-
- static class CronQuartzLifecycleStrategy extends LifecycleStrategySupport {
-
- private static final Logger LOG = LoggerFactory.getLogger(CronQuartzLifecycleStrategy.class);
-
- @Override
- public void onEndpointAdd(Endpoint endpoint) {
- if (endpoint instanceof QuartzEndpoint) {
- LOG.info("Cron policy is configuring the quartz endpoint to startup immediately");
- QuartzEndpoint qe = (QuartzEndpoint) endpoint;
- qe.setCron(null);
- qe.setFireNow(true);
- qe.setAutoStartScheduler(true);
- qe.setCustomCalendar(null);
- qe.setStartDelayedSeconds(0);
- }
- }
- }
-}
diff --git a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRuntimeListener.java b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRuntimeListener.java
new file mode 100644
index 0000000..43a5212
--- /dev/null
+++ b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRuntimeListener.java
@@ -0,0 +1,82 @@
+/*
+ * 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.k.cron;
+
+import java.util.List;
+
+import org.apache.camel.k.Runtime;
+import org.apache.camel.k.listener.AbstractPhaseListener;
+import org.apache.camel.k.listener.RoutesConfigurer;
+import org.apache.camel.model.Model;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CronRuntimeListener extends AbstractPhaseListener {
+ private static final Logger LOGGER = LoggerFactory.getLogger(RoutesConfigurer.class);
+
+ private static final String ENV_CAMEL_K_CRON_OVERRIDE = "CAMEL_K_CRON_OVERRIDE";
+ private static final String PROPERTY_CAMEL_K_CRON_OVERRIDE = "camel.k.cron.override";
+
+ public CronRuntimeListener() {
+ super(Runtime.Phase.ConfigureContext);
+ }
+
+ @Override
+ protected void accept(Runtime runtime) {
+ String components = System.getProperty(PROPERTY_CAMEL_K_CRON_OVERRIDE);
+
+ if (ObjectHelper.isEmpty(components)) {
+ components = System.getenv(ENV_CAMEL_K_CRON_OVERRIDE);
+ }
+
+ if (ObjectHelper.isEmpty(components)) {
+ LOGGER.warn("No components to override found in {} environment variable", ENV_CAMEL_K_CRON_OVERRIDE);
+ return;
+ }
+
+ // Add the cron route policy if there's at least one component to override
+ runtime.getCamelContext().addRoutePolicyFactory(new CronRoutePolicyFactory());
+
+ // Override components
+ overrideCron(runtime, components.split(",", -1));
+ }
+
+ protected void overrideCron(Runtime runtime, String[] components) {
+ List<RouteDefinition> definitions = runtime.getCamelContext().getExtension(Model.class).getRouteDefinitions();
+ for (RouteDefinition def : definitions) {
+ String uri = def.getInput() != null ? def.getInput().getUri() : null;
+ if (shouldBeOverridden(uri, components)) {
+ def.getInput().setUri("timer:camel-k-cron-override?delay=0&period=1&repeatCount=1");
+ }
+ }
+ }
+
+ protected boolean shouldBeOverridden(String uri, String[] components) {
+ if (uri == null) {
+ return false;
+ }
+ for (String c : components) {
+ if (uri.startsWith(c + ":")) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronTimerContextCustomizer.java b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronTimerContextCustomizer.java
deleted file mode 100644
index 19ca64f..0000000
--- a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronTimerContextCustomizer.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.k.cron;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.Endpoint;
-import org.apache.camel.component.timer.TimerEndpoint;
-import org.apache.camel.k.ContextCustomizer;
-import org.apache.camel.support.LifecycleStrategySupport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class CronTimerContextCustomizer implements ContextCustomizer {
-
- public CronTimerContextCustomizer() {
- }
-
- @Override
- public void apply(CamelContext camelContext) {
- camelContext.addLifecycleStrategy(new CronTimerLifecycleStrategy());
- camelContext.addRoutePolicyFactory(new CronRoutePolicyFactory());
- }
-
- static class CronTimerLifecycleStrategy extends LifecycleStrategySupport {
-
- private static final Logger LOG = LoggerFactory.getLogger(CronTimerLifecycleStrategy.class);
-
- @Override
- public void onEndpointAdd(Endpoint endpoint) {
- if (endpoint instanceof TimerEndpoint) {
- LOG.info("Cron policy is configuring the timer endpoint to startup immediately");
- TimerEndpoint te = (TimerEndpoint)endpoint;
- te.setDelay(0);
- te.setPeriod(1);
- te.setRepeatCount(1);
- te.setTime(null);
- te.setFixedRate(false);
- te.setPattern(null);
- }
- }
- }
-}
diff --git a/camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-timer b/camel-k-runtime-cron/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener
similarity index 92%
rename from camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-timer
rename to camel-k-runtime-cron/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener
index a29f31c..f4fb992 100644
--- a/camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-timer
+++ b/camel-k-runtime-cron/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener
@@ -15,4 +15,4 @@
# limitations under the License.
#
-class=org.apache.camel.k.cron.CronTimerContextCustomizer
+org.apache.camel.k.cron.CronRuntimeListener
diff --git a/camel-k-runtime-cron/src/test/java/org/apache/camel/k/cron/CronTest.java b/camel-k-runtime-cron/src/test/java/org/apache/camel/k/cron/CronTest.java
index d0b616f..fc95d27 100644
--- a/camel-k-runtime-cron/src/test/java/org/apache/camel/k/cron/CronTest.java
+++ b/camel-k-runtime-cron/src/test/java/org/apache/camel/k/cron/CronTest.java
@@ -16,7 +16,6 @@
*/
package org.apache.camel.k.cron;
-import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@@ -25,10 +24,9 @@
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.k.listener.ContextConfigurer;
import org.apache.camel.k.listener.RoutesConfigurer;
-import org.apache.camel.k.loader.js.JavaScriptSourceLoader;
import org.apache.camel.k.main.ApplicationRuntime;
import org.apache.camel.support.LifecycleStrategySupport;
-import org.junit.jupiter.api.Test;
+import org.junit.After;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@@ -37,16 +35,20 @@
public class CronTest {
+ @After
+ public void unsetSystemProperties() {
+ System.clearProperty("camel.k.cron.override");
+ }
+
@ParameterizedTest
@MethodSource("parameters")
- public void testCronTimerActivation(String routes, String customizer) throws Exception {
+ public void testCronTimerActivation(String routes, String cronOverride) throws Exception {
ApplicationRuntime runtime = new ApplicationRuntime();
runtime.addListener(RoutesConfigurer.forRoutes(routes));
runtime.addListener(new ContextConfigurer());
+ runtime.addListener(new CronRuntimeListener());
- Properties properties = new Properties();
- properties.setProperty("customizer." + customizer + ".enabled", "true");
- runtime.setProperties(properties);
+ System.setProperty("camel.k.cron.override", cronOverride);
// To check auto-termination of Camel context
CountDownLatch termination = new CountDownLatch(1);
@@ -70,8 +72,9 @@
static Stream<Arguments> parameters() {
return Stream.of(
- Arguments.arguments("classpath:routes-timer.js", "cron-timer"),
- Arguments.arguments("classpath:routes-quartz.js", "cron-quartz")
+ Arguments.arguments("classpath:routes-timer.js", "timer"),
+ Arguments.arguments("classpath:routes-quartz.js", "quartz"),
+ Arguments.arguments("classpath:routes-quartz.js", "timer,quartz")
);
}