Skip to content

Commit

Permalink
Fix apache#222: shutting down the runtime instead of the Camel context
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Jan 13, 2020
1 parent 7538b7a commit 6dc1007
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ default void addConfiguration(Object configuration) {
throw new UnsupportedOperationException();
}

/**
* Lifecycle method used to stops the entire integration.
*/
default void stop() throws Exception {
// Stopping the Camel context in default config is enough to tear down the integration
getCamelContext().stop();
}

enum Phase {
Starting,
ConfigureContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.camel.Exchange;
import org.apache.camel.NamedNode;
import org.apache.camel.Route;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.k.Runtime;
import org.apache.camel.spi.RoutePolicy;
import org.apache.camel.spi.RoutePolicyFactory;
import org.apache.camel.support.RoutePolicySupport;
Expand All @@ -31,17 +33,20 @@
*/
public class CronRoutePolicyFactory implements RoutePolicyFactory {

public CronRoutePolicyFactory() {
private Runtime runtime;

public CronRoutePolicyFactory(Runtime runtime) {
this.runtime = runtime;
}

@Override
public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, NamedNode route) {
return new CronRoutePolicy(camelContext);
}

private static class CronRoutePolicy extends RoutePolicySupport {
private class CronRoutePolicy extends RoutePolicySupport {

private static final Logger LOG = LoggerFactory.getLogger(CronRoutePolicy.class);
private final Logger logger = LoggerFactory.getLogger(CronRoutePolicy.class);

private final CamelContext context;

Expand All @@ -51,8 +56,16 @@ public CronRoutePolicy(CamelContext context) {

@Override
public void onExchangeDone(Route route, Exchange exchange) {
LOG.info("Context shutdown started by cron policy");
context.getExecutorServiceManager().newThread("terminator", context::stop).start();
logger.info("Context shutdown started by cron policy");
context.getExecutorServiceManager().newThread("terminator", this::stopRuntime).start();
}

private void stopRuntime() {
try {
runtime.stop();
} catch (Exception e) {
throw new RuntimeCamelException(e);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected void accept(Runtime runtime) {
}

// Add the cron route policy if there's at least one component to override
runtime.getCamelContext().addRoutePolicyFactory(new CronRoutePolicyFactory());
runtime.getCamelContext().addRoutePolicyFactory(new CronRoutePolicyFactory(runtime));

// Override components
overrideCron(runtime, components.split(",", -1));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public void run() throws Exception {
this.main.run();
}

public void stop()throws Exception {
@Override
public void stop() throws Exception {
this.main.stop();
}

Expand Down

0 comments on commit 6dc1007

Please sign in to comment.