Skip to content

Commit

Permalink
Merge pull request #11 from NetoDevel/master
Browse files Browse the repository at this point in the history
Adds shutdown hook
  • Loading branch information
robertotru committed Apr 27, 2020
2 parents aeef6be + 03b5b3f commit 6bb0419
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@
<version>2.5</version>
</dependency>

<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>

<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
Expand Down Expand Up @@ -163,6 +169,14 @@
<tag>${project.version}</tag>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -11,9 +13,12 @@
import java.util.concurrent.Executors;

abstract class AbstractRedisInstance implements Redis {

private static Log log = LogFactory.getLog(AbstractRedisInstance.class);

protected List<String> args = Collections.emptyList();
private volatile boolean active = false;
private Process redisProcess;
private Process redisProcess;
private final int port;

private ExecutorService executor;
Expand All @@ -32,6 +37,7 @@ public synchronized void start() throws EmbeddedRedisException {
}
try {
redisProcess = createRedisProcessBuilder().start();
installExitHook();
logErrors();
awaitRedisServerReady();
active = true;
Expand All @@ -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));
Expand All @@ -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);
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 6bb0419

Please sign in to comment.