*
* this method assumes that you are holding the finished tasks lock
* locks when invoking it. violate this assumption at
* your own peril */
private void fillFuture( ReceiveMessageFuture f ){
ReceiveMessageResult r = new ReceiveMessageResult();
LinkedList<Message> messages = new LinkedList<Message>();
r.setMessages(messages);
Exception exception = null;
if ( !finishedTasks.isEmpty() ) {
ReceiveMessageBatchTask t = finishedTasks.getFirst();
exception = t.getException();
int retrieved = 0;
boolean batchDone = false;
while ( retrieved < f.getRequestedSize() )
{
Message m = t.removeMessage();
// a non-empty batch can still give back a null
// message if the message expired.
if ( null != m) {
messages.add(m);
++retrieved;
}
else {
batchDone = true;
break;
}
}
//we may have just drained the batch.
batchDone = batchDone || t.isEmpty() || ( exception != null );
if ( batchDone) {
finishedTasks.removeFirst();
}
r.setMessages(messages);
}
//if after the above runs the exception is not null,
//the finished batch has encountered an error, and we will
//report that in the Future. Otherwise, we will fill