diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 6cf059c..c6cd7be 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -627,4 +627,9 @@ public class BinaryClient extends Connection { public void debug(final DebugParams params) { sendCommand(DEBUG, params.getCommand()); } + + public void brpoplpush(final byte[] source, final byte[] destination, + final int timeout) { + sendCommand(BRPOPLPUSH, source, destination, toByteArray(timeout)); + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 09ca04c..8ad564f 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -2876,4 +2876,19 @@ public class BinaryJedis implements BinaryJedisCommands { public Client getClient() { return client; } + + /** + * Pop a value from a list, push it to another list and return it; or block + * until one is available + * + * @param source + * @param destination + * @param timeout + * @return the element + */ + public String brpoplpush(byte[] source, byte[] destination, int timeout) { + client.brpoplpush(source, destination, timeout); + return client.getBulkReply(); + } + } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index ee52f7b..b6fe0d0 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -501,4 +501,9 @@ public class Client extends BinaryClient implements Commands { linsert(SafeEncoder.encode(key), where, SafeEncoder.encode(pivot), SafeEncoder.encode(value)); } + + public void brpoplpush(String source, String destination, int timeout) { + brpoplpush(SafeEncoder.encode(source), SafeEncoder.encode(destination), + timeout); + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index c8ffe5c..6294033 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -2581,4 +2581,18 @@ public class Jedis extends BinaryJedis implements JedisCommands { client.linsert(key, where, pivot, value); return client.getIntegerReply(); } + + /** + * Pop a value from a list, push it to another list and return it; or block + * until one is available + * + * @param source + * @param destination + * @param timeout + * @return the element + */ + public String brpoplpush(String source, String destination, int timeout) { + client.brpoplpush(source, destination, timeout); + return client.getBulkReply(); + } } \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 37f0717..d493057 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -134,7 +134,7 @@ public final class Protocol { } public static enum Command { - PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG; + PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH; public final byte[] raw; diff --git a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java index a2b9f89..41f06a1 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ListCommandsTest.java @@ -1,5 +1,7 @@ package redis.clients.jedis.tests.commands; +import java.io.IOException; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; @@ -578,4 +580,52 @@ public class ListCommandsTest extends JedisCommandTestBase { assertEquals(-1, bstatus); } + + @Test + public void brpoplpush() { + (new Thread(new Runnable() { + public void run() { + try { + Thread.sleep(100); + Jedis j = createJedis(); + j.lpush("foo", "a"); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + })).start(); + + String element = jedis.brpoplpush("foo", "bar", 0); + + assertEquals("a", element); + assertEquals(1, jedis.llen("bar").longValue()); + assertEquals("a", jedis.lrange("bar", 0, -1).get(0)); + + (new Thread(new Runnable() { + public void run() { + try { + Thread.sleep(100); + Jedis j = createJedis(); + j.lpush("foo", "a"); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + })).start(); + + element = jedis.brpoplpush("foo".getBytes(), "bar".getBytes(), 0); + + assertEquals("a", element); + assertEquals(1, jedis.llen("bar").longValue()); + assertEquals("a", jedis.lrange("bar", 0, -1).get(0)); + + } } \ No newline at end of file