Search in sources :

Example 16 with AmqpReceiver

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

the class AmqpTempDestinationTest method doTestCreateDynamicReceiver.

@SuppressWarnings("unchecked")
protected void doTestCreateDynamicReceiver(boolean topic) throws Exception {
    Source source = createDynamicSource(topic);
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpReceiver receiver = session.createReceiver(source);
    assertNotNull(receiver);
    Source remoteSource = (Source) receiver.getEndpoint().getRemoteSource();
    assertTrue(remoteSource.getDynamic());
    assertTrue(remoteSource.getDurable().equals(TerminusDurability.NONE));
    assertTrue(remoteSource.getExpiryPolicy().equals(TerminusExpiryPolicy.LINK_DETACH));
    // Check the dynamic node lifetime-policy
    Map<Symbol, Object> dynamicNodeProperties = remoteSource.getDynamicNodeProperties();
    assertTrue(dynamicNodeProperties.containsKey(LIFETIME_POLICY));
    assertEquals(DeleteOnClose.getInstance(), dynamicNodeProperties.get(LIFETIME_POLICY));
    Queue queueView = getProxyToQueue(remoteSource.getAddress());
    assertNotNull(queueView);
    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) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Queue(org.apache.activemq.artemis.core.server.Queue) Source(org.apache.qpid.proton.amqp.messaging.Source)

Example 17 with AmqpReceiver

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

the class AmqpTransactionTest method testReceiversCommitAndRollbackWithMultipleSessionsInSingleTX.

@Test(timeout = 60000)
public void testReceiversCommitAndRollbackWithMultipleSessionsInSingleTX() throws Exception {
    final int NUM_MESSAGES = 10;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    try {
        // Root TXN session controls all TXN send lifetimes.
        AmqpSession txnSession = connection.createSession();
        // Normal Session which won't create an TXN itself
        AmqpSession session = connection.createSession();
        AmqpSender sender = session.createSender(getQueueName());
        for (int i = 0; i < NUM_MESSAGES + 1; ++i) {
            AmqpMessage message = new AmqpMessage();
            message.setText("Test-Message");
            message.setApplicationProperty("msgId", i);
            sender.send(message, txnSession.getTransactionId());
        }
        // Read all messages from the Queue, do not accept them yet.
        AmqpReceiver receiver = session.createReceiver(getQueueName());
        ArrayList<AmqpMessage> messages = new ArrayList<>(NUM_MESSAGES);
        receiver.flow((NUM_MESSAGES + 2) * 2);
        for (int i = 0; i < NUM_MESSAGES; ++i) {
            AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
            assertNotNull(message);
            messages.add(message);
        }
        // Commit half the consumed messages
        txnSession.begin();
        for (int i = 0; i < NUM_MESSAGES / 2; ++i) {
            messages.get(i).accept(txnSession);
        }
        txnSession.commit();
        // Rollback the other half the consumed messages
        txnSession.begin();
        for (int i = NUM_MESSAGES / 2; i < NUM_MESSAGES; ++i) {
            messages.get(i).accept(txnSession, false);
        }
        txnSession.rollback();
        {
            AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
            assertNotNull(message);
            assertEquals(NUM_MESSAGES, message.getApplicationProperty("msgId"));
            message.release();
        }
        // The final message should still be pending.
        {
            AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
            receiver.flow(1);
            assertNotNull(message);
            assertEquals(NUM_MESSAGES, message.getApplicationProperty("msgId"));
            message.release();
        }
    } finally {
        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) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 18 with AmqpReceiver

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

the class AmqpTransactionTest method testMultipleSessionReceiversInSingleTXNWithCommit.

@Test(timeout = 60000)
public void testMultipleSessionReceiversInSingleTXNWithCommit() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    // Load up the Queue with some messages
    {
        AmqpSession session = connection.createSession();
        AmqpSender sender = session.createSender(getQueueName());
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        sender.send(message);
        sender.send(message);
        sender.send(message);
        sender.close();
    }
    // Root TXN session controls all TXN send lifetimes.
    AmqpSession txnSession = connection.createSession();
    // Create some sender sessions
    AmqpSession session1 = connection.createSession();
    AmqpSession session2 = connection.createSession();
    AmqpSession session3 = connection.createSession();
    // Sender linked to each session
    AmqpReceiver receiver1 = session1.createReceiver(getQueueName());
    AmqpReceiver receiver2 = session2.createReceiver(getQueueName());
    AmqpReceiver receiver3 = session3.createReceiver(getQueueName());
    final Queue queue = getProxyToQueue(getQueueName());
    assertEquals(3, queue.getMessageCount());
    // Begin the transaction that all senders will operate in.
    txnSession.begin();
    assertTrue(txnSession.isInTransaction());
    receiver1.flow(1);
    receiver2.flow(1);
    receiver3.flow(1);
    AmqpMessage message1 = receiver1.receive(5, TimeUnit.SECONDS);
    AmqpMessage message2 = receiver2.receive(5, TimeUnit.SECONDS);
    AmqpMessage message3 = receiver3.receive(5, TimeUnit.SECONDS);
    message1.accept(txnSession);
    message2.accept(txnSession);
    message3.accept(txnSession);
    assertEquals(3, queue.getMessageCount());
    txnSession.commit();
    assertEquals(0, queue.getMessageCount());
}
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 19 with AmqpReceiver

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

