Package com.comcast.cqs.model

Examples of com.comcast.cqs.model.CQSQueue


     * @throws Exception
     */
    public long getQueueMessageCount(String queueUrl, boolean processHiddenIds) throws Exception  {
     
      long messageCount = 0;
      CQSQueue queue = CQSCache.getCachedQueue(queueUrl);
      int numberOfShards = 1;
     
      if (queue != null) {
        numberOfShards = queue.getNumberOfShards();
      }

    ShardedJedis jedis = null;
        boolean brokenJedis = false;
       
View Full Code Here


     * @throws Exception
     */
    public long getCacheQueueMessageCount(String queueUrl) throws Exception  {
     
      long messageCount = 0;
      CQSQueue queue = CQSCache.getCachedQueue(queueUrl);
      int numberOfShards = 1;
     
      if (queue != null) {
        numberOfShards = queue.getNumberOfShards();
      }

    ShardedJedis jedis = null;
        boolean brokenJedis = false;
       
View Full Code Here

     * @throws Exception
     */
    private long getCount(String queueUrl, String suffix, boolean processHiddenIds) throws Exception  {
     
      long messageCount = 0;
      CQSQueue queue = CQSCache.getCachedQueue(queueUrl);
      int numberOfShards = 1;
     
      if (queue != null) {
        numberOfShards = queue.getNumberOfShards();
      }

    ShardedJedis jedis = null;
        boolean brokenJedis = false;
       
View Full Code Here

  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
   
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
   
      CQSQueue queue = CQSCache.getCachedQueue(user, request);
       
        HashMap<String, String> attributes = Util.fillAllSetAttributesRequests(request);
        HashMap<String, String> postVars = new HashMap<String, String>();
       
        for (String attributeName : attributes.keySet()) {
         
            String value = attributes.get(attributeName);
       
            if (attributeName.equals(CQSConstants.VISIBILITY_TIMEOUT)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 0 || v > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.VISIBILITY_TIMEOUT + " must be between 0 and " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut() + " seconds");
                }
               
              queue.setVisibilityTO(v);
                postVars.put(CQSConstants.COL_VISIBILITY_TO, value);
           
            } else if (attributeName.equals(CQSConstants.POLICY)) {
             
            if (value != null && !value.equals("")) {
               
              // validate policy before updating
             
              try {
                new CMBPolicy(value);
              } catch (Exception ex) {
                      logger.warn("event=invalid_policy queue_url=" + queue.getRelativeUrl() + " policy=" + value, ex);
                throw ex;
              }
            }
             
                queue.setPolicy(value);
                postVars.put(CQSConstants.COL_POLICY, value);
           
            } else if (attributeName.equals(CQSConstants.MAXIMUM_MESSAGE_SIZE)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v > CMBProperties.getInstance().getCQSMaxMessageSize()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.MAXIMUM_MESSAGE_SIZE + " cannot be over " + CMBProperties.getInstance().getCQSMaxMessageSize() + " bytes");
                }
               
              queue.setMaxMsgSize(v);
                postVars.put(CQSConstants.COL_MAX_MSG_SIZE, value);
           
            } else if (attributeName.equals(CQSConstants.MESSAGE_RETENTION_PERIOD)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < CMBProperties.getInstance().getCQSMinMessageRetentionPeriod() || v > CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " must be between 1 and " + CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod() + " seconds");
                }
             
                queue.setMsgRetentionPeriod(v);
                postVars.put(CQSConstants.COL_MSG_RETENTION_PERIOD, value);
           
            } else if (attributeName.equals(CQSConstants.DELAY_SECONDS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 0 || v > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.DELAY_SECONDS + " must be less than " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds() + " seconds");
                }
               
              queue.setDelaySeconds(v);
                postVars.put(CQSConstants.COL_DELAY_SECONDS, value);
          
            } else if (attributeName.equals(CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 0 || v > CMBProperties.getInstance().getCMBRequestTimeoutSec()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " must be 20 seconds or less");
                }
               
              queue.setReceiveMessageWaitTimeSeconds(v);
                postVars.put(CQSConstants.COL_WAIT_TIME_SECONDS, value);
          
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_PARTITIONS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 1) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " should be at least 1");
                }
               
              queue.setNumberOfPartitions(v);
                postVars.put(CQSConstants.COL_NUMBER_PARTITIONS, value);
          
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_SHARDS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 1 || v > 100) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_SHARDS + " should be between 1 and 100");
                }
               
              queue.setNumberOfShards(v);
                postVars.put(CQSConstants.COL_NUMBER_SHARDS, value);
               
              for (int shard=0; shard<v; shard++) {
                PersistenceFactory.getCQSMessagePersistence().checkCacheConsistency(queue.getRelativeUrl(), shard, false);
              }

            } else if (attributeName.equals(CQSConstants.IS_COMPRESSED)) {
             
              boolean isCompressed = Boolean.parseBoolean(value);
              queue.setCompressed(isCompressed);
                postVars.put(CQSConstants.COL_COMPRESSED, value);
             
            } else {
                throw new CMBException(CMBErrorCodes.InvalidAttributeName, "Attribute.Name: " + attributeName + " is not a valid attribute");
            }
        }

        PersistenceFactory.getQueuePersistence().updateQueueAttribute(queue.getRelativeUrl(), postVars);
       
        String out = CQSQueuePopulator.setQueueAttributesResponse();
        writeResponse(out, response);
       
        return true;
