Reputation: 25
I have an aeron publisher and receiver service. Wrote it using springboot. I have added Shutdown hook Runtime.getRuntime().addShutdownHook
with a thread signaling a ShutdownSignalBarrier to initiate a graceful shutdown.
The onStop()
method waits for this signal to proceed with the shutdown sequence, ensuring that all processes are given time to complete their tasks before the application stops.
Finally, the close()
method ensures that all resources are properly released, which is crucial for avoiding resource leaks.
Here is the relevant code:
private Aeron aeron;
private AeronArchive aeronArchive;
private Publication publication;
private Subscription subscription;
private AgentRunner sendAgentRunner;
private AgentRunner receiveAgentRunner;
private SimReceiveAgent receiveAgent;
private SimSendAgent sendAgent;
@PostConstruct
public void init() {
setup();
receiveAgent = new SimReceiveAgent(subscription, barrier);
sendAgent = new SimSendAgent(publication, aeron);
// Construct the subs and pubs Runners
sendAgentRunner = new AgentRunner(idleStrategySend,
Throwable::printStackTrace, null, sendAgent);
receiveAgentRunner = new AgentRunner(idleStrategyReceive,
Throwable::printStackTrace, null, receiveAgent);
// Start the subs and pubs
LOGGER.info("Starting publisher simulator sender thread");
AgentRunner.startOnThread(sendAgentRunner);
LOGGER.info("Starting publisher simulator receiver thread");
AgentRunner.startOnThread(receiveAgentRunner);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("Shutdown signal received, signaling barrier...");
barrier.signal();
}));
}
public void onStop() throws Exception {
barrier.await();
}
public void close() throws Exception {
barrier.await();
LOGGER.info("Attempt to stop application");
if (timer != null) {
timer.cancel();
}
CloseHelper.quietClose(sendAgentRunner);
CloseHelper.quietClose(receiveAgentRunner);
CloseHelper.quietClose(aeron);
CloseHelper.quietClose(aeronArchive);
}
However, what I see is even with the signalling getting caught and proper shutdown sequence followed, the application won't terminate. Anything missing here.
Upvotes: 0
Views: 45
Reputation: 1
Yes, it appears you are using the barrier wrong. the shutdown is already being captured and the signal is being issued by the barrier... all you need to do is wait for it, in short pause the main thread. You can reference to this class in my framework for a simple example where I isolated the startup logic in a class -> https://github.com/Reef3rm4n/alv/blob/main/alv-core/src/main/java/io/alv/core/cluster/ClusterAppMain.java
Upvotes: 0