use of org.apache.activemq.transport.amqp.client.AmqpMessage in project activemq-artemis by apache.
the class AmqpSendReceiveTest method doTestSendReceiveLotsOfDurableMessages.
private void doTestSendReceiveLotsOfDurableMessages(Class<?> destType) throws Exception {
final int MSG_COUNT = 1000;
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
final CountDownLatch done = new CountDownLatch(MSG_COUNT);
final AtomicBoolean error = new AtomicBoolean(false);
final ExecutorService executor = Executors.newSingleThreadExecutor();
final String address;
if (Queue.class.equals(destType)) {
address = getQueueName();
} else {
address = getTopicName();
}
final AmqpReceiver receiver = session.createReceiver(address);
receiver.flow(MSG_COUNT);
AmqpSender sender = session.createSender(address);
Queue queueView = getProxyToQueue(address);
executor.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < MSG_COUNT; i++) {
try {
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
received.accept();
done.countDown();
} catch (Exception ex) {
LOG.info("Caught error: {}", ex.getClass().getSimpleName());
error.set(true);
}
}
}
});
for (int i = 0; i < MSG_COUNT; i++) {
AmqpMessage message = new AmqpMessage();
message.setMessageId("msg" + i);
sender.send(message);
}
assertTrue("did not read all messages, waiting on: " + done.getCount(), done.await(10, TimeUnit.SECONDS));
assertFalse("should not be any errors on receive", error.get());
Wait.assertEquals(0, queueView::getDeliveringCount);
sender.close();
receiver.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpMessage in project activemq-artemis by apache.
the class AmqpSendReceiveTest method doTestMessageWithToFieldSet.
private void doTestMessageWithToFieldSet(boolean anonymous, String expected) throws Exception {
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
final String address = getQueueName();
AmqpSender sender = session.createSender(anonymous ? null : address);
AmqpMessage message = new AmqpMessage();
message.setAddress(expected);
message.setMessageId("msg:1");
sender.send(message);
AmqpReceiver receiver = session.createReceiver(address);
Queue queueView = getProxyToQueue(address);
assertEquals(1, queueView.getMessageCount());
receiver.flow(1);
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
assertEquals(expected, received.getAddress());
receiver.close();
assertEquals(1, queueView.getMessageCount());
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpMessage 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();
}
}
use of org.apache.activemq.transport.amqp.client.AmqpMessage 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());
}
use of org.apache.activemq.transport.amqp.client.AmqpMessage in project activemq-artemis by apache.
the class AmqpTransactionTest method testMultipleSessionSendersInSingleTXNWithCommit.
@Test(timeout = 60000)
public void testMultipleSessionSendersInSingleTXNWithCommit() 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.commit();
assertEquals(3, queue.getMessageCount());
}
Aggregations