View Full Code Here

  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
   
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
   
      CQSQueue queue = CQSCache.getCachedQueue(user, request);
   
        HashMap<String, String> msgParam = new HashMap<String, String>();
        List<CQSMessage> messageList = PersistenceFactory.getCQSMessagePersistence().receiveMessage(queue, msgParam);
       
    String out = "";
View Full Code Here

  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
   
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();

      CQSQueue queue = CQSCache.getCachedQueue(user, request);
        String receiptHandle = request.getParameter(CQSConstants.RECEIPT_HANDLE);
       
        if (receiptHandle == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "ReceiptHandle not found");
        }
View Full Code Here

        if (queueName == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "Parameter QueueName not found");
        }

        CQSQueue queue = PersistenceFactory.getQueuePersistence().getQueue(user.getUserId(), queueName);

        if (queue == null) {
            throw new CMBException(CQSErrorCodes.NonExistentQueue, "Queue not found with name " + queueName + " for user " + user.getUserId());
        }
       
View Full Code Here

  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
   
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();

      CQSQueue queue = CQSCache.getCachedQueue(user, request);
     
      int numberOfShards = queue.getNumberOfShards();
     
        PersistenceFactory.getQueuePersistence().deleteQueue(queue.getRelativeUrl());
       
        // clear all shards in redis
       
        for (int shard=0; shard<numberOfShards; shard++) {
            PersistenceFactory.getCQSMessagePersistence().clearQueue(queue.getRelativeUrl(), shard);
        }
       
        CQSCache.removeQueue(queue.getRelativeUrl());
       
        String out = CQSQueuePopulator.getDeleteQueueResponse();
        writeResponse(out, response);
       
        return true;
