use of org.apache.activemq.transport.amqp.client.AmqpSender 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();
}
use of org.apache.activemq.transport.amqp.client.AmqpSender 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();
}
}
use of org.apache.activemq.transport.amqp.client.AmqpSender in project activemq-artemis by apache.
the class AmqpPresettledReceiverTest method testPresettledReceiverWithinBoundsOfActiveTXWithSendAndRollback.
@Test(timeout = 60000)
public void testPresettledReceiverWithinBoundsOfActiveTXWithSendAndRollback() 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(), null, false, true);
session.begin();
receiver.flow(1);
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
assertTrue(received.getWrappedDelivery().remotelySettled());
message = new AmqpMessage();
message.setText("Test-Message - Rolled Back");
sender.send(message);
session.rollback();
assertEquals(0, queue.getMessageCount());
sender.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpSender in project activemq-artemis by apache.
the class AmqpPresettledReceiverTest method doTestPresettledReceiverWithinBoundsOfActiveTX.
private void doTestPresettledReceiverWithinBoundsOfActiveTX(boolean commit) 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(), null, false, true);
session.begin();
receiver.flow(1);
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
assertTrue(received.getWrappedDelivery().remotelySettled());
if (commit) {
session.commit();
} else {
session.rollback();
}
assertEquals(0, queue.getMessageCount());
sender.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpSender in project activemq-artemis by apache.
the class AmqpScheduledMessageTest method testSendRecvWithDeliveryTime.
@Test(timeout = 60000)
public void testSendRecvWithDeliveryTime() throws Exception {
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
try {
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getQueueName());
// Get the Queue View early to avoid racing the delivery.
final Queue queueView = getProxyToQueue(getQueueName());
assertNotNull(queueView);
AmqpMessage message = new AmqpMessage();
long deliveryTime = System.currentTimeMillis() + 6000;
message.setMessageAnnotation("x-opt-delivery-time", deliveryTime);
message.setText("Test-Message");
sender.send(message);
sender.close();
assertEquals(1, queueView.getScheduledCount());
AmqpReceiver receiver = session.createReceiver(getQueueName());
receiver.flow(1);
// Now try and get the message, should not due to being scheduled.
AmqpMessage received = receiver.receive(2, TimeUnit.SECONDS);
assertNull(received);
// Now try and get the message, should get it now
received = receiver.receive(10, TimeUnit.SECONDS);
assertNotNull(received);
received.accept();
} finally {
connection.close();
}
}
Aggregations