Skip to content

Commit

Permalink
Fixup apache#1468 Intermittent failure of CamelDevModeTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Jul 16, 2020
1 parent 29c7e7a commit b173013
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.camel.quarkus.main;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import io.quarkus.runtime.Quarkus;
import org.apache.camel.CamelContext;
Expand All @@ -32,6 +31,7 @@ public class CamelMainRuntime implements CamelRuntime {
private static final Logger LOGGER = LoggerFactory.getLogger(CamelMainRuntime.class);
private final CamelMain main;
private final long shutdownTimeoutMs;
private volatile Thread mainThread;

public CamelMainRuntime(CamelMain main, long shutdownTimeoutMs) {
this.main = main;
Expand All @@ -48,15 +48,17 @@ public void start(String[] args) {
main.parseArguments(args);
main.startEngine();

new Thread(() -> {
final Thread worker = new Thread(() -> {
try {
main.runEngine();
} catch (Exception e) {
LOGGER.error("Failed to start application", e);
stop();
throw new RuntimeException(e);
}
}, "camel-main").start();
}, "camel-main");
this.mainThread = worker;
worker.start();
} catch (Exception e) {
LOGGER.error("Failed to start application", e);
stop();
Expand All @@ -68,10 +70,13 @@ public void start(String[] args) {
public void stop() {
main.stop();
/* Wait till the Camel shutdown is finished in camel-main thread started in start(String[]) above */
try {
main.getShutdownStrategy().await(shutdownTimeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
final Thread worker = this.mainThread;
if (worker != null) {
try {
worker.join(shutdownTimeoutMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

Expand Down

0 comments on commit b173013

Please sign in to comment.