Skip to content

Commit

Permalink
OZ-476: Add ShutdownHandler bean (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
VaishSiddharth authored May 29, 2024
1 parent 292a692 commit dd77b72
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
50 changes: 50 additions & 0 deletions commons/src/main/java/com/ozonehis/eip/utils/ShutdownHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright © 2021, Ozone HIS <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.ozonehis.eip.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ShutdownHandler implements ApplicationContextAware {

private ApplicationContext applicationContext;

public static boolean shuttingDown = false;

public static Logger LOGGER = LoggerFactory.getLogger(ShutdownHandler.class);

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public void shutdown() {
ConfigurableApplicationContext configurableApplicationContext =
(ConfigurableApplicationContext) applicationContext;
configurableApplicationContext.close();
exitApplication();
}

public static synchronized void exitApplication() {
if (shuttingDown) {
LOGGER.info("Application is already shutting down");
return;
}
shuttingDown = true;
LOGGER.info("Shutting down the application...");

// Shutdown in a new thread to ensure other background shutdown threads complete too
new Thread(() -> System.exit(0)).start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © 2021, Ozone HIS <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.ozonehis.eip.utils;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

public class ShutdownHandlerTest {

private ShutdownHandler shutdownHandler;
private ApplicationContext applicationContext;
private Logger logger;

@BeforeEach
public void setUp() {
shutdownHandler = new ShutdownHandler();
applicationContext = mock(ConfigurableApplicationContext.class);
shutdownHandler.setApplicationContext(applicationContext);
logger = mock(Logger.class);
ShutdownHandler.LOGGER = logger;
}

@Test
public void testShutdownCloseApplicationContext() {
shutdownHandler.shutdown();

Mockito.verify((ConfigurableApplicationContext) applicationContext, times(1))
.close();
}

@Test
public void testExitApplication() {
ShutdownHandler.shuttingDown = false;

ShutdownHandler.exitApplication();

// Since System.exit() cannot be directly tested, we can't validate that it was invoked.
// We can only verify that the logger is invoked.
Mockito.verify(logger, times(1)).info("Shutting down the application...");
}

@Test
public void testExitApplicationWhenAlreadyShuttingDown() {
ShutdownHandler.shuttingDown = true;

ShutdownHandler.exitApplication();

Mockito.verify(logger, times(1)).info("Application is already shutting down");
}
}

0 comments on commit dd77b72

Please sign in to comment.