Skip to content

Commit

Permalink
Graceful shutdown strategy used as default one
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Nov 19, 2021
1 parent 5a06580 commit 8f64fb5
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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;
Expand Down Expand Up @@ -102,6 +104,24 @@ CamelContextBuildItem context(
return new CamelContextBuildItem(context);
}

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.core.deployment;

import javax.inject.Inject;

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;

import static org.assertj.core.api.Assertions.assertThat;

public class CamelContextDefaultStrategyTest {
@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest();

@Inject
CamelContext camelContext;

@Test
public void testRestConfiguration() {
assertThat(camelContext.getShutdownStrategy() instanceof DefaultShutdownStrategy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public RuntimeValue<CamelRuntime> createRuntime(BeanContainer beanContainer, Run
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected ManagementNameStrategy createManagementNameStrategy() {

@Override
protected ShutdownStrategy createShutdownStrategy() {
return new NoShutdownStrategy();
return new DefaultShutdownStrategy();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@ public JsonObject describeMain() {
.add("routes", routes)
.build();
}

@Path("/getShutdownStrategy")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getShutdownStrategy() {
return context.getShutdownStrategy().getClass().getSimpleName();
}
}

0 comments on commit 8f64fb5

Please sign in to comment.