Search in sources :

Example 71 with ServerMessage

use of org.apache.qpid.server.message.ServerMessage in project qpid-broker-j by apache.

the class ConsumerTarget_0_10 method sendToDLQOrDiscard.

private void sendToDLQOrDiscard(final MessageInstanceConsumer consumer, MessageInstance entry) {
    final ServerMessage msg = entry.getMessage();
    int requeues = 0;
    if (entry.makeAcquisitionUnstealable(consumer)) {
        requeues = entry.routeToAlternate(new Action<MessageInstance>() {

            @Override
            public void performAction(final MessageInstance requeueEntry) {
                getEventLogger().message(ChannelMessages.DEADLETTERMSG(msg.getMessageNumber(), requeueEntry.getOwningResource().getName()));
            }
        }, null);
    }
    if (requeues == 0) {
        TransactionLogResource owningResource = entry.getOwningResource();
        if (owningResource instanceof Queue) {
            final Queue<?> queue = (Queue<?>) owningResource;
            final MessageDestination alternateBindingDestination = queue.getAlternateBindingDestination();
            if (alternateBindingDestination != null) {
                getEventLogger().message(ChannelMessages.DISCARDMSG_NOROUTE(msg.getMessageNumber(), alternateBindingDestination.getName()));
            } else {
                getEventLogger().message(ChannelMessages.DISCARDMSG_NOALTEXCH(msg.getMessageNumber(), queue.getName(), msg.getInitialRoutingAddress()));
            }
        }
    }
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) Action(org.apache.qpid.server.util.Action) MessageDestination(org.apache.qpid.server.message.MessageDestination) ServerMessage(org.apache.qpid.server.message.ServerMessage) TransactionLogResource(org.apache.qpid.server.store.TransactionLogResource) Queue(org.apache.qpid.server.model.Queue)

Example 72 with ServerMessage

use of org.apache.qpid.server.message.ServerMessage in project qpid-broker-j by apache.

the class ConsumerTarget_0_10 method doSend.

