Search in sources :

Example 11 with AmqpSession

use of org.apache.activemq.transport.amqp.client.AmqpSession in project activemq-artemis by apache.

the class AmqpSendReceiveTest method testAdvancedLinkFlowControl.

@Test(timeout = 60000)
public void testAdvancedLinkFlowControl() throws Exception {
    final int MSG_COUNT = 20;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    for (int i = 0; i < MSG_COUNT; i++) {
        AmqpMessage message = new AmqpMessage();
        message.setMessageId("msg" + i);
        message.setMessageAnnotation("serialNo", i);
        message.setText("Test-Message");
        sender.send(message);
    }
    sender.close();
    LOG.info("Attempting to read first two messages with receiver #1");
    AmqpReceiver receiver1 = session.createReceiver(getQueueName());
    receiver1.flow(2);
    AmqpMessage message1 = receiver1.receive(10, TimeUnit.SECONDS);
    AmqpMessage message2 = receiver1.receive(10, TimeUnit.SECONDS);
    assertNotNull("Should have read message 1", message1);
    assertNotNull("Should have read message 2", message2);
    assertEquals("msg0", message1.getMessageId());
    assertEquals("msg1", message2.getMessageId());
    message1.accept();
    message2.accept();
    LOG.info("Attempting to read next two messages with receiver #2");
    AmqpReceiver receiver2 = session.createReceiver(getQueueName());
    receiver2.flow(2);
    AmqpMessage message3 = receiver2.receive(10, TimeUnit.SECONDS);
    AmqpMessage message4 = receiver2.receive(10, TimeUnit.SECONDS);
    assertNotNull("Should have read message 3", message3);
    assertNotNull("Should have read message 4", message4);
    assertEquals("msg2", message3.getMessageId());
    assertEquals("msg3", message4.getMessageId());
    message3.accept();
    message4.accept();
    LOG.info("Attempting to read remaining messages with receiver #1");
    receiver1.flow(MSG_COUNT - 4);
    for (int i = 4; i < MSG_COUNT; i++) {
        AmqpMessage message = receiver1.receive(10, TimeUnit.SECONDS);
        assertNotNull("Should have read a message", message);
        assertEquals("msg" + i, message.getMessageId());
        message.accept();
    }
    receiver1.close();
    receiver2.close();
    connection.close();
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 12 with AmqpSession

use of org.apache.activemq.transport.amqp.client.AmqpSession in project activemq-artemis by apache.

the class AmqpSendReceiveTest method doTestBrokerRestartAndDurability.

private void doTestBrokerRestartAndDurability(boolean durable, boolean enforceHeader) throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    final Queue queueView1 = getProxyToQueue(getQueueName());
    // Create default message that should be sent as non-durable
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message -> non-durable");
    message.setMessageId("ID:Message:1");
    if (durable) {
        message.setDurable(true);
    } else {
        if (enforceHeader) {
            message.setDurable(false);
            assertNotNull(message.getWrappedMessage().getHeader());
        } else {
            assertNull(message.getWrappedMessage().getHeader());
        }
    }
    sender.send(message);
    connection.close();
    Wait.assertEquals(1, queueView1::getMessageCount);
    // Restart the server and the Queue should be empty
    server.stop();
    server.start();
    // Reconnect now
    connection = addConnection(client.connect());
    session = connection.createSession();
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    final Queue queueView2 = getProxyToQueue(getQueueName());
    if (durable) {
        Wait.assertTrue("Message should not have returned", () -> queueView2.getMessageCount() == 1);
    } else {
        Wait.assertTrue("Message should have been restored", () -> queueView2.getMessageCount() == 0);
    }
    receiver.flow(1);
    message = receiver.receive(1, TimeUnit.SECONDS);
    if (durable) {
        assertNotNull("Should have read a message", message);
    } else {
        assertNull("Should not have read a message", message);
    }
    connection.close();
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Queue(org.apache.activemq.artemis.core.server.Queue) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage)

Example 13 with AmqpSession

use of org.apache.activemq.transport.amqp.client.AmqpSession in project activemq-artemis by apache.

the class AmqpSendReceiveTest method testReceiveMessageBeyondAckedAmountQueue.

