Skip to content

Commit

Permalink
Implemented MONITOR
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Leibiusky committed Aug 8, 2010
1 parent 0453ffc commit f0a426a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Expand Up @@ -535,4 +535,8 @@ public void shutdown() {
public void info() {
sendCommand("INFO");
}

public void monitor() {
sendCommand("MONITOR");
}
}
14 changes: 9 additions & 5 deletions src/main/java/redis/clients/jedis/Connection.java
Expand Up @@ -68,12 +68,16 @@ public void connect() throws UnknownHostException, IOException {
}
}

public void disconnect() throws IOException {
public void disconnect() {
if (connected) {
inputStream.close();
outputStream.close();
if (!socket.isClosed()) {
socket.close();
try {
inputStream.close();
outputStream.close();
if (!socket.isClosed()) {
socket.close();
}
} catch (IOException ex) {
throw new JedisException(ex);
}
connected = false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Expand Up @@ -638,4 +638,9 @@ public String info() {
client.info();
return client.getBulkReply();
}

public void monitor(JedisMonitor jedisMonitor) {
client.monitor();
jedisMonitor.proceed(client);
}
}
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/JedisMonitor.java
@@ -0,0 +1,15 @@
package redis.clients.jedis;

public abstract class JedisMonitor {
protected Client client;

public void proceed(Client client) {
this.client = client;
do {
String command = client.getBulkReply();
onCommand(command);
} while (client.isConnected());
}

public abstract void onCommand(String command);
}
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

import redis.clients.jedis.JedisException;
import redis.clients.jedis.JedisMonitor;

public class PersistenceControlCommandsTest extends JedisCommandTestBase {
@Test
Expand Down Expand Up @@ -40,8 +41,18 @@ public void lastsave() throws InterruptedException {
}

@Test
public void info() throws InterruptedException {
public void info() {
String info = jedis.info();
assertNotNull(info);
}

@Test
public void monitor() {
jedis.monitor(new JedisMonitor() {
public void onCommand(String command) {
assertTrue(command.contains("OK"));
client.disconnect();
}
});
}
}

0 comments on commit f0a426a

Please sign in to comment.