Skip to content

Commit

Permalink
Fix apache#1468 Intermittent failure of CamelDevModeTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Jul 13, 2020
1 parent 0684fee commit b1eb8f4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.camel.quarkus.core.deployment.spi.ContainerBeansBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.RuntimeCamelContextCustomizerBuildItem;
import org.apache.camel.quarkus.main.CamelMain;
import org.apache.camel.quarkus.main.CamelMainConfig;
import org.apache.camel.quarkus.main.CamelMainProducers;
import org.apache.camel.quarkus.main.CamelMainRecorder;
import org.apache.camel.quarkus.main.deployment.spi.CamelMainBuildItem;
Expand Down Expand Up @@ -101,12 +102,14 @@ CamelMainBuildItem main(
CamelContextBuildItem context,
CamelRoutesCollectorBuildItem routesCollector,
List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses,
List<CamelMainListenerBuildItem> listeners) {
List<CamelMainListenerBuildItem> listeners,
CamelMainConfig camelMainConfig) {

RuntimeValue<CamelMain> main = recorder.createCamelMain(
context.getCamelContext(),
routesCollector.getValue(),
beanContainer.getValue());
beanContainer.getValue(),
camelMainConfig.shutdown.timeout.toMillis());

for (CamelRoutesBuilderClassBuildItem item : routesBuilderClasses) {
// don't add routes builders that are known by the container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
import org.apache.camel.spi.CamelBeanPostProcessor;
import org.apache.camel.spi.HasCamelContext;
import org.apache.camel.support.service.ServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class CamelMain extends MainCommandLineSupport implements HasCamelContext {
private static final Logger LOGGER = LoggerFactory.getLogger(CamelMain.class);

private final AtomicBoolean engineStarted;
private final long shutdownTimeoutMs;

public CamelMain(CamelContext camelContext) {
public CamelMain(CamelContext camelContext, long shutdownTimeoutMs) {
this.camelContext = camelContext;
this.shutdownTimeoutMs = shutdownTimeoutMs;
this.engineStarted = new AtomicBoolean();
}

Expand All @@ -55,7 +61,7 @@ protected void loadRouteBuilders(CamelContext camelContext) throws Exception {

@Override
protected void doInit() throws Exception {
setShutdownStrategy(new ShutdownStrategy());
setShutdownStrategy(new ShutdownStrategy(shutdownTimeoutMs));

super.doInit();
initCamelContext();
Expand All @@ -77,6 +83,9 @@ protected void doStart() throws Exception {
@Override
protected void doStop() throws Exception {
super.doStop();
/* Wait till the Camel shutdown is finished in its separate thread and proceed with subsequent Quarkus shutdown
* tasks only after that */
getShutdownStrategy().await(shutdownTimeoutMs, TimeUnit.MILLISECONDS);
this.camelContext.stop();
this.engineStarted.set(false);
}
Expand Down Expand Up @@ -153,10 +162,12 @@ public void run(String[] args) throws Exception {
private class ShutdownStrategy implements MainShutdownStrategy {
private final AtomicBoolean completed;
private final CountDownLatch latch;
private final long shutdownTimeoutMs;

public ShutdownStrategy() {
public ShutdownStrategy(long shutdownTimeoutMs) {
this.completed = new AtomicBoolean(false);
this.latch = new CountDownLatch(1);
this.shutdownTimeoutMs = shutdownTimeoutMs;
}

@Override
Expand All @@ -182,7 +193,10 @@ public void await() throws InterruptedException {

@Override
public void await(long timeout, TimeUnit unit) throws InterruptedException {
latch.await(timeout, unit);
if (!latch.await(timeout, unit)) {
LOGGER.warn("Could not await stopping CamelMain within " + shutdownTimeoutMs
+ " ms. You may want to increase camel.main.shutdown.timeout");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,28 @@
*/
package org.apache.camel.quarkus.main;

import java.time.Duration;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "camel.main", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class CamelMainConfig {

/**
* Build time configuration options for {@link CamelMain} shutdown.
*/
@ConfigItem
public ShutdownConfig shutdown;

@ConfigGroup
public static class ShutdownConfig {
/**
* A timeout (with millisecond precision) to wait for {@link CamelMain#stop()} to finish
*/
@ConfigItem(defaultValue = "PT3S")
public Duration timeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public class CamelMainRecorder {
public RuntimeValue<CamelMain> createCamelMain(
RuntimeValue<CamelContext> runtime,
RuntimeValue<RoutesCollector> routesCollector,
BeanContainer container) {
CamelMain main = new CamelMain(runtime.getValue());
BeanContainer container,
long shutdownTimeoutMs) {
CamelMain main = new CamelMain(runtime.getValue(), shutdownTimeoutMs);
main.setRoutesCollector(routesCollector.getValue());
main.addMainListener(new CamelMainEventBridge());

Expand Down

0 comments on commit b1eb8f4

Please sign in to comment.