diff --git a/src/main/java/redis/embedded/RedisInstance.java b/src/main/java/redis/embedded/RedisInstance.java index 60c61beb..264566e0 100644 --- a/src/main/java/redis/embedded/RedisInstance.java +++ b/src/main/java/redis/embedded/RedisInstance.java @@ -1,5 +1,7 @@ package redis.embedded; +import redis.embedded.util.CheckedRunnable; + import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -14,14 +16,16 @@ public abstract class RedisInstance implements Redis { private final Pattern readyPattern; private final int port; private final List args; + private final boolean forceStop; private volatile boolean active = false; private Process process; - protected RedisInstance(final int port, final List args, final Pattern readyPattern) { + protected RedisInstance(final int port, final List args, final Pattern readyPattern, final boolean forceStop) { this.port = port; this.args = args; this.readyPattern = readyPattern; + this.forceStop = forceStop; } public synchronized void start() throws IOException { @@ -31,7 +35,10 @@ public synchronized void start() throws IOException { process = new ProcessBuilder(args) .directory(new File(args.get(0)).getParentFile()) .start(); - addShutdownHook("RedisInstanceCleaner", checkedToRuntime(this::stop)); + addShutdownHook("RedisInstanceCleaner", checkedToRuntime(new CheckedRunnable() { + public void run() throws Exception { + } + })); logStream(process.getErrorStream(), System.out::println); awaitServerReady(); @@ -51,8 +58,12 @@ public synchronized void stop() throws IOException { if (!active) return; try { - process.destroy(); - process.waitFor(); + if (forceStop) + process.destroyForcibly(); + else { + process.destroy(); + process.waitFor(); + } active = false; } catch (InterruptedException e) { throw new IOException("Failed to stop redis service", e); diff --git a/src/main/java/redis/embedded/RedisSentinel.java b/src/main/java/redis/embedded/RedisSentinel.java index 868a1159..99f0a418 100644 --- a/src/main/java/redis/embedded/RedisSentinel.java +++ b/src/main/java/redis/embedded/RedisSentinel.java @@ -6,8 +6,8 @@ public final class RedisSentinel extends RedisInstance { - public RedisSentinel(final int port, final List args) { - super(port, args, SENTINEL_READY_PATTERN); + public RedisSentinel(final int port, final List args, final boolean forceStop) { + super(port, args, SENTINEL_READY_PATTERN, forceStop); } public static RedisSentinelBuilder newRedisSentinel() { return new RedisSentinelBuilder(); } diff --git a/src/main/java/redis/embedded/RedisServer.java b/src/main/java/redis/embedded/RedisServer.java index 36bf91f7..c389cf4a 100644 --- a/src/main/java/redis/embedded/RedisServer.java +++ b/src/main/java/redis/embedded/RedisServer.java @@ -15,25 +15,25 @@ public RedisServer() throws IOException { } public RedisServer(final int port) throws IOException { - this(port, newRedisServer().port(port).buildCommandArgs()); + this(port, newRedisServer().port(port).buildCommandArgs(), false); } public RedisServer(final int port, final File executable) { - this(port, Arrays.asList( - executable.getAbsolutePath(), - "--port", Integer.toString(port) - )); + this( port + , Arrays.asList(executable.getAbsolutePath(), "--port", Integer.toString(port)) + , false + ); } public RedisServer(final int port, final ExecutableProvider executableProvider) throws IOException { - this(port, Arrays.asList( - executableProvider.get().getAbsolutePath(), - "--port", Integer.toString(port) - )); + this( port + , Arrays.asList(executableProvider.get().getAbsolutePath(), "--port", Integer.toString(port)) + , false + ); } - public RedisServer(final int port, final List args) { - super(port, args, SERVER_READY_PATTERN); + public RedisServer(final int port, final List args, final boolean forceStop) { + super(port, args, SERVER_READY_PATTERN, forceStop); } public static RedisServerBuilder newRedisServer() { diff --git a/src/main/java/redis/embedded/core/RedisSentinelBuilder.java b/src/main/java/redis/embedded/core/RedisSentinelBuilder.java index 2e509b5f..4ce4396d 100644 --- a/src/main/java/redis/embedded/core/RedisSentinelBuilder.java +++ b/src/main/java/redis/embedded/core/RedisSentinelBuilder.java @@ -33,6 +33,7 @@ public final class RedisSentinelBuilder { private int parallelSyncs = 1; private int quorumSize = 1; private String sentinelConf; + private boolean forceStop = false; private StringBuilder redisConfigBuilder; @@ -107,9 +108,14 @@ public RedisSentinelBuilder setting(final String configLine) { return this; } + public RedisSentinelBuilder onShutdownForceStop(final boolean forceStop) { + this.forceStop = forceStop; + return this; + } + public RedisSentinel build() { tryResolveConfAndExec(); - return new RedisSentinel(port, buildCommandArgs()); + return new RedisSentinel(port, buildCommandArgs(), forceStop); } private void tryResolveConfAndExec() { diff --git a/src/main/java/redis/embedded/core/RedisServerBuilder.java b/src/main/java/redis/embedded/core/RedisServerBuilder.java index ec866349..7384c621 100644 --- a/src/main/java/redis/embedded/core/RedisServerBuilder.java +++ b/src/main/java/redis/embedded/core/RedisServerBuilder.java @@ -24,6 +24,7 @@ public final class RedisServerBuilder { private String bindAddress = "127.0.0.1"; private int bindPort = DEFAULT_REDIS_PORT; private InetSocketAddress slaveOf; + private boolean forceStop = false; private StringBuilder redisConfigBuilder = new StringBuilder(); @@ -70,8 +71,13 @@ public RedisServerBuilder setting(final String configLine) { return this; } + public RedisServerBuilder onShutdownForceStop(final boolean forceStop) { + this.forceStop = forceStop; + return this; + } + public RedisServer build() throws IOException { - return new RedisServer(bindPort, buildCommandArgs()); + return new RedisServer(bindPort, buildCommandArgs(), forceStop); } public void reset() {