@Override
public void doSend(final MessageInstanceConsumer consumer, final MessageInstance entry, boolean batch) {
    ServerMessage serverMsg = entry.getMessage();
    MessageTransfer xfr;
    DeliveryProperties deliveryProps;
    MessageProperties messageProps = null;
    MessageTransferMessage msg;
    MessageConverter<? super ServerMessage, MessageTransferMessage> converter = null;
    if (serverMsg instanceof MessageTransferMessage) {
        msg = (MessageTransferMessage) serverMsg;
    } else {
        converter = (MessageConverter<? super ServerMessage, MessageTransferMessage>) MessageConverterRegistry.getConverter(serverMsg.getClass(), MessageTransferMessage.class);
        msg = converter.convert(serverMsg, _session.getAddressSpace());
    }
    DeliveryProperties origDeliveryProps = msg.getHeader() == null ? null : msg.getHeader().getDeliveryProperties();
    messageProps = msg.getHeader() == null ? null : msg.getHeader().getMessageProperties();
    deliveryProps = new DeliveryProperties();
    if (origDeliveryProps != null) {
        if (origDeliveryProps.hasDeliveryMode()) {
            deliveryProps.setDeliveryMode(origDeliveryProps.getDeliveryMode());
        }
        if (origDeliveryProps.hasExchange()) {
            deliveryProps.setExchange(origDeliveryProps.getExchange());
        }
        if (origDeliveryProps.hasExpiration()) {
            deliveryProps.setExpiration(origDeliveryProps.getExpiration());
        }
        if (origDeliveryProps.hasPriority()) {
            deliveryProps.setPriority(origDeliveryProps.getPriority());
        }
        if (origDeliveryProps.hasRoutingKey()) {
            deliveryProps.setRoutingKey(origDeliveryProps.getRoutingKey());
        }
        if (origDeliveryProps.hasTimestamp()) {
            deliveryProps.setTimestamp(origDeliveryProps.getTimestamp());
        }
        if (origDeliveryProps.hasTtl()) {
            deliveryProps.setTtl(origDeliveryProps.getTtl());
        }
    }
    deliveryProps.setRedelivered(entry.isRedelivered());
    boolean msgCompressed = messageProps != null && GZIPUtils.GZIP_CONTENT_ENCODING.equals(messageProps.getContentEncoding());
    QpidByteBuffer bodyBuffer = msg.getBody();
    boolean compressionSupported = _session.getConnection().getConnectionDelegate().isCompressionSupported();
    if (msgCompressed && !compressionSupported && bodyBuffer != null) {
        QpidByteBuffer uncompressedBuffer = inflateIfPossible(bodyBuffer);
        messageProps.setContentEncoding(null);
        bodyBuffer.dispose();
        bodyBuffer = uncompressedBuffer;
    } else if (!msgCompressed && compressionSupported && (messageProps == null || messageProps.getContentEncoding() == null) && bodyBuffer != null && bodyBuffer.remaining() > _session.getConnection().getMessageCompressionThreshold()) {
        QpidByteBuffer compressedBuffers = deflateIfPossible(bodyBuffer);
        if (messageProps == null) {
            messageProps = new MessageProperties();
        }
        messageProps.setContentEncoding(GZIPUtils.GZIP_CONTENT_ENCODING);
        bodyBuffer.dispose();
        bodyBuffer = compressedBuffers;
    }
    Header header = new Header(deliveryProps, messageProps, msg.getHeader() == null ? null : msg.getHeader().getNonStandardProperties());
    xfr = batch ? new MessageTransfer(_name, _acceptMode, _acquireMode, header, bodyBuffer, BATCHED) : new MessageTransfer(_name, _acceptMode, _acquireMode, header, bodyBuffer);
    if (bodyBuffer != null) {
        bodyBuffer.dispose();
        bodyBuffer = null;
    }
    if (_acceptMode == MessageAcceptMode.NONE && _acquireMode != MessageAcquireMode.PRE_ACQUIRED) {
        xfr.setCompletionListener(new MessageAcceptCompletionListener(this, consumer, _session, entry, _flowMode == MessageFlowMode.WINDOW));
    } else if (_flowMode == MessageFlowMode.WINDOW) {
        final long messageSize = entry.getMessage().getSize();
        xfr.setCompletionListener(new Method.CompletionListener() {

            @Override
            public void onComplete(Method method) {
                deferredAddCredit(1, messageSize);
            }
        });
    }
    _postIdSettingAction.setXfr(xfr);
    _postIdSettingAction.setAction(null);
    if (_acquireMode == MessageAcquireMode.PRE_ACQUIRED) {
        entry.incrementDeliveryCount();
    }
    if (_acceptMode == MessageAcceptMode.EXPLICIT) {
        _postIdSettingAction.setAction(new ExplicitAcceptDispositionChangeListener(entry, this, consumer));
    } else if (_acquireMode != MessageAcquireMode.PRE_ACQUIRED) {
        _postIdSettingAction.setAction(new ImplicitAcceptDispositionChangeListener(entry, this, consumer));
    }
    _session.sendMessage(xfr, _postIdSettingAction);
    xfr.dispose();
    if (converter != null) {
        converter.dispose(msg);
    }
    _postIdSettingAction.setAction(null);
    _postIdSettingAction.setXfr(null);
    if (_acceptMode == MessageAcceptMode.NONE && _acquireMode == MessageAcquireMode.PRE_ACQUIRED) {
        forceDequeue(entry, false);
    } else if (_acquireMode == MessageAcquireMode.PRE_ACQUIRED) {
        addUnacknowledgedMessage(entry);
    }
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage) DeliveryProperties(org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties) Method(org.apache.qpid.server.protocol.v0_10.transport.Method) Header(org.apache.qpid.server.protocol.v0_10.transport.Header) MessageProperties(org.apache.qpid.server.protocol.v0_10.transport.MessageProperties) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) MessageTransfer(org.apache.qpid.server.protocol.v0_10.transport.MessageTransfer)

Example 73 with ServerMessage