@Test(timeout = 60000)
public void testReceiveMessageBeyondAckedAmountQueue() throws Exception {
    final int MSG_COUNT = 50;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    final String address = getQueueName();
    AmqpReceiver receiver = session.createReceiver(address);
    AmqpSender sender = session.createSender(address);
    final Queue destinationView = getProxyToQueue(address);
    for (int i = 0; i < MSG_COUNT; i++) {
        AmqpMessage message = new AmqpMessage();
        message.setMessageId("msg" + i);
        sender.send(message);
    }
    List<AmqpMessage> pendingAcks = new ArrayList<>();
    for (int i = 0; i < MSG_COUNT; i++) {
        receiver.flow(1);
        AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
        assertNotNull(received);
        pendingAcks.add(received);
    }
    // Send one more to check in-flight stays at zero with no credit and all
    // pending messages settled.
    AmqpMessage message = new AmqpMessage();
    message.setMessageId("msg-final");
    sender.send(message);
    for (AmqpMessage pendingAck : pendingAcks) {
        pendingAck.accept();
    }
    Wait.assertEquals(0, destinationView::getDeliveringCount);
    sender.close();
    receiver.close();
    connection.close();
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) ArrayList(java.util.ArrayList) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Queue(org.apache.activemq.artemis.core.server.Queue) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 14 with AmqpSession

use of org.apache.activemq.transport.amqp.client.AmqpSession in project activemq-artemis by apache.

the class AmqpSendReceiveTest method testSimpleSendOneReceiveOne.

@Test(timeout = 60000)
public void testSimpleSendOneReceiveOne() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    AmqpMessage message = new AmqpMessage();
    message.setMessageId("msg" + 1);
    message.setMessageAnnotation("serialNo", 1);
    message.setText("Test-Message");
    sender.send(message);
    sender.close();
    LOG.info("Attempting to read message with receiver");
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    receiver.flow(2);
    AmqpMessage received = receiver.receive(10, TimeUnit.SECONDS);
    assertNotNull("Should have read message", received);
    assertEquals("msg1", received.getMessageId());
    received.accept();
    receiver.close();
    connection.close();
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 15 with AmqpSession

use of org.apache.activemq.transport.amqp.client.AmqpSession in project activemq-artemis by apache.

the class AmqpSendReceiveTest method testSecondReceiverOnQueueGetsAllUnconsumedMessages.

@Test(timeout = 60000)
public void testSecondReceiverOnQueueGetsAllUnconsumedMessages() throws Exception {
    int MSG_COUNT = 20;
    sendMessages(getQueueName(), MSG_COUNT);
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpReceiver receiver1 = session.createReceiver(getQueueName());
    final Queue queueView = getProxyToQueue(getQueueName());
    assertEquals(MSG_COUNT, queueView.getMessageCount());
    receiver1.flow(20);
    assertTrue("Should have dispatch to prefetch", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisfied() throws Exception {
            return queueView.getDeliveringCount() >= 2;
        }
    }, TimeUnit.SECONDS.toMillis(5), TimeUnit.MILLISECONDS.toMillis(50)));
    receiver1.close();
    AmqpReceiver receiver2 = session.createReceiver(getQueueName());
    assertEquals(1, server.getTotalConsumerCount());
    receiver2.flow(MSG_COUNT * 2);
    AmqpMessage message = receiver2.receive(5, TimeUnit.SECONDS);
    assertNotNull(message);
    message.accept();
    message = receiver2.receive(5, TimeUnit.SECONDS);
    assertNotNull(message);
    message.accept();
    assertTrue("Should have ack'd two", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisfied() throws Exception {
            return queueView.getMessagesAcknowledged() == 2;
        }
    }, TimeUnit.SECONDS.toMillis(5), TimeUnit.MILLISECONDS.toMillis(50)));
    receiver2.close();
    assertEquals(MSG_COUNT - 2, queueView.getMessageCount());
    connection.close();
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Queue(org.apache.activemq.artemis.core.server.Queue) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Aggregations

AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)169 AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)168 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)168 Test (org.junit.Test)143 AmqpReceiver (org.apache.activemq.transport.amqp.client.AmqpReceiver)123 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)115 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)92 Queue (org.apache.activemq.artemis.core.server.Queue)70 Source (org.apache.qpid.proton.amqp.messaging.Source)25 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)22 AmqpValidator (org.apache.activemq.transport.amqp.client.AmqpValidator)15 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)14 Receiver (org.apache.qpid.proton.engine.Receiver)12 Sender (org.apache.qpid.proton.engine.Sender)7 URI (java.net.URI)6 HashMap (java.util.HashMap)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Map (java.util.Map)5 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)5 ArrayList (java.util.ArrayList)4