Implemented MONITOR

This commit is contained in:
Jonathan Leibiusky
2010-08-08 13:48:04 -03:00
parent 0453ffcd1f
commit f0a426a7c7
5 changed files with 45 additions and 6 deletions

View File

@@ -535,4 +535,8 @@ public class Client extends Connection {
public void info() { public void info() {
sendCommand("INFO"); sendCommand("INFO");
} }
public void monitor() {
sendCommand("MONITOR");
}
} }

View File

@@ -68,12 +68,16 @@ public class Connection {
} }
} }
public void disconnect() throws IOException { public void disconnect() {
if (connected) { if (connected) {
inputStream.close(); try {
outputStream.close(); inputStream.close();
if (!socket.isClosed()) { outputStream.close();
socket.close(); if (!socket.isClosed()) {
socket.close();
}
} catch (IOException ex) {
throw new JedisException(ex);
} }
connected = false; connected = false;
} }

View File

@@ -638,4 +638,9 @@ public class Jedis {
client.info(); client.info();
return client.getBulkReply(); return client.getBulkReply();
} }
public void monitor(JedisMonitor jedisMonitor) {
client.monitor();
jedisMonitor.proceed(client);
}
} }

View File

@@ -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);
}

View File

@@ -3,6 +3,7 @@ package redis.clients.jedis.tests.commands;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.JedisException; import redis.clients.jedis.JedisException;
import redis.clients.jedis.JedisMonitor;
public class PersistenceControlCommandsTest extends JedisCommandTestBase { public class PersistenceControlCommandsTest extends JedisCommandTestBase {
@Test @Test
@@ -40,8 +41,18 @@ public class PersistenceControlCommandsTest extends JedisCommandTestBase {
} }
@Test @Test
public void info() throws InterruptedException { public void info() {
String info = jedis.info(); String info = jedis.info();
assertNotNull(info); assertNotNull(info);
} }
@Test
public void monitor() {
jedis.monitor(new JedisMonitor() {
public void onCommand(String command) {
assertTrue(command.contains("OK"));
client.disconnect();
}
});
}
} }