View Full Code Here

 
  public static String sendMessage(String userId, String relativeQueueUrl, String messageBody, Integer delaySeconds) throws Exception {
   
    long ts1 = System.currentTimeMillis();

      CQSQueue queue = CQSCache.getCachedQueue(relativeQueueUrl);

      if (queue == null) {
        throw new CMBException(CMBErrorCodes.InternalError, "Unknown queue " + relativeQueueUrl);
      }
       
    if (messageBody == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "MessageBody not found");
        }

        if (messageBody.length() > CMBProperties.getInstance().getCQSMaxMessageSize()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Value for parameter MessageBody is invalid. Reason: Message body must be shorter than " + CMBProperties.getInstance().getCQSMaxMessageSize() + " bytes");
        }
       
        if (!Util.isValidUnicode(messageBody)) {
            throw new CMBException(CMBErrorCodes.InvalidMessageContents, "The message contains characters outside the allowed set.");
        }
       
        HashMap<String, String> attributes = new HashMap<String, String>();

        if (delaySeconds != null) {
          if (delaySeconds < 0 || delaySeconds > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
              throw new CMBException(CMBErrorCodes.InvalidParameterValue, "DelaySeconds should be from 0 to " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
          } else {
              attributes.put(CQSConstants.DELAY_SECONDS, "" + delaySeconds);
          }
        }

        attributes.put(CQSConstants.SENDER_ID, userId);
        attributes.put(CQSConstants.SENT_TIMESTAMP, "" + System.currentTimeMillis());
        attributes.put(CQSConstants.APPROXIMATE_RECEIVE_COUNT, "0");
        attributes.put(CQSConstants.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, "");
       
        CQSMessage message = new CQSMessage(messageBody, attributes);
       
    int shard = rand.nextInt(queue.getNumberOfShards());

        String receiptHandle = PersistenceFactory.getCQSMessagePersistence().sendMessage(queue, shard, message);

        if (receiptHandle == null || receiptHandle.isEmpty()) {
            throw new CMBException(CMBErrorCodes.InternalError, "Failed to add message to queue");
        }
       
        try {
          CQSLongPollSender.send(queue.getArn());
        } catch (Exception ex) {
          logger.warn("event=failed_to_send_longpoll_notification", ex);
        }
       
    long ts2 = System.currentTimeMillis();
View Full Code Here

       
        if (!m.matches()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "QueueName " + queueName + " is invalid. Only alphanumeric and hyphen and underscore allowed.");
        }

        CQSQueue newQueue = new CQSQueue(queueName, user.getUserId());
        CQSQueue existingQueue = PersistenceFactory.getQueuePersistence().getQueue(newQueue.getRelativeUrl());
       
        boolean throwQueueExistsError = false;
        int index = 1;
        String attributeName = request.getParameter("Attribute." + index + ".Name");
        int numberOfShards = 1;
       
        while (attributeName != null) {
           
            String attributeValue = request.getParameter("Attribute." + index + ".Value");

            if (attributeValue == null) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Attribute: " + attributeName + " does not have a corresponding attribute value");
            }

            if (attributeName.equals(CQSConstants.VISIBILITY_TIMEOUT)) {
             
              int visibilityTo = 0;
             
              try {
                visibilityTo = Integer.parseInt(attributeValue);
              } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.VISIBILITY_TIMEOUT + " must be an integer value");
              }
             
            if (visibilityTo < 0 || visibilityTo > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.VISIBILITY_TIMEOUT + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut());
            }

            if (existingQueue != null && existingQueue.getVisibilityTO() != visibilityTo) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setVisibilityTO(visibilityTo);
               
            } else if (attributeName.equals(CQSConstants.POLICY)) {
             
                if (existingQueue != null && !existingQueue.getPolicy().equals(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setPolicy(attributeValue);
               
            } else if (attributeName.equals(CQSConstants.MAXIMUM_MESSAGE_SIZE)) {
             
              int maxMessageSize = 0;
             
              try {
                maxMessageSize = Integer.parseInt(attributeValue);
              } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MAXIMUM_MESSAGE_SIZE + " must be an integer value");
              }
             
            if (maxMessageSize < 0 || maxMessageSize > CMBProperties.getInstance().getCQSMaxMessageSize()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MAXIMUM_MESSAGE_SIZE + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageSize());
            }

            if (existingQueue != null && existingQueue.getMaxMsgSize() != maxMessageSize) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setMaxMsgSize(maxMessageSize);
               
            } else if (attributeName.equals(CQSConstants.MESSAGE_RETENTION_PERIOD)) {
             
              int messageRetentionPeriod = 0;
             
              try {
                messageRetentionPeriod = Integer.parseInt(attributeValue);
              } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " must be an integer value");
              }
             
            if (messageRetentionPeriod < 0 || messageRetentionPeriod > CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod());
            }
             
                if (existingQueue != null && existingQueue.getMsgRetentionPeriod() != messageRetentionPeriod) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setMsgRetentionPeriod(messageRetentionPeriod);
               
            } else if (attributeName.equals(CQSConstants.DELAY_SECONDS)) {
             
                if (existingQueue != null && existingQueue.getDelaySeconds() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                int delaySeconds = 0;
               
                try {
                  delaySeconds = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.DELAY_SECONDS + " must be an integer value");
                }
               
            if (delaySeconds < 0 || delaySeconds > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.DELAY_SECONDS + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
            }
               
                newQueue.setDelaySeconds(delaySeconds);
               
            } else if (attributeName.equals(CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS)) {
             
                if (existingQueue != null && existingQueue.getReceiveMessageWaitTimeSeconds() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                int receiveMessageWaitTimeSeconds = 0;
               
                try {
                  receiveMessageWaitTimeSeconds = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " must be an integer value");
                }
               
            if (receiveMessageWaitTimeSeconds < 0 || receiveMessageWaitTimeSeconds > CMBProperties.getInstance().getCMBRequestTimeoutSec()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
            }
               
                newQueue.setReceiveMessageWaitTimeSeconds(receiveMessageWaitTimeSeconds);
               
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_PARTITIONS)) {
             
              // number of Cassandra rows used to store messages, default is 100 - this number can be set at queue creation time
              // or changed later via SetQueueAttributes() API

              if (existingQueue != null && existingQueue.getNumberOfPartitions() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                int numberOfPartitions = 0;
               
                try {
                  numberOfPartitions = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " must be an integer value");
                }
               
            if (numberOfPartitions < 1) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " should be at least 1");
            }
               
                newQueue.setNumberOfPartitions(numberOfPartitions);
               
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_SHARDS)) {
             
              // number of internal queues used for random sharding to trade message ordering for enhanced single-queue
              // throughput - this number can only be set at queue creation time and cannot be changed later
             
                if (existingQueue != null && existingQueue.getNumberOfShards() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                try {
View Full Code Here

TOP

Related Classes of com.comcast.cqs.model.CQSQueue

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.