From 03b5b3ffe05867df3df8101b238206824ccc6ecf Mon Sep 17 00:00:00 2001 From: NetoDevel Date: Fri, 10 Apr 2020 21:27:38 -0300 Subject: [PATCH] Adds shutdown hook --- pom.xml | 18 +++++++++++++++-- .../redis/embedded/AbstractRedisInstance.java | 20 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index a29a572b..8aa21d5a 100644 --- a/pom.xml +++ b/pom.xml @@ -49,13 +49,19 @@ 2.5 - + + org.slf4j + slf4j-simple + 1.7.21 + + commons-logging commons-logging 1.2 - test + + redis.clients jedis @@ -163,6 +169,14 @@ ${project.version} + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + diff --git a/src/main/java/redis/embedded/AbstractRedisInstance.java b/src/main/java/redis/embedded/AbstractRedisInstance.java index 7dd75428..2e3c9ba7 100644 --- a/src/main/java/redis/embedded/AbstractRedisInstance.java +++ b/src/main/java/redis/embedded/AbstractRedisInstance.java @@ -1,6 +1,8 @@ package redis.embedded; import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import redis.embedded.exceptions.EmbeddedRedisException; import java.io.*; @@ -11,9 +13,12 @@ import java.util.concurrent.Executors; abstract class AbstractRedisInstance implements Redis { + + private static Log log = LogFactory.getLog(AbstractRedisInstance.class); + protected List args = Collections.emptyList(); private volatile boolean active = false; - private Process redisProcess; + private Process redisProcess; private final int port; private ExecutorService executor; @@ -32,6 +37,7 @@ public synchronized void start() throws EmbeddedRedisException { } try { redisProcess = createRedisProcessBuilder().start(); + installExitHook(); logErrors(); awaitRedisServerReady(); active = true; @@ -40,6 +46,10 @@ public synchronized void start() throws EmbeddedRedisException { } } + private void installExitHook() { + Runtime.getRuntime().addShutdownHook(new Thread(this::stop, "RedisInstanceCleaner")); + } + private void logErrors() { final InputStream errorStream = redisProcess.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream)); @@ -57,9 +67,8 @@ private void awaitRedisServerReady() throws IOException { outputLine = reader.readLine(); if (outputLine == null) { //Something goes wrong. Stream is ended before server was activated. - throw new RuntimeException("Can't start redis server. Check logs for details. Redis process log: "+outputStringBuffer.toString()); - } - else{ + throw new RuntimeException("Can't start redis server. Check logs for details. Redis process log: " + outputStringBuffer.toString()); + } else { outputStringBuffer.append("\n"); outputStringBuffer.append(outputLine); } @@ -80,11 +89,14 @@ private ProcessBuilder createRedisProcessBuilder() { public synchronized void stop() throws EmbeddedRedisException { if (active) { + log.info("Stopping redis server..."); + if (executor != null && !executor.isShutdown()) { executor.shutdown(); } redisProcess.destroy(); tryWaitFor(); + log.info("Redis exited"); active = false; } }