use of org.apache.activemq.transport.amqp.client.AmqpReceiver in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testDispatchOrderWithPrefetchOfOne.
@Test(timeout = 60000)
public void testDispatchOrderWithPrefetchOfOne() 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();
AmqpReceiver receiver1 = session.createReceiver(getQueueName());
receiver1.flow(1);
AmqpReceiver receiver2 = session.createReceiver(getQueueName());
receiver2.flow(1);
AmqpMessage message1 = receiver1.receive(10, TimeUnit.SECONDS);
AmqpMessage message2 = receiver2.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();
receiver1.flow(1);
AmqpMessage message3 = receiver1.receive(10, TimeUnit.SECONDS);
receiver2.flow(1);
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 both receivers");
int splitCredit = (MSG_COUNT - 4) / 2;
LOG.info("**** Receiver #1 granting credit[{}] for its block of messages", splitCredit);
receiver1.flow(splitCredit);
for (int i = 0; i < splitCredit; i++) {
AmqpMessage message = receiver1.receive(10, TimeUnit.SECONDS);
assertNotNull("Receiver #1 should have read a message", message);
LOG.info("Receiver #1 read message: {}", message.getMessageId());
message.accept();
}
LOG.info("**** Receiver #2 granting credit[{}] for its block of messages", splitCredit);
receiver2.flow(splitCredit);
for (int i = 0; i < splitCredit; i++) {
AmqpMessage message = receiver2.receive(10, TimeUnit.SECONDS);
assertNotNull("Receiver #2 should have read message[" + i + "]", message);
LOG.info("Receiver #2 read message: {}", message.getMessageId());
message.accept();
}
receiver1.close();
receiver2.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpReceiver in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testTwoPresettledReceiversReceiveAllMessages.
@Test(timeout = 60000)
public void testTwoPresettledReceiversReceiveAllMessages() throws Exception {
final int MSG_COUNT = 100;
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
final String address = getQueueName();
AmqpSender sender = session.createSender(address);
AmqpReceiver receiver1 = session.createReceiver(address, null, false, true);
AmqpReceiver receiver2 = session.createReceiver(address, null, false, true);
for (int i = 0; i < MSG_COUNT; i++) {
AmqpMessage message = new AmqpMessage();
message.setMessageId("msg" + i);
sender.send(message);
}
LOG.info("Attempting to read first two messages with receiver #1");
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");
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 both receivers");
int splitCredit = (MSG_COUNT - 4) / 2;
LOG.info("**** Receiver #1 granting credit[{}] for its block of messages", splitCredit);
receiver1.flow(splitCredit);
for (int i = 0; i < splitCredit; i++) {
AmqpMessage message = receiver1.receive(10, TimeUnit.SECONDS);
assertNotNull("Receiver #1 should have read a message", message);
LOG.info("Receiver #1 read message: {}", message.getMessageId());
message.accept();
}
LOG.info("**** Receiver #2 granting credit[{}] for its block of messages", splitCredit);
receiver2.flow(splitCredit);
for (int i = 0; i < splitCredit; i++) {
AmqpMessage message = receiver2.receive(10, TimeUnit.SECONDS);
assertNotNull("Receiver #2 should have read a message[" + i + "]", message);
LOG.info("Receiver #2 read message: {}", message.getMessageId());
message.accept();
}
receiver1.close();
receiver2.close();
connection.close();
}
use of org.apache.activemq.transport.amqp.client.AmqpReceiver in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testAcceptWithoutSettling.
@Test(timeout = 60000)
public void testAcceptWithoutSettling() throws Exception {
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
AmqpSession session = connection.createSession();
AmqpReceiver receiver = session.createReceiver(getQueueName());
sendMessages(getQueueName(), 10);
for (int i = 0; i < 10; i++) {
receiver.flow(1);
AmqpMessage receive = receiver.receive();
receive.accept(false);
receive.settle();
}
receiver.close();
connection.close();
Queue queue = getProxyToQueue(getQueueName());
assertNotNull(queue);
assertEquals(0, queue.getMessageCount());
}
use of org.apache.activemq.transport.amqp.client.AmqpReceiver in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testSendingAndReceivingToQueueWithDifferentAddressAndQueueName.
@Test(timeout = 60000)
public void testSendingAndReceivingToQueueWithDifferentAddressAndQueueName() throws Exception {
String queueName = "TestQueueName";
String address = "TestAddress";
server.addAddressInfo(new AddressInfo(SimpleString.toSimpleString(address), RoutingType.ANYCAST));
server.createQueue(new SimpleString(address), RoutingType.ANYCAST, new SimpleString(queueName), null, true, false);
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
try {
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(address);
AmqpReceiver receiver = session.createReceiver(address);
receiver.flow(1);
AmqpMessage message = new AmqpMessage();
message.setText("TestPayload");
sender.send(message);
AmqpMessage receivedMessage = receiver.receive(5000, TimeUnit.MILLISECONDS);
assertNotNull(receivedMessage);
} finally {
connection.close();
}
}
use of org.apache.activemq.transport.amqp.client.AmqpReceiver in project activemq-artemis by apache.
the class AmqpSendReceiveTest method testMessageDurableTrue.
@Test(timeout = 60000)
public void testMessageDurableTrue() throws Exception {
sendMessages(getQueueName(), 1, true);
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);
AmqpMessage receive = receiver.receive(5, TimeUnit.SECONDS);
assertNotNull(receive);
assertTrue(receive.isDurable());
receiver.close();
assertEquals(1, queueView.getMessageCount());
connection.close();
}
Aggregations