Skip to content

Commit

Permalink
Fix apache#222: second attempt using runtime listener
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Jan 9, 2020
1 parent 49cd31c commit 7538b7a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 127 deletions.
6 changes: 0 additions & 6 deletions camel-k-runtime-cron/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# limitations under the License.
#

class=org.apache.camel.k.cron.CronTimerContextCustomizer
org.apache.camel.k.cron.CronRuntimeListener
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -70,8 +72,9 @@ public void onContextStop(CamelContext context) {

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")
);
}

Expand Down

0 comments on commit 7538b7a

Please sign in to comment.