the class AmqpTransactionTest method testSendersCommitAndRollbackWithMultipleSessionsInSingleTX.

// ----- Tests Ported from AmqpNetLite client -----------------------------//
@Test(timeout = 60000)
public void testSendersCommitAndRollbackWithMultipleSessionsInSingleTX() throws Exception {
    final int NUM_MESSAGES = 5;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    // Root TXN session controls all TXN send lifetimes.
    AmqpSession txnSession = connection.createSession();
    // Normal Session which won't create an TXN itself
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    // Commit TXN work from a sender.
    txnSession.begin();
    for (int i = 0; i < NUM_MESSAGES; ++i) {
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        sender.send(message, txnSession.getTransactionId());
    }
    txnSession.commit();
    // Rollback an additional batch of TXN work from a sender.
    txnSession.begin();
    for (int i = 0; i < NUM_MESSAGES; ++i) {
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        sender.send(message, txnSession.getTransactionId());
    }
    txnSession.rollback();
    // Commit more TXN work from a sender.
    txnSession.begin();
    for (int i = 0; i < NUM_MESSAGES; ++i) {
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        sender.send(message, txnSession.getTransactionId());
    }
    txnSession.commit();
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    receiver.flow(NUM_MESSAGES * 2);
    for (int i = 0; i < NUM_MESSAGES * 2; ++i) {
        AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
        assertNotNull(message);
        message.accept(txnSession);
    }
    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 20 with AmqpReceiver

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

the class AmqpTransactionTest method testCommitAndRollbackWithMultipleSessionsInSingleTXNoSettlement.

@Test(timeout = 60000)
public void testCommitAndRollbackWithMultipleSessionsInSingleTXNoSettlement() throws Exception {
    final int NUM_MESSAGES = 10;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = client.connect();
    // Root TXN session controls all TXN send lifetimes.
    AmqpSession txnSession = connection.createSession();
    // Normal Session which won't create an TXN itself
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    for (int i = 0; i < NUM_MESSAGES; ++i) {
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        message.setApplicationProperty("msgId", i);
        sender.send(message, txnSession.getTransactionId());
    }
    // Read all messages from the Queue, do not accept them yet.
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    receiver.flow(2);
    AmqpMessage message1 = receiver.receive(5, TimeUnit.SECONDS);
    AmqpMessage message2 = receiver.receive(5, TimeUnit.SECONDS);
    // Accept the first one in a TXN and send a new message in that TXN as well
    txnSession.begin();
    {
        // This will result in message [0[ being consumed once we commit.
        message1.accept(txnSession, false);
        System.out.println("Commit: accepting message: " + message1.getApplicationProperty("msgId"));
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        message.setApplicationProperty("msgId", NUM_MESSAGES);
        sender.send(message, txnSession.getTransactionId());
    }
    txnSession.commit();
    // Accept the second one in a TXN and send a new message in that TXN as well but rollback
    txnSession.begin();
    {
        message2.accept(txnSession, false);
        System.out.println("Rollback: accepting message: " + message2.getApplicationProperty("msgId"));
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        message.setApplicationProperty("msgId", NUM_MESSAGES + 1);
        sender.send(message, txnSession.getTransactionId());
    }
    txnSession.rollback();
    // This releases message [1]
    message2.release();
    // Should be ten message available for dispatch given that we sent and committed one, and
    // releases another we had previously received.
    receiver.flow(10);
    for (int i = 1; i <= NUM_MESSAGES; ++i) {
        AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
        assertNotNull("Expected a message for: " + i, message);
        System.out.println("Accepting message: " + message.getApplicationProperty("msgId"));
        assertEquals(i, message.getApplicationProperty("msgId"));
        message.accept();
    }
    // Should be nothing left.
    receiver.flow(1);
    assertNull(receiver.receive(1, TimeUnit.SECONDS));
    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)

Aggregations

AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)118 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)118 AmqpReceiver (org.apache.activemq.transport.amqp.client.AmqpReceiver)118 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)118 Test (org.junit.Test)101 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)94 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)61 Queue (org.apache.activemq.artemis.core.server.Queue)59 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)17 Source (org.apache.qpid.proton.amqp.messaging.Source)17 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)12 Receiver (org.apache.qpid.proton.engine.Receiver)6 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)3 Wait (org.apache.activemq.artemis.tests.util.Wait)3 AmqpValidator (org.apache.activemq.transport.amqp.client.AmqpValidator)3 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)2