Search in sources :

Example 6 with MessageInstance

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

the class UnacknowledgedMessageMapImpl method acknowledge.

@Override
public Collection<MessageConsumerAssociation> acknowledge(long deliveryTag, boolean multiple) {
    if (multiple) {
        Map<Long, MessageConsumerAssociation> ackedMessageMap = new LinkedHashMap<>();
        collect(deliveryTag, multiple, ackedMessageMap);
        remove(ackedMessageMap.keySet());
        List<MessageConsumerAssociation> acknowledged = new ArrayList<>();
        for (MessageConsumerAssociation messageConsumerAssociation : ackedMessageMap.values()) {
            MessageInstance instance = messageConsumerAssociation.getMessageInstance();
            if (instance.makeAcquisitionUnstealable(messageConsumerAssociation.getConsumer())) {
                acknowledged.add(messageConsumerAssociation);
            }
        }
        return acknowledged;
    } else {
        final MessageConsumerAssociation association = remove(deliveryTag, true);
        if (association != null) {
            final MessageInstance messageInstance = association.getMessageInstance();
            if (messageInstance != null && messageInstance.makeAcquisitionUnstealable(association.getConsumer())) {
                return Collections.singleton(association);
            }
        }
        return Collections.emptySet();
    }
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with MessageInstance

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

the class UnacknowledgedMessageMapTest method createMessageInstance.

private MessageInstance createMessageInstance(final int id) {
    MessageInstance instance = mock(MessageInstance.class);
    when(instance.makeAcquisitionUnstealable(_consumer)).thenReturn(Boolean.TRUE);
    when(instance.getAcquiringConsumer()).thenReturn(_consumer);
    ServerMessage message = mock(ServerMessage.class);
    when(message.getSize()).thenReturn(0L);
    when(instance.getMessage()).thenReturn(message);
    return instance;
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) ServerMessage(org.apache.qpid.server.message.ServerMessage)

Example 8 with MessageInstance

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

the class AutoCommitTransactionTest method createTestQueueEntries.

private Collection<MessageInstance> createTestQueueEntries(boolean[] queueDurableFlags, boolean[] messagePersistentFlags) {
    Collection<MessageInstance> queueEntries = new ArrayList<MessageInstance>();
    assertTrue("Boolean arrays must be the same length", queueDurableFlags.length == messagePersistentFlags.length);
    for (int i = 0; i < queueDurableFlags.length; i++) {
        final BaseQueue queue = createTestAMQQueue(queueDurableFlags[i]);
        final ServerMessage message = createTestMessage(messagePersistentFlags[i]);
        final boolean hasRecord = queueDurableFlags[i] && messagePersistentFlags[i];
        queueEntries.add(new MockMessageInstance() {

            @Override
            public ServerMessage getMessage() {
                return message;
            }

            @Override
            public TransactionLogResource getOwningResource() {
                return queue;
            }

            @Override
            public MessageEnqueueRecord getEnqueueRecord() {
                if (hasRecord) {
                    return mock(MessageEnqueueRecord.class);
                } else {
                    return null;
                }
            }
        });
    }
    return queueEntries;
}
Also used : MockMessageInstance(org.apache.qpid.server.queue.MockMessageInstance) MessageInstance(org.apache.qpid.server.message.MessageInstance) MockMessageInstance(org.apache.qpid.server.queue.MockMessageInstance) ArrayList(java.util.ArrayList) BaseQueue(org.apache.qpid.server.queue.BaseQueue) ServerMessage(org.apache.qpid.server.message.ServerMessage) MessageEnqueueRecord(org.apache.qpid.server.store.MessageEnqueueRecord) TransactionLogResource(org.apache.qpid.server.store.TransactionLogResource)

Example 9 with MessageInstance

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

the class ConsumerTarget_1_0Test method testTTLAdjustedOnSend.

public void testTTLAdjustedOnSend() throws Exception {
    final MessageInstanceConsumer comsumer = mock(MessageInstanceConsumer.class);
    long ttl = 2000L;
    long arrivalTime = System.currentTimeMillis() - 1000L;
    final Header header = new Header();
    header.setTtl(UnsignedInteger.valueOf(ttl));
    final Message_1_0 message = createTestMessage(header, arrivalTime);
    final MessageInstance messageInstance = mock(MessageInstance.class);
    when(messageInstance.getMessage()).thenReturn(message);
    AtomicReference<QpidByteBuffer> payloadRef = new AtomicReference<>();
    doAnswer(invocation -> {
        final Object[] args = invocation.getArguments();
        Transfer transfer = (Transfer) args[0];
        QpidByteBuffer transferPayload = transfer.getPayload();
        QpidByteBuffer payloadCopy = transferPayload.duplicate();
        payloadRef.set(payloadCopy);
        return null;
    }).when(_sendingLinkEndpoint).transfer(any(Transfer.class), anyBoolean());
    _consumerTarget.doSend(comsumer, messageInstance, false);
    verify(_sendingLinkEndpoint, times(1)).transfer(any(Transfer.class), anyBoolean());
    final List<EncodingRetainingSection<?>> sections;
    try (QpidByteBuffer payload = payloadRef.get()) {
        sections = new SectionDecoderImpl(_describedTypeRegistry.getSectionDecoderRegistry()).parseAll(payload);
    }
    Header sentHeader = null;
    for (EncodingRetainingSection<?> section : sections) {
        if (section instanceof HeaderSection) {
            sentHeader = ((HeaderSection) section).getValue();
        }
    }
    assertNotNull("Header is not found", sentHeader);
    assertNotNull("Ttl is not set", sentHeader.getTtl());
    assertTrue("Unexpected ttl", sentHeader.getTtl().longValue() <= 1000);
}
Also used : EncodingRetainingSection(org.apache.qpid.server.protocol.v1_0.type.messaging.EncodingRetainingSection) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) HeaderSection(org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection) MessageInstance(org.apache.qpid.server.message.MessageInstance) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) SectionDecoderImpl(org.apache.qpid.server.protocol.v1_0.messaging.SectionDecoderImpl) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 10 with MessageInstance

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

