Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.FolderNotFoundException;
Expand Down Expand Up @@ -86,6 +85,30 @@ protected void doStop() throws Exception {
super.doStop();
}

/**
* Returns the max number of messages to be processed. Will return -1 if no maximum is set
*
* @return
*/
private int getMaxNumberOfMessages() {
int maxMessagesPerPoll = (getEndpoint().getMaxMessagesPerPoll() == 0) ? -1 : getEndpoint().getMaxMessagesPerPoll();
int fetchSize = getEndpoint().getConfiguration().getFetchSize();

if (hasMessageLimit(fetchSize)) {
return fetchSize;
}

if (hasMessageLimit(maxMessagesPerPoll)) {
return maxMessagesPerPoll;
}

return -1;
}

private boolean hasMessageLimit(int limitValue) {
return limitValue >= 0;
}

protected int poll() throws Exception {
// must reset for each poll
shutdownRunningTask = null;
Expand Down Expand Up @@ -174,13 +197,6 @@ protected int poll() throws Exception {

public int processBatch(Queue<Object> exchanges) throws Exception {
int total = exchanges.size();

// limit if needed
if (maxMessagesPerPoll > 0 && total > maxMessagesPerPoll) {
LOG.debug("Limiting to maximum messages to poll {} as there were {} messages in this poll.", maxMessagesPerPoll, total);
total = maxMessagesPerPoll;
}

for (int index = 0; index < total && isBatchAllowed(); index++) {
// only loop if we are started (allowed to run)
Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
Expand Down Expand Up @@ -269,13 +285,22 @@ private List<KeyValueHolder<String, Message>> retrieveMessages() throws Messagin
}
}

int maxMessage = getMaxNumberOfMessages();
boolean messageNeedsValidation = getEndpoint().getIdempotentRepository() != null;
for (Message message : messages) {
if (hasMessageLimit(maxMessage) && answer.size() >= maxMessage) {
break;
}
String key = getEndpoint().getMailUidGenerator().generateUuid(getEndpoint(), message);
if (isValidMessage(key, message)) {
if (!messageNeedsValidation || isValidMessage(key, message)) {
answer.add(new KeyValueHolder<>(key, message));
}
}

if (LOG.isDebugEnabled()) {
LOG.debug("Fetching {} messages. Total {} messages.", answer.size(), messages.length);
}

return answer;
}

Expand Down Expand Up @@ -328,16 +353,9 @@ private SearchTerm computeSearchTerm() {
}

protected Queue<Exchange> createExchanges(List<KeyValueHolder<String, Message>> messages) throws MessagingException {
Queue<Exchange> answer = new LinkedList<>();

int fetchSize = getEndpoint().getConfiguration().getFetchSize();
int count = fetchSize == -1 ? messages.size() : Math.min(fetchSize, messages.size());

if (LOG.isDebugEnabled()) {
LOG.debug("Fetching {} messages. Total {} messages.", count, messages.size());
}
Queue<Exchange> answer = new LinkedList<Exchange>();

for (int i = 0; i < count; i++) {
for (int i = 0; i < messages.size(); i++) {
try {
KeyValueHolder<String, Message> holder = messages.get(i);
String key = holder.getKey();
Expand Down