Search in sources :

Example 81 with AmqpMessage

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

the class AmqpTransactionTest method testMultipleSessionSendersInSingleTXNWithRollback.

@Test(timeout = 60000)
public void testMultipleSessionSendersInSingleTXNWithRollback() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    // 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
    AmqpSender sender1 = session1.createSender(getQueueName());
    AmqpSender sender2 = session2.createSender(getQueueName());
    AmqpSender sender3 = session3.createSender(getQueueName());
    final Queue queue = getProxyToQueue(getQueueName());
    assertEquals(0, queue.getMessageCount());
    // Begin the transaction that all senders will operate in.
    txnSession.begin();
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    assertTrue(txnSession.isInTransaction());
    sender1.send(message, txnSession.getTransactionId());
    sender2.send(message, txnSession.getTransactionId());
    sender3.send(message, txnSession.getTransactionId());
    assertEquals(0, queue.getMessageCount());
    txnSession.rollback();
    assertEquals(0, queue.getMessageCount());
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) 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 82 with AmqpMessage

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

the class AmqpTransactionTest method testReceiveMessageWithRollback.

@Test(timeout = 60000)
public void testReceiveMessageWithRollback() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    final Queue queue = getProxyToQueue(getQueueName());
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    sender.send(message);
    Wait.assertEquals(1, queue::getMessageCount);
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    session.begin();
    receiver.flow(1);
    AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
    assertNotNull(received);
    received.accept();
    session.rollback();
    assertEquals(1, queue.getMessageCount());
    sender.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 83 with AmqpMessage

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

the class AmqpTransactionTest method testMultipleSessionReceiversInSingleTXNWithRollback.

@Test(timeout = 60000)
public void testMultipleSessionReceiversInSingleTXNWithRollback() 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.rollback();
    assertEquals(3, 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 84 with AmqpMessage

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

the class AmqpTransactionTest method testCommitAndRollbackWithMultipleSessionsInSingleTX.

@Test(timeout = 60000)
public void testCommitAndRollbackWithMultipleSessionsInSingleTX() throws Exception {
    final int NUM_MESSAGES = 10;
    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());
    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();
    {
        message1.accept(txnSession);
        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);
        AmqpMessage message = new AmqpMessage();
        message.setText("Test-Message");
        message.setApplicationProperty("msgId", NUM_MESSAGES + 1);
        sender.send(message, txnSession.getTransactionId());
    }
    txnSession.rollback();
    // Variation here from .NET code, the client settles the accepted message where
    // the .NET client does not and instead releases here to have it redelivered.
    receiver.flow(NUM_MESSAGES);
    for (int i = 1; i <= NUM_MESSAGES; ++i) {
        AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
        assertNotNull(message);
        assertEquals(i, message.getApplicationProperty("msgId"));
        message.accept();
    }
    // Should be nothing left.
    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)

Example 85 with AmqpMessage

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

the class AmqpTransactionTest method testReceiversCommitAndRollbackWithMultipleSessionsInSingleTXNoSettlement.

@Test(timeout = 60000)
public void testReceiversCommitAndRollbackWithMultipleSessionsInSingleTXNoSettlement() throws Exception {
    final int NUM_MESSAGES = 10;
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = 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);
            System.out.println("Read message: " + message.getApplicationProperty("msgId"));
            assertNotNull(message);
            messages.add(message);
        }
        // Commit half the consumed messages [0, 1, 2, 3, 4]
        txnSession.begin();
        for (int i = 0; i < NUM_MESSAGES / 2; ++i) {
            System.out.println("Commit: Accepting message: " + messages.get(i).getApplicationProperty("msgId"));
            messages.get(i).accept(txnSession, false);
        }
        txnSession.commit();
        // Rollback the other half the consumed messages [5, 6, 7, 8, 9]
        txnSession.begin();
        for (int i = NUM_MESSAGES / 2; i < NUM_MESSAGES; ++i) {
            System.out.println("Rollback: Accepting message: " + messages.get(i).getApplicationProperty("msgId"));
            messages.get(i).accept(txnSession, false);
        }
        txnSession.rollback();
        // After rollback messages should still be acquired so we read last sent message [10]
        {
            AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
            System.out.println("Read message: " + message.getApplicationProperty("msgId"));
            assertNotNull(message);
            assertEquals(NUM_MESSAGES, message.getApplicationProperty("msgId"));
            message.release();
        }
        // Commit the other half the consumed messages [5, 6, 7, 8, 9] which should still be acquired
        txnSession.begin();
        for (int i = NUM_MESSAGES / 2; i < NUM_MESSAGES; ++i) {
            messages.get(i).accept(txnSession);
        }
        txnSession.commit();
        // The final message [10] should still be pending as we released it previously and committed
        // the previously accepted but not settled messages [5, 6, 7, 8, 9] in a new TX
        {
            receiver.flow(1);
            AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
            System.out.println("Read message: " + message.getApplicationProperty("msgId"));
            assertNotNull(message);
            assertEquals(NUM_MESSAGES, message.getApplicationProperty("msgId"));
            message.accept();
        }
        // We should have now drained the Queue
        receiver.flow(1);
        AmqpMessage message = receiver.receive(5, TimeUnit.SECONDS);
        if (message != null) {
            System.out.println("Read message: " + message.getApplicationProperty("msgId"));
        }
        assertNull(message);
    } 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)

Aggregations

AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)118 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)115 AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)114 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)114 Test (org.junit.Test)100 AmqpReceiver (org.apache.activemq.transport.amqp.client.AmqpReceiver)94 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)82 Queue (org.apache.activemq.artemis.core.server.Queue)56 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)16 Source (org.apache.qpid.proton.amqp.messaging.Source)9 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 Connection (javax.jms.Connection)4 Message (javax.jms.Message)4 MessageConsumer (javax.jms.MessageConsumer)4 Session (javax.jms.Session)4 AmqpValidator (org.apache.activemq.transport.amqp.client.AmqpValidator)4 Delivery (org.apache.qpid.proton.engine.Delivery)4