Search in sources :

Example 21 with AmqpMessage

use of org.apache.activemq.transport.amqp.client.AmqpMessage 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 22 with AmqpMessage

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

the class AmqpTransactionTest method testSendMessageToQueueWithCommit.

@Test(timeout = 60000)
public void testSendMessageToQueueWithCommit() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getQueueName());
    final Queue queue = getProxyToQueue(getQueueName());
    session.begin();
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    sender.send(message);
    assertEquals(0, queue.getMessageCount());
    session.commit();
    Wait.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) 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 23 with AmqpMessage

use of org.apache.activemq.transport.amqp.client.AmqpMessage 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)

Example 24 with AmqpMessage

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

the class AmqpTransactionTest method testUnsettledTXMessageGetTransactedDispostion.

@Test(timeout = 30000)
public void testUnsettledTXMessageGetTransactedDispostion() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    assertNotNull(session);
    AmqpSender sender = session.createSender(getQueueName());
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    sender.send(message);
    AmqpReceiver receiver = session.createReceiver(getQueueName());
    receiver.setStateInspector(new AmqpValidator() {

        @Override
        public void inspectDeliveryUpdate(Sender sender, Delivery delivery) {
            if (delivery.remotelySettled()) {
                LOG.info("Receiver got delivery update for: {}", delivery);
                if (!(delivery.getRemoteState() instanceof TransactionalState)) {
                    markAsInvalid("Transactionally acquire work no tagged as being in a transaction.");
                } else {
                    TransactionalState txState = (TransactionalState) delivery.getRemoteState();
                    if (!(txState.getOutcome() instanceof Accepted)) {
                        markAsInvalid("Transaction state lacks any outcome");
                    } else if (txState.getTxnId() == null) {
                        markAsInvalid("Transaction state lacks any TX Id");
                    }
                }
                if (!(delivery.getLocalState() instanceof TransactionalState)) {
                    markAsInvalid("Transactionally acquire work no tagged as being in a transaction.");
                } else {
                    TransactionalState txState = (TransactionalState) delivery.getLocalState();
                    if (!(txState.getOutcome() instanceof Accepted)) {
                        markAsInvalid("Transaction state lacks any outcome");
                    } else if (txState.getTxnId() == null) {
                        markAsInvalid("Transaction state lacks any TX Id");
                    }
                }
                TransactionalState localTxState = (TransactionalState) delivery.getLocalState();
                TransactionalState remoteTxState = (TransactionalState) delivery.getRemoteState();
                if (!localTxState.getTxnId().equals(remoteTxState)) {
                    markAsInvalid("Message not enrolled in expected transaction");
                }
            }
        }
    });
    session.begin();
    assertTrue(session.isInTransaction());
    receiver.flow(1);
    AmqpMessage received = receiver.receive(2, TimeUnit.SECONDS);
    assertNotNull(received);
    received.accept(false);
    session.commit();
    sender.getStateInspector().assertValid();
    connection.close();
}
Also used : AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) 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) Delivery(org.apache.qpid.proton.engine.Delivery) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState) Test(org.junit.Test)

Example 25 with AmqpMessage

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

the class AmqpTransactionTest method testReceiveMessageWithCommit.

@Test(timeout = 60000)
public void testReceiveMessageWithCommit() 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.commit();
    assertEquals(0, 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)

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