You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/05/20 11:52:40 UTC

[camel-k-runtime] 02/03: cron: use ManagementStrategy to shutdown the context to make cron available to all the runtimes

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git

commit 01762515a757b1b011401d0c220261ffad9e3188
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Fri May 15 16:31:04 2020 +0200

    cron: use ManagementStrategy to shutdown the context to make cron available to all the runtimes
---
 .../main/java/org/apache/camel/k/RuntimeAware.java | 22 +++++++++
 .../apache/camel/k/listener/RoutesConfigurer.java  |  5 ++
 .../camel/k/cron/CronSourceLoaderInterceptor.java  | 56 +++++++++++++++++++++-
 .../java/org/apache/camel/k/cron/CronTest.java     |  2 -
 4 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeAware.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeAware.java
new file mode 100644
index 0000000..96f9571
--- /dev/null
+++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeAware.java
@@ -0,0 +1,22 @@
+/*
+ * 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;
+
+public interface RuntimeAware {
+    void setRuntime(Runtime runtime);
+    Runtime getRuntime();
+}
diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java
index 125eccd..c701621 100644
--- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java
+++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java
@@ -21,6 +21,7 @@ import java.util.List;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.k.Constants;
 import org.apache.camel.k.Runtime;
+import org.apache.camel.k.RuntimeAware;
 import org.apache.camel.k.Source;
 import org.apache.camel.k.SourceLoader;
 import org.apache.camel.k.Sources;
@@ -83,6 +84,10 @@ public class RoutesConfigurer extends AbstractPhaseListener {
 
         try {
             for (SourceLoader.Interceptor interceptor: interceptors) {
+                if (interceptor instanceof RuntimeAware) {
+                    ((RuntimeAware) interceptor).setRuntime(runtime);
+                }
+
                 interceptor.beforeLoad(loader, source);
             }
 
diff --git a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronSourceLoaderInterceptor.java b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronSourceLoaderInterceptor.java
index f24d463..04585e9 100644
--- a/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronSourceLoaderInterceptor.java
+++ b/camel-k-runtime-cron/src/main/java/org/apache/camel/k/cron/CronSourceLoaderInterceptor.java
@@ -18,18 +18,27 @@ package org.apache.camel.k.cron;
 
 import java.util.Optional;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.k.Runtime;
+import org.apache.camel.k.RuntimeAware;
 import org.apache.camel.k.Source;
 import org.apache.camel.k.SourceLoader;
 import org.apache.camel.k.annotation.LoaderInterceptor;
 import org.apache.camel.k.support.RuntimeSupport;
 import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.support.EventNotifierSupport;
 import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @LoaderInterceptor("cron")
-public class CronSourceLoaderInterceptor implements SourceLoader.Interceptor {
+public class CronSourceLoaderInterceptor implements SourceLoader.Interceptor, RuntimeAware {
+
     private String timerUri;
     private String overridableComponents;
+    private Runtime runtime;
 
     public CronSourceLoaderInterceptor() {
         this.timerUri = "timer:camel-k-cron-override?delay=0&period=1&repeatCount=1";
@@ -53,6 +62,16 @@ public class CronSourceLoaderInterceptor implements SourceLoader.Interceptor {
     }
 
     @Override
+    public void setRuntime(Runtime runtime) {
+        this.runtime = runtime;
+    }
+
+    @Override
+    public Runtime getRuntime() {
+        return runtime;
+    }
+
+    @Override
     public void beforeLoad(SourceLoader loader, Source source) {
     }
 
@@ -66,12 +85,20 @@ public class CronSourceLoaderInterceptor implements SourceLoader.Interceptor {
                         return;
                     }
 
+                    CamelContext context = runtime.getCamelContext();
                     String[] components = overridableComponents.split(",", -1);
 
                     for (RouteDefinition def : builder.getRouteCollection().getRoutes()) {
                         String uri = def.getInput() != null ? def.getInput().getUri() : null;
                         if (shouldBeOverridden(uri, components)) {
                             def.getInput().setUri(timerUri);
+
+                            //
+                            // Don't install the shutdown strategy more than once.
+                            //
+                            if (context.getManagementStrategy().getEventNotifiers().stream().noneMatch(CronShutdownStrategy.class::isInstance)) {
+                                context.getManagementStrategy().addEventNotifier(new CronShutdownStrategy(runtime));
+                            }
                         }
                     }
                 });
@@ -95,4 +122,31 @@ public class CronSourceLoaderInterceptor implements SourceLoader.Interceptor {
         }
         return false;
     }
+
+    private static class CronShutdownStrategy extends EventNotifierSupport {
+        private static final Logger LOGGER = LoggerFactory.getLogger(CronShutdownStrategy.class);
+        private final Runtime runtime;
+
+        public CronShutdownStrategy(Runtime runtime) {
+            this.runtime = runtime;
+        }
+
+        @Override
+        public void notify(CamelEvent event) throws Exception {
+            LOGGER.info("Initiate runtime shutdown");
+            this.runtime.getCamelContext().getExecutorServiceManager().newThread("CronShutdownStrategy", () -> {
+                try {
+                    LOGGER.info("Shutting down the runtime");
+                    runtime.stop();
+                } catch (Exception e) {
+                    LOGGER.warn("Error while shutting down the runtime", e);
+                }
+            }).start();
+        }
+
+        @Override
+        public boolean isEnabled(CamelEvent event) {
+            return event instanceof CamelEvent.ExchangeCompletedEvent || event instanceof CamelEvent.ExchangeFailedEvent;
+        }
+    }
 }
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 9785987..d90427a 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
@@ -37,8 +37,6 @@ public class CronTest {
     public void testCronTimerActivation(String routes, String cronOverride) throws Exception {
         ApplicationRuntime runtime = new ApplicationRuntime();
         runtime.setProperties(
-            // does not seems to work
-            "camel.main.duration-max-messages", "1",
             "loader.interceptor.cron.overridable-components", cronOverride
         );
         runtime.addListener(RoutesConfigurer.forRoutes(routes));