requestExpTime = 0;
return null;
}
String id;
Message message;
Integer deliveryAttempts = null;
int lastPrior = -1;
int insertionIndex = -1;
int prior;
Vector deliverables = new Vector();
DMQManager dmqManager = null;
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> messageIds.size() = " + messageIds.size());
// Delivering to a listener.
if (toListener) {
// Browsing the identifiers of the messages to deliver.
while (! messageIds.isEmpty()) {
id = (String) messageIds.remove(0);
save();
message = (Message) messagesTable.get(id);
// Message still exists.
if (message != null) {
// Delivering it if valid.
if (message.isValid(System.currentTimeMillis())) {
deliveredIds.put(id, id);
// Setting the message's deliveryCount and denied fields.
deliveryAttempts = (Integer) deniedMsgs.get(id);
if (deliveryAttempts == null)
message.setDeliveryCount(1);
else {
message.setDeliveryCount(deliveryAttempts.intValue() +1);
message.setRedelivered();
}
// Inserting it according to its priority.
if (lastPrior == -1 || message.getPriority() == lastPrior)
insertionIndex++;
else {
insertionIndex = 0;
while (insertionIndex < deliverables.size()) {
prior =
((Message) deliverables.get(insertionIndex)).getPriority();
if (prior >= message.getPriority())
insertionIndex++;
else
break;
}
}
lastPrior = message.getPriority();
deliverables.add(insertionIndex, message.getFullMessage().clone());
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, this + ": message " + id + " added for delivery.");
} else {
// Invalid message: removing and adding it to the vector of dead
// messages.
messagesTable.remove(id);
// Deleting the message, if needed.
if (durable)
message.delete();
// Setting the message's deliveryCount, denied and expired fields.
deliveryAttempts = (Integer) deniedMsgs.remove(id);
if (deliveryAttempts != null) {
message.setDeliveryCount(deliveryAttempts.intValue() +1);
message.setRedelivered();
}
if (dmqManager == null) {
dmqManager = new DMQManager(dmqId, null);
}
nbMsgsSentToDMQSinceCreation++;
dmqManager.addDeadMessage(message.getFullMessage(), MessageErrorConstants.EXPIRED);
}
} else {
// Message has already been deleted.
deniedMsgs.remove(id);
}
}
} else {
// Delivering to a receiver: getting the highest priority message.
int highestP = -1;
Message keptMsg = null;
// Browsing the non delivered messages.
int i = 0;
while (i < messageIds.size()) {
id = (String) messageIds.get(i);
message = (Message) messagesTable.get(id);
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> message = " + message);
// Message still exists.
if (message != null) {
// Checking valid message.
if (message.isValid(System.currentTimeMillis())) {
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> valid message");
// Higher priority: keeping the message.
if (message.getPriority() > highestP) {
highestP = message.getPriority();
keptMsg = message;
}
// get next message
i++;
} else {
// Invalid message: removing and adding it to the vector of dead
// messages.
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> invalid message");
messageIds.remove(id);
save();
messagesTable.remove(id);
// Deleting the message, if needed.
if (durable)
message.delete();
// Setting the message's deliveryCount, denied and expired fields.
deliveryAttempts = (Integer) deniedMsgs.remove(id);
if (deliveryAttempts != null) {
message.setDeliveryCount(deliveryAttempts.intValue());
message.setRedelivered();
}
if (dmqManager == null) {
dmqManager = new DMQManager(dmqId, null);
}
nbMsgsSentToDMQSinceCreation++;
dmqManager.addDeadMessage(message.getFullMessage(), MessageErrorConstants.EXPIRED);
}
} else {
// Message has already been deleted.
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, " -> deleted message");
messageIds.remove(id);
deniedMsgs.remove(id);
save();
}
}
// Putting the kept message in the vector.
if (keptMsg != null) {
messageIds.remove(keptMsg.getIdentifier());
deliveredIds.put(keptMsg.getIdentifier(), keptMsg.getIdentifier());
save();
// Setting the message's deliveryCount and denied fields.
deliveryAttempts = (Integer) deniedMsgs.get(keptMsg.getIdentifier());
if (deliveryAttempts == null)
keptMsg.setDeliveryCount(1);
else {
keptMsg.setDeliveryCount(deliveryAttempts.intValue() +1);
keptMsg.setRedelivered();
}
deliverables.add(keptMsg.getFullMessage().clone());
if (logger.isLoggable(BasicLevel.DEBUG))
logger
.log(BasicLevel.DEBUG, this + ": message " + keptMsg.getIdentifier() + " added for delivery.");
} else {
i++;
}
}