use of org.apache.activemq.transport.amqp.client.AmqpConnection in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testReceiveWithJMSSelectorFilter.
@Test(timeout = 60000)
public void testReceiveWithJMSSelectorFilter() throws Exception {
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpMessage message1 = new AmqpMessage();
message1.setGroupId("abcdefg");
message1.setApplicationProperty("sn", 100);
AmqpMessage message2 = new AmqpMessage();
message2.setGroupId("hijklm");
message2.setApplicationProperty("sn", 200);
AmqpSender sender = session.createSender(getQueueName());
sender.send(message1);
sender.send(message2);
sender.close();
AmqpReceiver receiver = session.createReceiver(getQueueName(), "sn = 100");
receiver.flow(2);
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull("Should have read a message", received);
assertEquals(100, received.getApplicationProperty("sn"));
assertEquals("abcdefg", received.getGroupId());
received.accept();
assertNull(receiver.receive(1, TimeUnit.SECONDS));
receiver.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpConnection in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testReceiveMessageAndRefillCreditBeforeAcceptOnQueueAsync.
@Test(timeout = 60000)
public void testReceiveMessageAndRefillCreditBeforeAcceptOnQueueAsync() throws Exception {
final AmqpClient client = createAmqpClient();
final LinkedList<Throwable> errors = new LinkedList<>();
final CountDownLatch receiverReady = new CountDownLatch(1);
ExecutorService executorService = Executors.newCachedThreadPool();
final String address = getQueueName();
executorService.submit(new Runnable() {
@Override
public void run() {
try {
LOG.info("Starting consumer connection");
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpReceiver receiver = session.createReceiver(address);
receiver.flow(1);
receiverReady.countDown();
AmqpMessage received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
receiver.flow(1);
received.accept();
received = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(received);
received.accept();
receiver.close();
connection.close();
} catch (Exception error) {
errors.add(error);
}
}
});
// producer
executorService.submit(new Runnable() {
@Override
public void run() {
try {
receiverReady.await(20, TimeUnit.SECONDS);
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(address);
for (int i = 0; i < 2; i++) {
AmqpMessage message = new AmqpMessage();
message.setMessageId("msg" + i);
sender.send(message);
}
sender.close();
connection.close();
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
});
executorService.shutdown();
executorService.awaitTermination(20, TimeUnit.SECONDS);
assertTrue("no errors: " + errors, errors.isEmpty());
}
use of org.apache.activemq.transport.amqp.client.AmqpConnection in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testQueueReceiverReadMessage.
@Test(timeout = 60000)
public void testQueueReceiverReadMessage() throws Exception {
sendMessages(getQueueName(), 1);
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpReceiver receiver = session.createReceiver(getQueueName());
Queue queueView = getProxyToQueue(getQueueName());
assertEquals(1, queueView.getMessageCount());
receiver.flow(1);
assertNotNull(receiver.receive(5, TimeUnit.SECONDS));
receiver.close();
assertEquals(1, queueView.getMessageCount());
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpConnection in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testAdvancedLinkFlowControl.
@Test(timeout = 60000)
public void testAdvancedLinkFlowControl() throws Exception {
final int MSG_COUNT = 20;
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getQueueName());
for (int i = 0; i < MSG_COUNT; i++) {
AmqpMessage message = new AmqpMessage();
message.setMessageId("msg" + i);
message.setMessageAnnotation("serialNo", i);
message.setText("Test-Message");
sender.send(message);
}
sender.close();
LOG.info("Attempting to read first two messages with receiver #1");
AmqpReceiver receiver1 = session.createReceiver(getQueueName());
receiver1.flow(2);
AmqpMessage message1 = receiver1.receive(10, TimeUnit.SECONDS);
AmqpMessage message2 = receiver1.receive(10, TimeUnit.SECONDS);
assertNotNull("Should have read message 1", message1);
assertNotNull("Should have read message 2", message2);
assertEquals("msg0", message1.getMessageId());
assertEquals("msg1", message2.getMessageId());
message1.accept();
message2.accept();
LOG.info("Attempting to read next two messages with receiver #2");
AmqpReceiver receiver2 = session.createReceiver(getQueueName());
receiver2.flow(2);
AmqpMessage message3 = receiver2.receive(10, TimeUnit.SECONDS);
AmqpMessage message4 = receiver2.receive(10, TimeUnit.SECONDS);
assertNotNull("Should have read message 3", message3);
assertNotNull("Should have read message 4", message4);
assertEquals("msg2", message3.getMessageId());
assertEquals("msg3", message4.getMessageId());
message3.accept();
message4.accept();
LOG.info("Attempting to read remaining messages with receiver #1");
receiver1.flow(MSG_COUNT - 4);
for (int i = 4; i < MSG_COUNT; i++) {
AmqpMessage message = receiver1.receive(10, TimeUnit.SECONDS);
assertNotNull("Should have read a message", message);
assertEquals("msg" + i, message.getMessageId());
message.accept();
}
receiver1.close();
receiver2.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpConnection in project activemq-artemis by apache.
the class AmqpSendReceiveTest method doTestBrokerRestartAndDurability.
private void doTestBrokerRestartAndDurability(boolean durable, boolean enforceHeader) throws Exception {
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getQueueName());
final Queue queueView1 = getProxyToQueue(getQueueName());
// Create default message that should be sent as non-durable
AmqpMessage message = new AmqpMessage();
message.setText("Test-Message -> non-durable");
message.setMessageId("ID:Message:1");
if (durable) {
message.setDurable(true);
} else {
if (enforceHeader) {
message.setDurable(false);
assertNotNull(message.getWrappedMessage().getHeader());
} else {
assertNull(message.getWrappedMessage().getHeader());
}
}
sender.send(message);
connection.close();
Wait.assertEquals(1, queueView1::getMessageCount);
// Restart the server and the Queue should be empty
server.stop();
server.start();
// Reconnect now
connection = addConnection(client.connect());
session = connection.createSession();
AmqpReceiver receiver = session.createReceiver(getQueueName());
final Queue queueView2 = getProxyToQueue(getQueueName());
if (durable) {
Wait.assertTrue("Message should not have returned", () -> queueView2.getMessageCount() == 1);
} else {
Wait.assertTrue("Message should have been restored", () -> queueView2.getMessageCount() == 0);
}
receiver.flow(1);
message = receiver.receive(1, TimeUnit.SECONDS);
if (durable) {
assertNotNull("Should have read a message", message);
} else {
assertNull("Should not have read a message", message);
}
connection.close();
}
Aggregations