the class StandardQueueTest method testEnqueueDequeuedEntry.

/**
 * Tests that entry in dequeued state are not enqueued and not delivered to consumer
 */
public void testEnqueueDequeuedEntry() throws Exception {
    // create a queue where each even entry is considered a dequeued
    AbstractQueue queue = new DequeuedQueue(getVirtualHost());
    queue.create();
    // create a consumer
    TestConsumerTarget consumer = new TestConsumerTarget();
    // register consumer
    queue.addConsumer(consumer, null, createMessage(-1l).getClass(), "test", EnumSet.of(ConsumerOption.ACQUIRES, ConsumerOption.SEES_REQUEUES), 0);
    // put test messages into a queue
    putGivenNumberOfMessages(queue, 4);
    while (consumer.processPending()) ;
    // assert received messages
    List<MessageInstance> messages = consumer.getMessages();
    assertEquals("Only 2 messages should be returned", 2, messages.size());
    assertEquals("ID of first message should be 1", 1l, (messages.get(0).getMessage()).getMessageNumber());
    assertEquals("ID of second message should be 3", 3l, (messages.get(1).getMessage()).getMessageNumber());
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) TestConsumerTarget(org.apache.qpid.server.consumer.TestConsumerTarget)

Aggregations

MessageInstance (org.apache.qpid.server.message.MessageInstance)24 MessageInstanceConsumer (org.apache.qpid.server.message.MessageInstanceConsumer)8 ServerMessage (org.apache.qpid.server.message.ServerMessage)8 MessageEnqueueRecord (org.apache.qpid.server.store.MessageEnqueueRecord)5 TransactionLogResource (org.apache.qpid.server.store.TransactionLogResource)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)3 TestConsumerTarget (org.apache.qpid.server.consumer.TestConsumerTarget)3 Action (org.apache.qpid.server.util.Action)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 MessageDestination (org.apache.qpid.server.message.MessageDestination)2 MessageSource (org.apache.qpid.server.message.MessageSource)2 Queue (org.apache.qpid.server.model.Queue)2 Visitor (org.apache.qpid.server.protocol.v0_8.UnacknowledgedMessageMap.Visitor)2 MockMessageInstance (org.apache.qpid.server.queue.MockMessageInstance)2 Transaction (org.apache.qpid.server.store.Transaction)2 PrivilegedAction (java.security.PrivilegedAction)1 List (java.util.List)1