Skip to content

Commit

Permalink
extracted RedisInstance interface and abstract class for server and s…
Browse files Browse the repository at this point in the history
…entinel
  • Loading branch information
turu committed Jan 22, 2015
1 parent 6927abb commit b5916b1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 63 deletions.
71 changes: 71 additions & 0 deletions src/main/java/redis/embedded/AbstractRedisInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package redis.embedded;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;

/**
* Created by piotrturek on 22/01/15.
*/
abstract class AbstractRedisInstance implements RedisInstance {
private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*";
protected List<String> args = Collections.emptyList();
private volatile boolean active = false;
private Process redisProcess;

public static RedisServerBuilder builder() {
return new RedisServerBuilder();
}

@Override
public boolean isActive() {
return active;
}

@Override
public synchronized void start() throws IOException {
if (active) {
throw new RuntimeException("This redis server instance is already running...");
}
redisProcess = createRedisProcessBuilder().start();
awaitRedisServerReady();
active = true;
}

private void awaitRedisServerReady() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(redisProcess.getInputStream()));
try {
String outputLine = null;
do {
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.");
}
} while (!outputLine.matches(REDIS_READY_PATTERN));
} finally {
reader.close();
}
}

private ProcessBuilder createRedisProcessBuilder() {
File executable = new File(args.get(0));
ProcessBuilder pb = new ProcessBuilder(args);
pb.directory(executable.getParentFile());
pb.redirectErrorStream();
return pb;
}

@Override
public synchronized void stop() throws InterruptedException {
if (active) {
redisProcess.destroy();
redisProcess.waitFor();
active = false;
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/redis/embedded/RedisInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package redis.embedded;

import java.io.IOException;

/**
* Created by piotrturek on 22/01/15.
*/
public interface RedisInstance {
boolean isActive();

void start() throws IOException;

void stop() throws InterruptedException;
}
67 changes: 4 additions & 63 deletions src/main/java/redis/embedded/RedisServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,30 @@

import redis.embedded.util.JarUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RedisServer {

private static final String REDIS_READY_PATTERN = ".*The server is now ready to accept connections on port.*";

private final List<String> args;

private volatile boolean active = false;
private Process redisProcess;

public RedisServer(Integer port) throws IOException {
public class RedisServer extends AbstractRedisInstance {
RedisServer(Integer port) throws IOException {
File executable = JarUtil.extractExecutableFromJar(RedisRunScriptEnum.getRedisRunScript());
this.args = Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
);
}

public RedisServer(File executable, Integer port) {
RedisServer(File executable, Integer port) {
this.args = Arrays.asList(
executable.getAbsolutePath(),
"--port", Integer.toString(port)
);
}

RedisServer(List<String> args) {
this.args = new ArrayList<String>(args);
this.args = new ArrayList<>(args);
}

public static RedisServerBuilder builder() {
return new RedisServerBuilder();
}

public boolean isActive() {
return active;
}

public synchronized void start() throws IOException {
if (active) {
throw new RuntimeException("This redis server instance is already running...");
}
redisProcess = createRedisProcessBuilder().start();
awaitRedisServerReady();
active = true;
}

private void awaitRedisServerReady() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(redisProcess.getInputStream()));
try {
String outputLine = null;
do {
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.");
}
} while (!outputLine.matches(REDIS_READY_PATTERN));
} finally {
reader.close();
}
}

private ProcessBuilder createRedisProcessBuilder() {
File executable = new File(args.get(0));
ProcessBuilder pb = new ProcessBuilder(args);
pb.directory(executable.getParentFile());
pb.redirectErrorStream();
return pb;
}

public synchronized void stop() throws InterruptedException {
if (active) {
redisProcess.destroy();
redisProcess.waitFor();
active = false;
}
}
}

0 comments on commit b5916b1

Please sign in to comment.