use of org.apache.qpid.server.message.MessageContainer in project qpid-broker-j by apache.
the class AbstractQueue method deliverSingleMessage.
MessageContainer deliverSingleMessage(QueueConsumer<?, ?> consumer) {
boolean queueEmpty = false;
MessageContainer messageContainer = null;
_queueConsumerManager.setNotified(consumer, false);
try {
if (!consumer.isSuspended()) {
messageContainer = attemptDelivery(consumer);
if (messageContainer.getMessageInstance() == null) {
if (consumer.acquires()) {
if (hasAvailableMessages()) {
notifyOtherConsumers(consumer);
}
}
consumer.noMessagesAvailable();
messageContainer = null;
} else {
_queueConsumerManager.setNotified(consumer, true);
}
} else {
// avoid referring old deleted queue entry in sub._queueContext._lastSeen
getNextAvailableEntry(consumer);
}
} finally {
consumer.flushBatched();
}
return messageContainer;
}
use of org.apache.qpid.server.message.MessageContainer in project qpid-broker-j by apache.
the class TrustStoreMessageSourceTest method testAddConsumer.
public void testAddConsumer() throws Exception {
final EnumSet<ConsumerOption> options = EnumSet.noneOf(ConsumerOption.class);
final ConsumerTarget target = mock(ConsumerTarget.class);
when(target.allocateCredit(any(ServerMessage.class))).thenReturn(true);
MessageInstanceConsumer consumer = _trustStoreMessageSource.addConsumer(target, null, ServerMessage.class, getTestName(), options, 0);
final MessageContainer messageContainer = consumer.pullMessage();
assertNotNull("Could not pull message of TrustStore", messageContainer);
final ServerMessage message = messageContainer.getMessageInstance().getMessage();
assertCertificates(getCertificatesFromMessage(message));
}
use of org.apache.qpid.server.message.MessageContainer in project qpid-broker-j by apache.
the class AbstractQueue method attemptDelivery.
/**
* Attempt delivery for the given consumer.
*
* Looks up the next node for the consumer and attempts to deliver it.
*
* @param sub the consumer
* @return true if we have completed all possible deliveries for this sub.
*/
private MessageContainer attemptDelivery(QueueConsumer<?, ?> sub) {
// avoid referring old deleted queue entry in sub._queueContext._lastSeen
QueueEntry node = getNextAvailableEntry(sub);
boolean subActive = sub.isActive() && !sub.isSuspended();
if (node != null && subActive && (sub.getPriority() == Integer.MAX_VALUE || noHigherPriorityWithCredit(sub, node))) {
if (_virtualHost.getState() != State.ACTIVE) {
throw new ConnectionScopedRuntimeException("Delivery halted owing to " + "virtualhost state " + _virtualHost.getState());
}
if (node.isAvailable() && mightAssign(sub, node)) {
if (sub.allocateCredit(node)) {
MessageReference messageReference = null;
if ((sub.acquires() && !assign(sub, node)) || (!sub.acquires() && (messageReference = node.newMessageReference()) == null)) {
// restore credit here that would have been taken away by allocateCredit since we didn't manage
// to acquire the entry for this consumer
sub.restoreCredit(node);
} else {
setLastSeenEntry(sub, node);
return new MessageContainer(node, messageReference);
}
} else {
sub.awaitCredit(node);
}
}
}
return NO_MESSAGES;
}
use of org.apache.qpid.server.message.MessageContainer in project qpid-broker-j by apache.
the class QueueConsumerImpl method pullMessage.
@Override
public MessageContainer pullMessage() {
MessageContainer messageContainer = _queue.deliverSingleMessage(this);
if (messageContainer != null) {
_deliveredCount.incrementAndGet();
_deliveredBytes.addAndGet(messageContainer.getMessageInstance().getMessage().getSizeIncludingHeader());
}
return messageContainer;
}
use of org.apache.qpid.server.message.MessageContainer in project qpid-broker-j by apache.
the class AbstractConsumerTarget method sendNextMessage.
@Override
public boolean sendNextMessage() {
MessageContainer messageContainer = null;
MessageInstanceConsumer consumer = null;
boolean iteratedCompleteList = false;
while (messageContainer == null) {
if (_pullIterator == null || !_pullIterator.hasNext()) {
if (iteratedCompleteList) {
break;
}
iteratedCompleteList = true;
_pullIterator = getConsumers().iterator();
}
if (_pullIterator.hasNext()) {
consumer = _pullIterator.next();
messageContainer = consumer.pullMessage();
}
}
if (messageContainer != null) {
MessageInstance entry = messageContainer.getMessageInstance();
try {
send(consumer, entry, false);
} catch (MessageConversionException mce) {
restoreCredit(entry.getMessage());
final TransactionLogResource owningResource = entry.getOwningResource();
if (owningResource instanceof MessageSource) {
final MessageSource.MessageConversionExceptionHandlingPolicy handlingPolicy = ((MessageSource) owningResource).getMessageConversionExceptionHandlingPolicy();
switch(handlingPolicy) {
case CLOSE:
entry.release(consumer);
throw new ConnectionScopedRuntimeException(String.format("Unable to convert message %s for this consumer", entry.getMessage()), mce);
case ROUTE_TO_ALTERNATE:
if (consumer.acquires()) {
int enqueues = entry.routeToAlternate(null, null);
if (enqueues == 0) {
LOGGER.info("Failed to convert message {} for this consumer because '{}'." + " Message discarded.", entry.getMessage(), mce.getMessage());
} else {
LOGGER.info("Failed to convert message {} for this consumer because '{}'." + " Message routed to alternate.", entry.getMessage(), mce.getMessage());
}
} else {
LOGGER.info("Failed to convert message {} for this browser because '{}'." + " Message skipped.", entry.getMessage(), mce.getMessage());
}
break;
case REJECT:
entry.reject(consumer);
entry.release(consumer);
LOGGER.info("Failed to convert message {} for this consumer because '{}'." + " Message skipped.", entry.getMessage(), mce.getMessage());
break;
default:
throw new ServerScopedRuntimeException("Unrecognised policy " + handlingPolicy);
}
} else {
throw new ConnectionScopedRuntimeException(String.format("Unable to convert message %s for this consumer", entry.getMessage()), mce);
}
} finally {
if (messageContainer.getMessageReference() != null) {
messageContainer.getMessageReference().release();
}
}
return true;
} else {
return false;
}
}
Aggregations