use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method testReceiveImmediateWithZeroWindow4.
@Test
public void testReceiveImmediateWithZeroWindow4() throws Exception {
ActiveMQServer server = createServer(false, isNetty());
server.start();
locator.setConsumerWindowSize(0);
ClientSessionFactory sf = createSessionFactory(locator);
ClientSession session = sf.createSession(false, false, false);
session.createQueue("testWindow", "testWindow", true);
session.close();
int numConsumers = 5;
ArrayList<ClientConsumer> consumers = new ArrayList<>();
ArrayList<ClientSession> sessions = new ArrayList<>();
for (int i = 0; i < numConsumers; i++) {
ClientSession session1 = sf.createSession();
ClientConsumer consumer = session1.createConsumer("testWindow");
consumers.add(consumer);
session1.start();
sessions.add(session1);
Assert.assertNull(consumer.receive(10));
}
ClientSession senderSession = sf.createSession(false, false);
ClientProducer producer = senderSession.createProducer("testWindow");
ClientMessage sent = senderSession.createMessage(true);
sent.putStringProperty("hello", "world");
producer.send(sent);
senderSession.commit();
senderSession.start();
ClientConsumer consumer = consumers.get(2);
ClientMessage received = consumer.receive(5000);
Assert.assertNotNull(received);
for (ClientSession tmpSess : sessions) {
tmpSess.close();
}
senderSession.close();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method internalTestSlowConsumerOnMessageHandlerNoBuffers.
public void internalTestSlowConsumerOnMessageHandlerNoBuffers(final boolean largeMessages) throws Exception {
ActiveMQServer server = createServer(false, isNetty());
ClientSession sessionB = null;
ClientSession session = null;
try {
final int numberOfMessages = 100;
server.start();
locator.setConsumerWindowSize(0);
if (largeMessages) {
locator.setMinLargeMessageSize(100);
}
ClientSessionFactory sf = createSessionFactory(locator);
session = sf.createSession(false, true, true);
SimpleString ADDRESS = new SimpleString("some-queue");
session.createQueue(ADDRESS, ADDRESS, true);
sessionB = sf.createSession(false, true, true);
sessionB.start();
session.start();
ClientConsumerInternal consReceiveOneAndHold = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);
final CountDownLatch latchReceived = new CountDownLatch(2);
final CountDownLatch latchDone = new CountDownLatch(1);
// It can't close the session while the large message is being read
final CountDownLatch latchRead = new CountDownLatch(1);
// It should receive two messages and then give up
class LocalHandler implements MessageHandler {
boolean failed = false;
int count = 0;
/* (non-Javadoc)
* @see MessageHandler#onMessage(ClientMessage)
*/
@Override
public synchronized void onMessage(final ClientMessage message) {
try {
String str = getTextMessage(message);
failed = failed || !str.equals("Msg" + count);
message.acknowledge();
latchReceived.countDown();
if (count++ == 1) {
// it will hold here for a while
if (!latchDone.await(TIMEOUT, TimeUnit.SECONDS)) {
// a timed wait, so if the test fails, one less thread around
new Exception("ClientConsuemrWindowSizeTest Handler couldn't receive signal in less than 5 seconds").printStackTrace();
failed = true;
}
if (largeMessages) {
message.getBodyBuffer().readBytes(new byte[600]);
}
latchRead.countDown();
}
} catch (Exception e) {
// Hudson / JUnit report
e.printStackTrace();
failed = true;
}
}
}
LocalHandler handler = new LocalHandler();
ClientConsumer cons1 = session.createConsumer(ADDRESS);
ClientProducer prod = session.createProducer(ADDRESS);
for (int i = 0; i < numberOfMessages; i++) {
ClientMessage msg = createTextMessage(session, "Msg" + i);
if (largeMessages) {
msg.getBodyBuffer().writeBytes(new byte[600]);
}
prod.send(msg);
}
consReceiveOneAndHold.setMessageHandler(handler);
Assert.assertTrue(latchReceived.await(TIMEOUT, TimeUnit.SECONDS));
Assert.assertEquals(0, consReceiveOneAndHold.getBufferSize());
for (int i = 2; i < numberOfMessages; i++) {
ClientMessage msg = cons1.receive(1000);
Assert.assertNotNull("expected message at i = " + i, msg);
Assert.assertEquals("Msg" + i, getTextMessage(msg));
msg.acknowledge();
}
Assert.assertEquals(0, consReceiveOneAndHold.getBufferSize());
latchDone.countDown();
// The test can' t close the session while the message is still being read, or it could interrupt the data
Assert.assertTrue(latchRead.await(10, TimeUnit.SECONDS));
session.close();
session = null;
sessionB.close();
sessionB = null;
Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
Assert.assertFalse("MessageHandler received a failure", handler.failed);
} finally {
try {
if (session != null) {
session.close();
}
if (sessionB != null) {
sessionB.close();
}
} catch (Exception ignored) {
}
}
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ConsumerWindowSizeTest method testSingleImmediate.
@Test
public void testSingleImmediate() throws Exception {
final int NUMBER_OF_MESSAGES = 200;
ActiveMQServer server = createServer(false, isNetty());
server.start();
locator.setConsumerWindowSize(0);
final ClientSessionFactory sf = createSessionFactory(locator);
{
ClientSession session = sf.createSession(false, false, false);
session.createQueue("testWindow", "testWindow", true);
session.close();
}
final AtomicInteger received = new AtomicInteger(0);
ClientSession senderSession = sf.createSession(false, false);
ClientProducer producer = senderSession.createProducer("testWindow");
ClientMessage sent = senderSession.createMessage(true);
sent.putStringProperty("hello", "world");
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
producer.send(sent);
}
senderSession.commit();
ClientSession session = sf.createSession(false, false);
ClientConsumer consumer = session.createConsumer("testWindow");
session.start();
while (true) {
ClientMessage msg = consumer.receiveImmediate();
if (msg == null) {
System.out.println("Returning null");
break;
}
msg.acknowledge();
session.commit();
received.incrementAndGet();
}
Assert.assertEquals(NUMBER_OF_MESSAGES, received.get());
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class DeadLetterAddressTest method testBasicSend.
@Test
public void testBasicSend() throws Exception {
SimpleString dla = new SimpleString("DLA");
SimpleString qName = new SimpleString("q1");
SimpleString adName = new SimpleString("ad1");
AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla);
server.getAddressSettingsRepository().addMatch(adName.toString(), addressSettings);
SimpleString dlq = new SimpleString("DLQ1");
clientSession.createQueue(dla, dlq, null, false);
clientSession.createQueue(adName, qName, null, false);
ClientProducer producer = clientSession.createProducer(adName);
producer.send(createTextMessage(clientSession, "heyho!"));
clientSession.start();
ClientConsumer clientConsumer = clientSession.createConsumer(qName);
ClientMessage m = clientConsumer.receive(500);
m.acknowledge();
Assert.assertNotNull(m);
Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
// force a cancel
clientSession.rollback();
m = clientConsumer.receiveImmediate();
Assert.assertNull(m);
clientConsumer.close();
clientConsumer = clientSession.createConsumer(dlq);
m = clientConsumer.receive(500);
Assert.assertNotNull(m);
assertEquals("q1", m.getStringProperty(Message.HDR_ORIGINAL_QUEUE));
assertEquals("ad1", m.getStringProperty(Message.HDR_ORIGINAL_ADDRESS));
Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class DeadLetterAddressTest method testDeadLetterAddressWithOverridenSublevelAddressSettings.
@Test
public void testDeadLetterAddressWithOverridenSublevelAddressSettings() throws Exception {
int defaultDeliveryAttempt = 3;
int specificeDeliveryAttempt = defaultDeliveryAttempt + 1;
SimpleString address = new SimpleString("prefix.address");
SimpleString queue = RandomUtil.randomSimpleString();
SimpleString defaultDeadLetterAddress = RandomUtil.randomSimpleString();
SimpleString defaultDeadLetterQueue = RandomUtil.randomSimpleString();
SimpleString specificDeadLetterAddress = RandomUtil.randomSimpleString();
SimpleString specificDeadLetterQueue = RandomUtil.randomSimpleString();
AddressSettings defaultAddressSettings = new AddressSettings().setMaxDeliveryAttempts(defaultDeliveryAttempt).setDeadLetterAddress(defaultDeadLetterAddress);
server.getAddressSettingsRepository().addMatch("*", defaultAddressSettings);
AddressSettings specificAddressSettings = new AddressSettings().setMaxDeliveryAttempts(specificeDeliveryAttempt).setDeadLetterAddress(specificDeadLetterAddress);
server.getAddressSettingsRepository().addMatch(address.toString(), specificAddressSettings);
clientSession.createQueue(address, queue, false);
clientSession.createQueue(defaultDeadLetterAddress, defaultDeadLetterQueue, false);
clientSession.createQueue(specificDeadLetterAddress, specificDeadLetterQueue, false);
ClientProducer producer = clientSession.createProducer(address);
ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
producer.send(clientMessage);
clientSession.start();
ClientConsumer clientConsumer = clientSession.createConsumer(queue);
ClientConsumer defaultDeadLetterConsumer = clientSession.createConsumer(defaultDeadLetterQueue);
ClientConsumer specificDeadLetterConsumer = clientSession.createConsumer(specificDeadLetterQueue);
for (int i = 0; i < defaultDeliveryAttempt; i++) {
ClientMessage m = clientConsumer.receive(500);
Assert.assertNotNull(m);
Assert.assertEquals(i + 1, m.getDeliveryCount());
m.acknowledge();
clientSession.rollback();
}
Assert.assertNull(defaultDeadLetterConsumer.receiveImmediate());
Assert.assertNull(specificDeadLetterConsumer.receiveImmediate());
// one more redelivery attempt:
ClientMessage m = clientConsumer.receive(500);
Assert.assertNotNull(m);
Assert.assertEquals(specificeDeliveryAttempt, m.getDeliveryCount());
m.acknowledge();
clientSession.rollback();
Assert.assertNull(defaultDeadLetterConsumer.receiveImmediate());
Assert.assertNotNull(specificDeadLetterConsumer.receive(500));
}
Aggregations