use of org.apache.qpid.server.message.ServerMessage in project qpid-broker-j by apache.

the class AbstractQueueTestBase method testEnqueueMessageThenRegisterConsumer.

public void testEnqueueMessageThenRegisterConsumer() throws Exception, InterruptedException {
    ServerMessage messageA = createMessage(new Long(24));
    _queue.enqueue(messageA, null, null);
    _consumer = (QueueConsumer<?, ?>) _queue.addConsumer(_consumerTarget, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    while (_consumerTarget.processPending()) ;
    assertEquals(messageA, _consumer.getQueueContext().getLastSeenEntry().getMessage());
    assertNull("There should be no releasedEntry after an enqueue", _consumer.getQueueContext().getReleasedEntry());
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage)

Example 74 with ServerMessage

use of org.apache.qpid.server.message.ServerMessage in project qpid-broker-j by apache.

the class AbstractQueueTestBase method testReleaseMessageThatBecomesExpiredIsNotRedelivered.

/**
 * Tests that a released message that becomes expired is not resent to the subscriber.
 * This tests ensures that SimpleAMQQueue<?>Entry.getNextAvailableEntry avoids expired entries.
 * Verifies also that the QueueContext._releasedEntry is reset to null after the entry has been reset.
 */
public void testReleaseMessageThatBecomesExpiredIsNotRedelivered() throws Exception {
    ServerMessage messageA = createMessage(new Long(24));
    final CountDownLatch sendIndicator = new CountDownLatch(1);
    _consumerTarget = new TestConsumerTarget() {

        @Override
        public void notifyWork() {
            while (processPending()) ;
        }

        @Override
        public void send(MessageInstanceConsumer consumer, MessageInstance entry, boolean batch) {
            try {
                super.send(consumer, entry, batch);
            } finally {
                sendIndicator.countDown();
            }
        }
    };
    _consumer = (QueueConsumer<?, ?>) _queue.addConsumer(_consumerTarget, null, messageA.getClass(), "test", EnumSet.of(ConsumerOption.SEES_REQUEUES, ConsumerOption.ACQUIRES), 0);
    final ArrayList<QueueEntry> queueEntries = new ArrayList<QueueEntry>();
    EntryListAddingAction postEnqueueAction = new EntryListAddingAction(queueEntries);
    /* Enqueue one message with expiration set for a short time in the future */
    final long expiration = System.currentTimeMillis() + 100L;
    when(messageA.getExpiration()).thenReturn(expiration);
    _queue.enqueue(messageA, postEnqueueAction, null);
    assertTrue("Message was not sent during expected time interval", sendIndicator.await(5000, TimeUnit.MILLISECONDS));
    assertEquals("Unexpected total number of messages sent to consumer", 1, _consumerTarget.getMessages().size());
    QueueEntry queueEntry = queueEntries.get(0);
    final CountDownLatch dequeueIndicator = new CountDownLatch(1);
    queueEntry.addStateChangeListener(new StateChangeListener<MessageInstance, MessageInstance.EntryState>() {

        @Override
        public void stateChanged(MessageInstance object, MessageInstance.EntryState oldState, MessageInstance.EntryState newState) {
            if (newState.equals(MessageInstance.DEQUEUED_STATE)) {
                dequeueIndicator.countDown();
            }
        }
    });
    assertFalse("Redelivery flag should not be set", queueEntry.isRedelivered());
    /* Wait a little more to be sure that message will have expired, then release the first message only, causing it to be requeued */
    while (!queueEntry.expired() && System.currentTimeMillis() <= expiration) {
        Thread.sleep(10);
    }
    assertTrue("Expecting the queue entry to be now expired", queueEntry.expired());
    queueEntry.release();
    assertTrue("Message was not de-queued due to expiration", dequeueIndicator.await(5000, TimeUnit.MILLISECONDS));
    assertEquals("Total number of messages sent should not have changed", 1, _consumerTarget.getMessages().size());
    assertFalse("Redelivery flag should not be set", queueEntry.isRedelivered());
    // QueueContext#_releasedEntry is updated after notification, thus, we need to make sure that it is updated
    long waitLoopLimit = 10;
    while (_consumer.getQueueContext().getReleasedEntry() != null && waitLoopLimit-- > 0) {
        Thread.sleep(10);
    }
    assertNull("releasedEntry should be cleared after requeue processed:" + _consumer.getQueueContext().getReleasedEntry(), _consumer.getQueueContext().getReleasedEntry());
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage) ArrayList(java.util.ArrayList) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget) MessageInstance(org.apache.qpid.server.message.MessageInstance)

