Skip to content

Commit b72b37d

Browse files
Yannic92thjaeckle
authored andcommitted
Exits JVM after graceful shutdown
* If shutdown was graceful jvm exits with code 0 * If shutdown was not graceful jvm exits with code -1 Signed-off-by: Klem Yannic (INST/ECS1) <[email protected]>
1 parent e2f4120 commit b72b37d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

services/base/src/main/java/org/eclipse/ditto/services/base/DittoService.java

+30
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.concurrent.CompletableFuture;
23+
import java.util.concurrent.Executors;
24+
import java.util.concurrent.ScheduledExecutorService;
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.concurrent.atomic.AtomicBoolean;
2327
import java.util.function.Function;
2428

2529
import javax.annotation.concurrent.Immutable;
@@ -42,11 +46,13 @@
4246
import akka.actor.ActorSystem;
4347
import akka.actor.CoordinatedShutdown;
4448
import akka.actor.Props;
49+
import akka.actor.Scheduler;
4550
import akka.cluster.Cluster;
4651
import akka.cluster.pubsub.DistributedPubSub;
4752
import akka.management.AkkaManagement;
4853
import akka.management.cluster.bootstrap.ClusterBootstrap;
4954
import akka.stream.ActorMaterializer;
55+
import akka.stream.stage.TimerMessages;
5056
import kamon.Kamon;
5157

5258
/**
@@ -173,6 +179,8 @@ protected void startActorSystem() {
173179
startClusterMemberAwareActor(actorSystem, configReader);
174180
startServiceRootActors(actorSystem, configReader);
175181

182+
final AtomicBoolean gracefulShutdown = new AtomicBoolean(false);
183+
176184
CoordinatedShutdown.get(actorSystem).addTask(
177185
CoordinatedShutdown.PhaseBeforeServiceUnbind(), "Log shutdown initiation",
178186
() -> {
@@ -184,8 +192,30 @@ protected void startActorSystem() {
184192
CoordinatedShutdown.PhaseBeforeActorSystemTerminate(), "Log successful graceful shutdown",
185193
() -> {
186194
logger.info("Graceful shutdown completed.");
195+
gracefulShutdown.set(true);
187196
return CompletableFuture.completedFuture(Done.getInstance());
188197
});
198+
199+
actorSystem.registerOnTermination(() -> {
200+
if (gracefulShutdown.get()) {
201+
exit(0);
202+
} else {
203+
exit(-1);
204+
}
205+
});
206+
}
207+
208+
private void exit(int status) {
209+
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
210+
final String message = String.format("Exiting JVM with status code '%d'", status);
211+
scheduler.schedule(() -> {
212+
if(status == 0) {
213+
logger.info(message);
214+
}else {
215+
logger.warn(message);
216+
}
217+
System.exit(status);
218+
}, 0, TimeUnit.SECONDS);
189219
}
190220

191221
/**

0 commit comments

Comments
 (0)