Search in sources :

Example 6 with AmqpSession

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

the class AmqpSendReceiveTest method testCloseBusyReceiver.

@Test(timeout = 60000)
public void testCloseBusyReceiver() 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");
        System.out.println("Sending message: " + message.getMessageId());
        sender.send(message);
    }
    sender.close();
    Queue queue = getProxyToQueue(getQueueName());
    Wait.assertEquals(MSG_COUNT, queue::getMessageCount);
    AmqpReceiver receiver1 = session.createReceiver(getQueueName());
    receiver1.flow(MSG_COUNT);
    AmqpMessage received = receiver1.receive(5, TimeUnit.SECONDS);
    assertNotNull("Should have got a message", received);
    assertEquals("msg0", received.getMessageId());
    receiver1.close();
    AmqpReceiver receiver2 = session.createReceiver(getQueueName());
    receiver2.flow(200);
    for (int i = 0; i < MSG_COUNT; ++i) {
        received = receiver2.receive(5, TimeUnit.SECONDS);
        assertNotNull("Should have got a message", received);
        System.out.println("Read message: " + received.getMessageId());
        assertEquals("msg" + i, received.getMessageId());
    }
    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) Queue(org.apache.activemq.artemis.core.server.Queue) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 7 with AmqpSession

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

the class AmqpSendReceiveTest method testReceiveMessageAndRefillCreditBeforeAccept.

@Test(timeout = 60000)
public void testReceiveMessageAndRefillCreditBeforeAccept() throws Exception {
    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);
    for (int i = 0; i < 2; i++) {
        AmqpMessage message = new AmqpMessage();
        message.setMessageId("msg" + i);
        sender.send(message);
    }
    sender.close();
    receiver.flow(1);
    AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
    assertNotNull(received);
    receiver.flow(1);
    received.accept();
    received = receiver.receive(10, TimeUnit.SECONDS);
    assertNotNull(received);
    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) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 8 with AmqpSession

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

the class AmqpSendReceiveTest method testReceiveWithJMSSelectorFilter.

@Test(timeout = 60000)
public void testReceiveWithJMSSelectorFilter() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpMessage message1 = new AmqpMessage();
    message1.setGroupId("abcdefg");
    message1.setApplicationProperty("sn", 100);
    AmqpMessage message2 = new AmqpMessage();
    message2.setGroupId("hijklm");
    message2.setApplicationProperty("sn", 200);
    AmqpSender sender = session.createSender(getQueueName());
    sender.send(message1);
    sender.send(message2);
    sender.close();
    AmqpReceiver receiver = session.createReceiver(getQueueName(), "sn = 100");
    receiver.flow(2);
    AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
    assertNotNull("Should have read a message", received);
    assertEquals(100, received.getApplicationProperty("sn"));
    assertEquals("abcdefg", received.getGroupId());
    received.accept();
    assertNull(receiver.receive(1, TimeUnit.SECONDS));
    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 9 with AmqpSession

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

the class AmqpSendReceiveTest method testReceiveMessageAndRefillCreditBeforeAcceptOnQueueAsync.

@Test(timeout = 60000)
public void testReceiveMessageAndRefillCreditBeforeAcceptOnQueueAsync() throws Exception {
    final AmqpClient client = createAmqpClient();
    final LinkedList<Throwable> errors = new LinkedList<>();
    final CountDownLatch receiverReady = new CountDownLatch(1);
    ExecutorService executorService = Executors.newCachedThreadPool();
    final String address = getQueueName();
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            try {
                LOG.info("Starting consumer connection");
                AmqpConnection connection = addConnection(client.connect());
                AmqpSession session = connection.createSession();
                AmqpReceiver receiver = session.createReceiver(address);
                receiver.flow(1);
                receiverReady.countDown();
                AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
                assertNotNull(received);
                receiver.flow(1);
                received.accept();
                received = receiver.receive(5, TimeUnit.SECONDS);
                assertNotNull(received);
                received.accept();
                receiver.close();
                connection.close();
            } catch (Exception error) {
                errors.add(error);
            }
        }
    });
    // producer
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            try {
                receiverReady.await(20, TimeUnit.SECONDS);
                AmqpConnection connection = addConnection(client.connect());
                AmqpSession session = connection.createSession();
                AmqpSender sender = session.createSender(address);
                for (int i = 0; i < 2; i++) {
                    AmqpMessage message = new AmqpMessage();
                    message.setMessageId("msg" + i);
                    sender.send(message);
                }
                sender.close();
                connection.close();
            } catch (Exception ignored) {
                ignored.printStackTrace();
            }
        }
    });
    executorService.shutdown();
    executorService.awaitTermination(20, TimeUnit.SECONDS);
    assertTrue("no errors: " + errors, errors.isEmpty());
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) LinkedList(java.util.LinkedList) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 10 with AmqpSession

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

the class AmqpSendReceiveTest method testQueueReceiverReadMessage.

@Test(timeout = 60000)
public void testQueueReceiverReadMessage() throws Exception {
    sendMessages(getQueueName(), 1);
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    Queue queueView = getProxyToQueue(getQueueName());
    assertEquals(1, queueView.getMessageCount());
    receiver.flow(1);
    assertNotNull(receiver.receive(5, TimeUnit.SECONDS));
    receiver.close();
    assertEquals(1, 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) 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