Package redis.clients.jedis

Examples of redis.clients.jedis.ShardedJedis


   }

   private boolean getProcessingState(String queueUrl, int shard) {
     long ts1 = System.currentTimeMillis();
     boolean brokenJedis = false;
     ShardedJedis jedis = getResource();
     String suffix = "-F";
     try {
       return jedis.exists(queueUrl + "-" + shard + suffix);
     } catch (JedisException e) {
       brokenJedis = true;
       throw e;
     } finally {
       returnResource(jedis, brokenJedis);
View Full Code Here


     }

     List<String> memIdsRet = new LinkedList<String>();
     Set <String> memIds = null;
     boolean brokenJedis = false;
     ShardedJedis jedis = null;

     try {

       jedis = getResource();
       String key = queueUrl + "-" + shard + "-Q";

       long llen = jedis.zcount(key, System.currentTimeMillis() - retention * 1000L, System.currentTimeMillis());

       if (llen == 0L) {
         return Collections.emptyList();
       }

       //if no previousReceiptHandle and no nextReceiptHandle, just retrieve from beginning
       if (previousReceiptHandle == null && nextReceiptHandle == null) {
         memIds = jedis.zrangeByScore(key, System.currentTimeMillis() - retention * 1000L, System.currentTimeMillis(), 0, num);
         if (memIds != null){
           memIdsRet = new ArrayList<String>(memIds);
         }
       }
       //else find the score for previous receipt,
       //   if not exist, same as no previous receipt
       //   else use zrangeByScore with limit
       else if (previousReceiptHandle != null) {
         Double previousScore = jedis.zscore(key, previousReceiptHandle);
         if (previousScore == null) {
           memIds = jedis.zrangeByScore(key, System.currentTimeMillis() - retention * 1000L, System.currentTimeMillis(), 0, num);
           if (memIds != null){
             memIdsRet = new ArrayList<String>(memIds);
           }
         } else {

           long startTime = previousScore.longValue();
           if (startTime < System.currentTimeMillis() - retention * 1000L) {
             startTime = System.currentTimeMillis() - retention * 1000L;
           }

           int retCount = 0;
           int i = 0;
           boolean includeSet = (previousReceiptHandle == null) ? true : false;

           while (retCount < num && i < llen) {

             memIds = jedis.zrangeByScore(key, startTime, System.currentTimeMillis(), i, num);

             if (memIds.size() == 0) {
               break; // done
             }

             i += num; // next time, exclude the last one in this set

             for (String memId : memIds) {
               if (!includeSet) {
                 if (memId.equals(previousReceiptHandle)) {
                   includeSet = true;
                   //skip over this one since it was the last el of the last call
                 }
               } else {
                 memIdsRet.add(memId);
                 retCount++;
                 if (retCount >= num) {
                   break; //done
                 }                              
               }
             }
           }
         }
       } else { //this means previousReceiptHandle == null and nextReceiptHandle != null. Retrieve id backward
         //return result will exclude the nextReceiptHandle
         //retrieve nextReceiptHandle, get index. if not exist, retrieve from beginning.
         Long endRank = jedis.zrank(key, nextReceiptHandle);
         if (endRank == null) {
           memIds = jedis.zrangeByScore(key, System.currentTimeMillis() - retention * 1000L, System.currentTimeMillis(), 0, num);
           if (memIds != null){
             memIdsRet = new ArrayList<String>(memIds);
           }
         }
         //if index exist, retrieve based on index. When get result, remove expired id
         else {
           long startIndex = endRank - num;
           if (startIndex < 0 ){
             startIndex = 0;
           }
           Set <Tuple> memIdWithScores = jedis.zrangeWithScores(key, startIndex, endRank);
           long expiredTime = System.currentTimeMillis() - retention * 1000L;
           for (Tuple t: memIdWithScores) {
             if (t.getScore() < expiredTime || t.getElement().equals(nextReceiptHandle)) {
               continue;
             } else {
View Full Code Here

       retention = queue.getMsgRetentionPeriod();
     } else {
       retention = CMBProperties.getInstance().getCQSMessageRetentionPeriod();
     }

     ShardedJedis jedis = null;
     boolean brokenJedis = false;
     try {

       jedis = getResource();

       for (int shard=0; shard<numberOfShards; shard++) {

         boolean cacheAvailable = checkCacheConsistency(queueUrl, shard, true);

         if (!cacheAvailable) {
           throw new IllegalStateException("Redis cache not available");
         }

         //get the count number
         if(type == MessageCountType.ALL){
           messageCount += jedis.zcount(queueUrl + "-" + shard + "-Q", String.valueOf(System.currentTimeMillis() - retention * 1000L), "+inf");
         } else if (type == MessageCountType.INVISIBLE) {
           messageCount += jedis.zcount(queueUrl + "-" + shard + "-Q", String.valueOf(System.currentTimeMillis()+1), "+inf");
         }
       }

     } catch (JedisException e) {
       brokenJedis = true;
View Full Code Here

       retention = queue.getMsgRetentionPeriod();
     } else {
       retention = CMBProperties.getInstance().getCQSMessageRetentionPeriod();
     }

     ShardedJedis jedis = null;
     boolean brokenJedis = false;

     try {

       jedis = getResource();

       for (int shard=0; shard<numberOfShards; shard++) {          
         messageCount += jedis.zcount(queueUrl + "-" + shard + "-Q", String.valueOf(System.currentTimeMillis() - retention * 1000L), "+inf");
       }

     } catch (JedisException e) {
       brokenJedis = true;
       throw e;
View Full Code Here

       throw new IllegalStateException("Could not get id from underlying storage");
     }
     boolean cacheAvailable = checkCacheConsistency(queue.getRelativeUrl(), shard, true); //set in cache even if its filling
     String memId = null;
     boolean brokenJedis = false;
     ShardedJedis jedis = null;
     long ts1 = System.currentTimeMillis();
     try {
       if (cacheAvailable) {
         int delaySeconds = 0;
         if (queue.getDelaySeconds() > 0) {
           delaySeconds = queue.getDelaySeconds();
         }
         if (message.getAttributes().containsKey(CQSConstants.DELAY_SECONDS)) {
           delaySeconds = Integer.parseInt(message.getAttributes().get(CQSConstants.DELAY_SECONDS));
         }
         memId = getMemQueueMessage(messageId);
         jedis = getResource();
         jedis.zadd(queue.getRelativeUrl() + "-" + shard + "-Q", System.currentTimeMillis() + (delaySeconds * 1000), memId); //insert or update already existing                 
         //expire old message
         jedis.zremrangeByScore(queue.getRelativeUrl() + "-" + shard + "-Q","-inf",String.valueOf(System.currentTimeMillis() -
             (queue.getMsgRetentionPeriod() * 1000)));
         logger.debug("event=send_message cache_available=true msg_id= " + memId + " queue_url=" + queue.getAbsoluteUrl() + " shard=" + shard);
       } else {
         logger.debug("event=send_message cache_available=false msg_id= " + memId + " queue_url=" + queue.getAbsoluteUrl() + " shard=" + shard);
       }
View Full Code Here

    pool = pl;
  }

  @Override
  public void clearLock(String name) throws IOException {
    ShardedJedis jds = pool.getResource();
    jds.del(name+".lock");
    pool.returnResource(jds);
  }
View Full Code Here

    pool = pl;
  }

  @Override
  public boolean isLocked() throws IOException {
    ShardedJedis jds = pool.getResource();
    boolean ret = jds.exists(name.concat(".lock"));
    pool.returnResource(jds);
    return ret;
  }
View Full Code Here

  @Override
  public boolean obtain() throws IOException {
    if( isLocked() )
      return false;
    ShardedJedis jds = pool.getResource();
    String ret = jds.set(name+".lock", "1");
    pool.returnResource(jds);
    return ret != null;
  }
View Full Code Here

    return ret != null;
  }

  @Override
  public void release() throws IOException {
    ShardedJedis jds = pool.getResource();
    jds.del(name+".lock");
    pool.returnResource(jds);
  }
View Full Code Here

    if(lockFactory instanceof RedisLockFactory)
      super.setLockFactory(lockFactory);
  }
 
  private void open() {
    ShardedJedis rds = redisPool.getResource();
    byte[] size = rds.hget(getDirNameBytes(), ":size".getBytes());
    directorySize = 0;
    try {
      directorySize = ByteBuffer.wrap(size).asLongBuffer().get();
    }catch(Exception e){
      reloadSizeFromFiles();
View Full Code Here

TOP

Related Classes of redis.clients.jedis.ShardedJedis

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.