-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #222: initial implementation of native cron support #223
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
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. | ||
|
||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.apache.camel.k</groupId> | ||
<artifactId>camel-k-runtime-parent</artifactId> | ||
<version>1.0.10-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>camel-k-runtime-cron</artifactId> | ||
|
||
<dependencies> | ||
|
||
<!-- ****************************** --> | ||
<!-- --> | ||
<!-- RUNTIME --> | ||
<!-- --> | ||
<!-- ****************************** --> | ||
|
||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-core-engine</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-timer</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel.k</groupId> | ||
<artifactId>camel-k-runtime-core</artifactId> | ||
</dependency> | ||
|
||
<!-- ****************************** --> | ||
<!-- --> | ||
<!-- TESTS --> | ||
<!-- --> | ||
<!-- ****************************** --> | ||
|
||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-endpointdsl</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-log</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel</groupId> | ||
<artifactId>camel-main</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel.k</groupId> | ||
<artifactId>camel-k-loader-js</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.camel.k</groupId> | ||
<artifactId>camel-k-runtime-main</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>${log4j2.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>${log4j2.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jboss.jandex</groupId> | ||
<artifactId>jandex-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>make-index</id> | ||
<goals> | ||
<goal>jandex</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
59 changes: 59 additions & 0 deletions
59
camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRoutePolicyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.Exchange; | ||
import org.apache.camel.NamedNode; | ||
import org.apache.camel.Route; | ||
import org.apache.camel.spi.RoutePolicy; | ||
import org.apache.camel.spi.RoutePolicyFactory; | ||
import org.apache.camel.support.RoutePolicySupport; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A RoutePolicyFactory that shuts the context down after the first exchange has been done. | ||
*/ | ||
public class CronRoutePolicyFactory implements RoutePolicyFactory { | ||
|
||
public CronRoutePolicyFactory() { | ||
} | ||
|
||
@Override | ||
public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, NamedNode route) { | ||
return new CronRoutePolicy(camelContext); | ||
} | ||
|
||
private static class CronRoutePolicy extends RoutePolicySupport { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CronRoutePolicy.class); | ||
|
||
private final CamelContext context; | ||
|
||
public CronRoutePolicy(CamelContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void onExchangeDone(Route route, Exchange exchange) { | ||
LOG.info("Context shutdown started by cron policy"); | ||
context.getExecutorServiceManager().newThread("terminator", context::stop).start(); | ||
} | ||
|
||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRuntimeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...l-k-runtime-cron/src/main/resources/META-INF/services/org.apache.camel.k.Runtime$Listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
org.apache.camel.k.cron.CronRuntimeListener |
18 changes: 18 additions & 0 deletions
18
...ntime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-quartz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
class=org.apache.camel.k.cron.CronQuartzContextCustomizer |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to add a new method
stop()
to theRuntime
and invoke such method here instead ofCamelContext::stop
so the standard runtime would invokeMain::stop
and the Quarkus one will invoke the correct method once provided by quarkusThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated it