You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2021/11/22 14:02:12 UTC

[camel-quarkus] branch main updated: Graceful shutdown strategy used as default one

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

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 79b7a7a  Graceful shutdown strategy used as default one
79b7a7a is described below

commit 79b7a7a37577d8eff9e00654b2eadc640402a39c
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Fri Nov 19 10:00:34 2021 +0100

    Graceful shutdown strategy used as default one
---
 docs/modules/ROOT/pages/migration-guide/2.6.0.adoc | 19 +++++++
 docs/modules/ROOT/pages/migration-guide/index.adoc |  1 +
 .../core/deployment/CamelContextProcessor.java     | 20 +++++++
 .../CamelContextDefaultStrategyTest.java           | 38 +++++--------
 .../camel/quarkus/core/CamelContextRecorder.java   |  4 ++
 .../camel/quarkus/core/FastCamelContext.java       |  3 +-
 ...lContextDefaultShutdownStrategyDevModeTest.java | 64 ++++++++++++++++++++++
 .../CamelContextNoShutdownStrategyDevModeTest.java | 43 +++++++++++++++
 .../camel/quarkus/main/CamelSupportResource.java   |  7 +++
 9 files changed, 175 insertions(+), 24 deletions(-)

diff --git a/docs/modules/ROOT/pages/migration-guide/2.6.0.adoc b/docs/modules/ROOT/pages/migration-guide/2.6.0.adoc
new file mode 100644
index 0000000..81dd32c
--- /dev/null
+++ b/docs/modules/ROOT/pages/migration-guide/2.6.0.adoc
@@ -0,0 +1,19 @@
+= Camel Quarkus 2.6.0 Migration Guide
+
+The following guide outlines how to adapt your code to changes that were made in Camel Quarkus 2.6.0 & Quarkus 2.6.0.Final.
+
+== Graceful shutdown strategy is used by default
+
+In previous releases, graceful shutdown strategy (default strategy in Camel, see https://camel.apache.org/manual/graceful-shutdown.html[documentation] ) wasn't used by default.
+Shutdown wasn't controlled by any strategy.
+
+This is no longer the case.
+Graceful shutdown strategy is enabled by default (see https://camel.apache.org/manual/graceful-shutdown.html[documentation]) with one exception only.
+If timeout for graceful shutdown is not set and application runs in a development mode, no shutdown strategy is used (behavior for the development mode without timeout for graceful timeout wasn't changed).
+
+`DefaultShutdownStrategy` could be configured via `application.properties`.
+For example:
+```
+#set graceful timeout to 15 seconds
+camel.main.shutdownTimeout = 15
+```
diff --git a/docs/modules/ROOT/pages/migration-guide/index.adoc b/docs/modules/ROOT/pages/migration-guide/index.adoc
index cca6a59..a53106e 100644
--- a/docs/modules/ROOT/pages/migration-guide/index.adoc
+++ b/docs/modules/ROOT/pages/migration-guide/index.adoc
@@ -4,6 +4,7 @@ We do frequent releases, a release almost every month, and even though we strive
 
 Listed here are guides on how to migrate between major versions and anything of significance to watch for when upgrading from minor versions.
 
+* xref:migration-guide/2.6.0.adoc[Camel Quarkus 2.5.0 to Camel Quarkus 2.6.0 migration guide]
 * xref:migration-guide/2.2.0.adoc[Camel Quarkus 2.1.0 to Camel Quarkus 2.2.0 migration guide]
 * xref:migration-guide/2.1.0.adoc[Camel Quarkus 2.0.0 to Camel Quarkus 2.1.0 migration guide]
 * xref:migration-guide/2.0.0.adoc[Camel Quarkus 1.x to Camel Quarkus 2.0.0 migration guide]
diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelContextProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelContextProcessor.java
index 73aa229..814ac56 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelContextProcessor.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelContextProcessor.java
@@ -25,6 +25,8 @@ import java.util.stream.Collectors;
 import io.quarkus.arc.deployment.BeanContainerBuildItem;
 import io.quarkus.arc.deployment.BeanDiscoveryFinishedBuildItem;
 import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
+import io.quarkus.deployment.IsDevelopment;
+import io.quarkus.deployment.annotations.BuildProducer;
 import io.quarkus.deployment.annotations.BuildStep;
 import io.quarkus.deployment.annotations.Consume;
 import io.quarkus.deployment.annotations.ExecutionTime;
@@ -103,6 +105,24 @@ public class CamelContextProcessor {
     }
 
     /**
+     * This step customizes camel context for development mode.
+     *
+     * @param recorder the recorder
+     * @param producer producer of context customizer build item
+     */
+    @Record(ExecutionTime.STATIC_INIT)
+    @BuildStep(onlyIf = IsDevelopment.class)
+    public void devModeCamelContextCustomizations(
+            CamelContextRecorder recorder,
+            BuildProducer<CamelContextCustomizerBuildItem> producer) {
+        String val = CamelSupport.getOptionalConfigValue("camel.main.shutdownTimeout", String.class, null);
+        if (val == null) {
+            //if no graceful timeout is set in development mode, graceful shutdown is replaced with no shutdown
+            producer.produce(new CamelContextCustomizerBuildItem(recorder.createNoShutdownStrategyCustomizer()));
+        }
+    }
+
+    /**
      * This build steps assembles the default implementation of a {@link CamelRuntime} responsible to bootstrap
      * Camel.
      * <p>
diff --git a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java b/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/CamelContextDefaultStrategyTest.java
similarity index 54%
copy from integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java
copy to extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/CamelContextDefaultStrategyTest.java
index e1e742f..8a50011 100644
--- a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java
+++ b/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/deployment/CamelContextDefaultStrategyTest.java
@@ -14,35 +14,27 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.quarkus.main;
+package org.apache.camel.quarkus.core.deployment;
 
-import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
-import javax.json.Json;
-import javax.json.JsonArrayBuilder;
-import javax.json.JsonObject;
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
 
+import io.quarkus.test.QuarkusUnitTest;
 import org.apache.camel.CamelContext;
+import org.apache.camel.impl.engine.DefaultShutdownStrategy;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
-@Path("/test")
-@ApplicationScoped
-public class CamelSupportResource {
-    @Inject
-    CamelContext context;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CamelContextDefaultStrategyTest {
+    @RegisterExtension
+    static final QuarkusUnitTest CONFIG = new QuarkusUnitTest();
 
-    @Path("/describe")
-    @GET
-    @Produces(MediaType.APPLICATION_JSON)
-    public JsonObject describeMain() {
-        JsonArrayBuilder routes = Json.createArrayBuilder();
-        context.getRoutes().forEach(route -> routes.add(route.getId()));
+    @Inject
+    CamelContext camelContext;
 
-        return Json.createObjectBuilder()
-                .add("routes", routes)
-                .build();
+    @Test
+    public void testRestConfiguration() {
+        assertThat(camelContext.getShutdownStrategy() instanceof DefaultShutdownStrategy);
     }
 }
diff --git a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelContextRecorder.java b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelContextRecorder.java
index 7c2df25..1e03f27 100644
--- a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelContextRecorder.java
+++ b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelContextRecorder.java
@@ -86,6 +86,10 @@ public class CamelContextRecorder {
         return new RuntimeValue<>(runtime);
     }
 
+    public RuntimeValue<CamelContextCustomizer> createNoShutdownStrategyCustomizer() {
+        return new RuntimeValue((CamelContextCustomizer) context -> context.setShutdownStrategy(new NoShutdownStrategy()));
+    }
+
     public void addRoutes(RuntimeValue<CamelContext> context, String typeName) {
         try {
             addRoutes(
diff --git a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/FastCamelContext.java b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/FastCamelContext.java
index 4914be7..10b59a9 100644
--- a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/FastCamelContext.java
+++ b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/FastCamelContext.java
@@ -29,6 +29,7 @@ import org.apache.camel.impl.engine.DefaultCamelBeanPostProcessor;
 import org.apache.camel.impl.engine.DefaultComponentResolver;
 import org.apache.camel.impl.engine.DefaultDataFormatResolver;
 import org.apache.camel.impl.engine.DefaultLanguageResolver;
+import org.apache.camel.impl.engine.DefaultShutdownStrategy;
 import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.ClassResolver;
@@ -89,7 +90,7 @@ public class FastCamelContext extends DefaultCamelContext implements CatalogCame
 
     @Override
     protected ShutdownStrategy createShutdownStrategy() {
-        return new NoShutdownStrategy();
+        return new DefaultShutdownStrategy();
     }
 
     @Override
diff --git a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelContextDefaultShutdownStrategyDevModeTest.java b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelContextDefaultShutdownStrategyDevModeTest.java
new file mode 100644
index 0000000..47b876e
--- /dev/null
+++ b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelContextDefaultShutdownStrategyDevModeTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.quarkus.main;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Properties;
+
+import io.quarkus.test.QuarkusDevModeTest;
+import io.restassured.RestAssured;
+import org.apache.camel.impl.engine.DefaultShutdownStrategy;
+import org.hamcrest.Matchers;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.Asset;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CamelContextDefaultShutdownStrategyDevModeTest {
+    @RegisterExtension
+    static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
+            .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
+                    .addAsResource(applicationProperties(), "application.properties")
+                    .addClass(CamelSupportResource.class));
+
+    public static Asset applicationProperties() {
+        Writer writer = new StringWriter();
+
+        Properties props = new Properties();
+        props.setProperty("camel.main.shutdownTimeout", "15");
+
+        try {
+            props.store(writer, "");
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        return new StringAsset(writer.toString());
+    }
+
+    @Test
+    public void test() throws IOException {
+        RestAssured.when().get("/test/getShutdownStrategy").then().assertThat()
+                .body(Matchers.is(DefaultShutdownStrategy.class.getSimpleName()));
+    }
+}
diff --git a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelContextNoShutdownStrategyDevModeTest.java b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelContextNoShutdownStrategyDevModeTest.java
new file mode 100644
index 0000000..d3ca52a
--- /dev/null
+++ b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelContextNoShutdownStrategyDevModeTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.quarkus.main;
+
+import java.io.IOException;
+
+import io.quarkus.test.QuarkusDevModeTest;
+import io.restassured.RestAssured;
+import org.apache.camel.quarkus.core.NoShutdownStrategy;
+import org.hamcrest.Matchers;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CamelContextNoShutdownStrategyDevModeTest {
+    @RegisterExtension
+    static final QuarkusDevModeTest TEST = new QuarkusDevModeTest()
+            .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
+                    .addClass(CamelSupportResource.class));
+
+    @Test
+    public void test() throws IOException {
+        RestAssured.when().get("/test/getShutdownStrategy").then().assertThat()
+                .body(Matchers.is(NoShutdownStrategy.class.getSimpleName()));
+    }
+}
diff --git a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java
index e1e742f..28e8f22 100644
--- a/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java
+++ b/integration-tests/main-devmode/src/test/java/org/apache/camel/quarkus/main/CamelSupportResource.java
@@ -45,4 +45,11 @@ public class CamelSupportResource {
                 .add("routes", routes)
                 .build();
     }
+
+    @Path("/getShutdownStrategy")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String getShutdownStrategy() {
+        return context.getShutdownStrategy().getClass().getSimpleName();
+    }
 }