From 6dc1007d1dc30ff15bdabe65d0ee45935640f3f7 Mon Sep 17 00:00:00 2001 From: Nicola Ferraro Date: Mon, 13 Jan 2020 16:18:51 +0100 Subject: [PATCH] Fix #222: shutting down the runtime instead of the Camel context --- .../main/java/org/apache/camel/k/Runtime.java | 8 +++++++ .../camel/k/cron/CronRoutePolicyFactory.java | 23 +++++++++++++++---- .../camel/k/cron/CronRuntimeListener.java | 2 +- .../org/apache/camel/k/customizer/cron-quartz | 18 --------------- .../camel/k/main/ApplicationRuntime.java | 3 ++- 5 files changed, 29 insertions(+), 25 deletions(-) delete mode 100644 camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-quartz diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java index e75d3890f..aaa0ae775 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java @@ -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, diff --git a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRoutePolicyFactory.java b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRoutePolicyFactory.java index d1dca0fda..6e5c7a2f6 100644 --- a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRoutePolicyFactory.java +++ b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronRoutePolicyFactory.java @@ -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; @@ -31,7 +33,10 @@ */ public class CronRoutePolicyFactory implements RoutePolicyFactory { - public CronRoutePolicyFactory() { + private Runtime runtime; + + public CronRoutePolicyFactory(Runtime runtime) { + this.runtime = runtime; } @Override @@ -39,9 +44,9 @@ public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, 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; @@ -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); + } } } 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 index 43a5212f9..ad624bb21 100644 --- 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 @@ -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)); diff --git a/camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-quartz b/camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-quartz deleted file mode 100644 index d567f8180..000000000 --- a/camel-k-runtime-cron/src/main/resources/META-INF/services/org/apache/camel/k/customizer/cron-quartz +++ /dev/null @@ -1,18 +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. -# - -class=org.apache.camel.k.cron.CronQuartzContextCustomizer diff --git a/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java b/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java index 002c56711..cf6d8dbd0 100644 --- a/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java +++ b/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java @@ -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(); }