if (memId == null) {
throw new IllegalStateException("Underlying storage layer returned a message that was not requested");
}
CQSMessage message = messageIdToMessage.getValue();
if (message == null) {
logger.warn("event=message_is_null msg_id=" + messageIdToMessage.getKey());
continue;
}
//check if message returned is expired. This will only happen if payload coming from payload-cache. Cassandra
//expires old messages just fine. If expired, skip over and continue.
if (isMessageExpired(queue, memId)) {
try {
logger.debug("event=message_expired message_id=" + memId);
persistenceStorage.deleteMessage(queue.getRelativeUrl(), message.getMessageId());
} catch (Exception e) {
//its fine to ignore this exception since message must have been auto-deleted from Cassandra
//but we need to do this to clean up payloadcache
logger.debug("event=message_already_deleted_in_cassandra msg_id=" + message.getMessageId());
}
continue;
}
//hide message if visibilityTO is greater than 0
if (visibilityTO > 0) {
long ts1 = System.currentTimeMillis();
jedis.hset(queue.getRelativeUrl() + "-" + shard + "-H", memId, Long.toString(System.currentTimeMillis() + (visibilityTO * 1000)));
long ts2 = System.currentTimeMillis();
CQSControllerServlet.valueAccumulator.addToCounter(AccumulatorName.RedisTime, (ts2 - ts1));
}
message.setMessageId(memId);
message.setReceiptHandle(memId);
//get message-attributes and populate in message
Map<String, String> msgAttrs = (message.getAttributes() != null) ? message.getAttributes() : new HashMap<String, String>();
List<String> attrs = jedis.hmget(queue.getRelativeUrl() + "-" + shard + "-A-" + memId, CQSConstants.REDIS_APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, CQSConstants.REDIS_APPROXIMATE_RECEIVE_COUNT);
if (attrs.get(0) == null) {
String firstRecvTS = Long.toString(System.currentTimeMillis());
jedis.hset(queue.getRelativeUrl() + "-" + shard + "-A-" + memId, CQSConstants.REDIS_APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, firstRecvTS);
msgAttrs.put(CQSConstants.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, firstRecvTS);
} else {
msgAttrs.put(CQSConstants.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, attrs.get(0));
}
int recvCount = 1;
if (attrs.get(1) != null) {
recvCount = Integer.parseInt(attrs.get(1)) + 1;
}
jedis.hset(queue.getRelativeUrl() + "-" + shard + "-A-" + memId, CQSConstants.REDIS_APPROXIMATE_RECEIVE_COUNT, Integer.toString(recvCount));
jedis.expire(queue.getRelativeUrl() + "-" + shard + "-A-" + memId, 3600 * 24 * 14); //14 days expiration if not deleted
msgAttrs.put(CQSConstants.APPROXIMATE_RECEIVE_COUNT, Integer.toString(recvCount));
message.setAttributes(msgAttrs);
ret.add(message);
}
if (ret.size() > 0) { //There may be cases where the underlying persistent message has two memIds while
//cache filling. In such cases trying to retrieve message for the second memId may result in no message