Java Code Examples for redis.clients.jedis.Jedis#brpop()

The following examples show how to use redis.clients.jedis.Jedis#brpop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleRedis.java    From ns4_frame with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] brpop(String queueName) throws MQRedisException {
    Jedis jedis = this.jedisPool.getResource();
    List<byte[]> result = null;
    try {
        result = jedis.brpop(0, SafeEncoder.encode(queueName));
    } catch (Exception e) {
        commandLog.error("mq brpop error", e);
        throw new MQRedisException(e);
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    if (result != null) {
        return result.get(1);
    } else {
        return null;
    }
}
 
Example 2
Source File: RedisMessageQueue.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param timeout
 * @param key
 * @return <br>
 */
@Override
public List<byte[]> pop(final int timeout, final String key) {
    Jedis jedis = null;
    try {
        jedis = RedisClientFactory.getJedisPool().getResource();
        List<byte[]> result = jedis.brpop(timeout, key.getBytes());
        if (CollectionUtils.isNotEmpty(result) && result.size() >= 2) {
            result.remove(0);
        }
        return result;
    }
    catch (Exception e) {
        throw new UtilException(ErrorCodeDef.CACHE_ERROR_10002, e);
    }
    finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 3
Source File: SingleRedis.java    From ns4_frame with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] brpop(final String queueName, final int timeout) throws MQRedisException {
    boolean runable = true;
    final Jedis jedis = this.jedisPool.getResource();
    List<byte[]> result = null;
    try {
        FutureTask<List<byte[]>> brpopTask = new FutureTask<>(new Callable<List<byte[]>>() {
            @Override
            public List<byte[]> call() throws Exception {
                return jedis.brpop(timeout, SafeEncoder.encode(queueName));
            }
        });
        executor.execute(brpopTask);
        result = brpopTask.get(timeout + 2, TimeUnit.SECONDS);
    } catch (Exception e) {
        if (e instanceof TimeoutException || e instanceof InterruptedException) {
            if (jedis != null) {
                jedis.disconnect();
                runable = false;
                jedisPool.returnBrokenResource(jedis);
            }
        }
        commandLog.error("mq brpop error", e);
        throw new MQRedisException(e);
    } finally {
        if (jedis != null && runable) {
            jedis.close();
        }
    }
    if (result != null) {
        return result.get(1);
    } else {
        return null;
    }
}
 
Example 4
Source File: RedisResourceIT.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourceCanSendAndRecieve() throws ResourceInitializationException {

  SharedRedisResource resource = new SharedRedisResource();
  final CustomResourceSpecifier_impl resourceSpecifier = new CustomResourceSpecifier_impl();
  final Parameter[] configParams =
      new Parameter[] {
        new Parameter_impl(SharedRedisResource.PARAM_HOST, redis.getContainerIpAddress()),
        new Parameter_impl(
            SharedRedisResource.PARAM_PORT, Integer.toString(redis.getMappedPort(REDIS_PORT)))
      };
  resourceSpecifier.setParameters(configParams);
  final Map<String, Object> config = Maps.newHashMap();
  resource.initialize(resourceSpecifier, config);

  Jedis jedis = resource.getJedis();
  String key = "key";
  String value = "value";
  jedis.lpush(key, new String[] {value});
  String result = jedis.rpop(key);
  assertEquals(value, result);

  jedis.lpush(key, new String[] {value});
  List<String> bresult = jedis.brpop(new String[] {key, "0"});
  assertEquals(key, bresult.get(0));
  assertEquals(value, bresult.get(1));
}
 
Example 5
Source File: AbstractRedisClient.java    From slime with MIT License 4 votes vote down vote up
protected static List<String> BRPOP(int timeout, String key, Jedis jedis) throws Exception {
	List<String> list = jedis.brpop(timeout, key);
	return list;
}
 
Example 6
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Map<String, String> brpop0(Jedis j, int timeout, String... keys) {
	List<String> l = j.brpop(timeout, keys);
	return convert4bpop(l);
}