-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OZ-476: Add ShutdownHandler bean (#33)
- Loading branch information
1 parent
292a692
commit dd77b72
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
commons/src/main/java/com/ozonehis/eip/utils/ShutdownHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
commons/src/test/java/com/ozonehis/eip/utils/ShutdownHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |