forked from ozimov/embedded-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted RedisInstance interface and abstract class for server and s…
…entinel
- Loading branch information
Showing
3 changed files
with
89 additions
and
63 deletions.
There are no files selected for viewing
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,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; | ||
} | ||
} | ||
} |
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,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; | ||
} |
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