Example 75 with ServerMessage

use of org.apache.qpid.server.message.ServerMessage in project qpid-broker-j by apache.

the class AbstractQueueTestBase method testRingOverflowPolicyMaxSize.

public void testRingOverflowPolicyMaxSize() {
    Map<String, Object> attributes = new HashMap<>(_arguments);
    attributes.put(Queue.OVERFLOW_POLICY, OverflowPolicy.RING);
    attributes.put(Queue.MAXIMUM_QUEUE_DEPTH_MESSAGES, 4);
    attributes.put(Queue.MAXIMUM_QUEUE_DEPTH_BYTES, 100);
    Queue<?> queue = getQueue();
    queue.setAttributes(attributes);
    ServerMessage message = createMessage(new Long(24), 10, 10);
    when(message.getArrivalTime()).thenReturn(10l);
    queue.enqueue(message, null, null);
    message = createMessage(new Long(25), 10, 10);
    when(message.getArrivalTime()).thenReturn(50l);
    queue.enqueue(message, null, null);
    message = createMessage(new Long(26), 20, 10);
    when(message.getArrivalTime()).thenReturn(200l);
    queue.enqueue(message, null, null);
    message = createMessage(new Long(27), 20, 10);
    when(message.getArrivalTime()).thenReturn(200l);
    queue.enqueue(message, null, null);
    assertEquals("Wrong number of messages in queue", 4, queue.getQueueDepthMessages());
    assertEquals("Wrong size of messages in queue", 100, queue.getQueueDepthBytes());
    message = createMessage(new Long(27), 20, 10);
    when(message.getArrivalTime()).thenReturn(500l);
    queue.enqueue(message, null, null);
    assertEquals("Wrong number of messages in queue", 3, queue.getQueueDepthMessages());
    assertEquals("Wrong size of messages in queue", 90, queue.getQueueDepthBytes());
    assertEquals("Wrong oldest message", 200l, ((AbstractQueue) queue).getEntries().getOldestEntry().getMessage().getArrivalTime());
}
Also used : HashMap(java.util.HashMap) ServerMessage(org.apache.qpid.server.message.ServerMessage)

Aggregations

ServerMessage (org.apache.qpid.server.message.ServerMessage)103 HashMap (java.util.HashMap)26 AMQMessageHeader (org.apache.qpid.server.message.AMQMessageHeader)16 TransactionLogResource (org.apache.qpid.server.store.TransactionLogResource)16 Exchange (org.apache.qpid.server.model.Exchange)14 MessageReference (org.apache.qpid.server.message.MessageReference)13 ArrayList (java.util.ArrayList)9 MessageInstance (org.apache.qpid.server.message.MessageInstance)8 InstanceProperties (org.apache.qpid.server.message.InstanceProperties)7 Queue (org.apache.qpid.server.model.Queue)7 StoredMessage (org.apache.qpid.server.store.StoredMessage)6 MessageEnqueueRecord (org.apache.qpid.server.store.MessageEnqueueRecord)4 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)3 TestConsumerTarget (org.apache.qpid.server.consumer.TestConsumerTarget)3 MessageDeletedException (org.apache.qpid.server.message.MessageDeletedException)3 PrivilegedAction (java.security.PrivilegedAction)2 UUID (java.util.UUID)2 MessageDestination (org.apache.qpid.server.message.MessageDestination)2 MessageInstanceConsumer (org.apache.qpid.server.message.MessageInstanceConsumer)2 RoutingResult (org.apache.qpid.server.message.RoutingResult)2