use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ExpiryAddressTest method testExpireWithDefaultAddressSettings.
@Test
public void testExpireWithDefaultAddressSettings() throws Exception {
SimpleString ea = new SimpleString("EA");
SimpleString qName = new SimpleString("q1");
SimpleString eq = new SimpleString("EA1");
AddressSettings addressSettings = new AddressSettings().setExpiryAddress(ea);
server.getAddressSettingsRepository().setDefault(addressSettings);
clientSession.createQueue(ea, eq, null, false);
clientSession.createQueue(qName, qName, null, false);
ClientProducer producer = clientSession.createProducer(qName);
ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
clientMessage.setExpiration(System.currentTimeMillis());
producer.send(clientMessage);
clientSession.start();
ClientConsumer clientConsumer = clientSession.createConsumer(qName);
ClientMessage m = clientConsumer.receiveImmediate();
Assert.assertNull(m);
clientConsumer.close();
clientConsumer = clientSession.createConsumer(eq);
m = clientConsumer.receive(500);
Assert.assertNotNull(m);
Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
m.acknowledge();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ExpiryAddressTest method testExpireWithWildcardAddressSettings.
@Test
public void testExpireWithWildcardAddressSettings() throws Exception {
SimpleString ea = new SimpleString("EA");
SimpleString qName = new SimpleString("q1");
SimpleString eq = new SimpleString("EA1");
AddressSettings addressSettings = new AddressSettings().setExpiryAddress(ea);
server.getAddressSettingsRepository().addMatch("*", addressSettings);
clientSession.createQueue(ea, eq, null, false);
clientSession.createQueue(qName, qName, null, false);
ClientProducer producer = clientSession.createProducer(qName);
ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
clientMessage.setExpiration(System.currentTimeMillis());
producer.send(clientMessage);
clientSession.start();
ClientConsumer clientConsumer = clientSession.createConsumer(qName);
ClientMessage m = clientConsumer.receiveImmediate();
Assert.assertNull(m);
clientConsumer.close();
clientConsumer = clientSession.createConsumer(eq);
m = clientConsumer.receive(500);
Assert.assertNotNull(m);
Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
m.acknowledge();
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ConsumerCloseTest method testCloseWithScheduledRedeliveryWithTX.
@Test
public void testCloseWithScheduledRedeliveryWithTX() throws Exception {
AddressSettings settings = new AddressSettings().setRedeliveryDelay(1000);
server.getAddressSettingsRepository().addMatch("#", settings);
ClientProducer producer = session.createProducer(address);
final int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(false);
message.putIntProperty("count", i);
producer.send(message);
}
session.close();
session = addClientSession(sf.createSession(false, false));
ClientConsumer consumer = session.createConsumer(queue);
session.start();
ClientMessage msg = consumer.receive(500);
msg.acknowledge();
long timeout = System.currentTimeMillis() + 1000;
while (((ClientConsumerImpl) consumer).getBufferSize() < 2 && System.currentTimeMillis() > timeout) {
Thread.sleep(10);
}
consumer.close();
session.rollback();
consumer = session.createConsumer(queue);
// We received one, so we must receive the others now
for (int i = 0; i < numMessages - 1; i++) {
msg = consumer.receive(1000);
assertNotNull("Expected message at i=" + i, msg);
msg.acknowledge();
}
assertNull(consumer.receiveImmediate());
// The first message received after redeliveryDelay
msg = consumer.receive(5000);
assertNotNull(msg);
assertEquals(0, msg.getIntProperty("count").intValue());
msg.acknowledge();
session.commit();
assertNull(consumer.receiveImmediate());
// Close shouldn't wait for all messages to be processed before closing
long start = System.currentTimeMillis();
consumer.close();
long end = System.currentTimeMillis();
Assert.assertTrue(end - start <= 1500);
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ConsumerCloseTest method testCloseWithManyMessagesInBufferAndSlowConsumer.
// https://jira.jboss.org/jira/browse/JBMESSAGING-1526
@Test
public void testCloseWithManyMessagesInBufferAndSlowConsumer() throws Exception {
ClientConsumer consumer = session.createConsumer(queue);
ClientProducer producer = session.createProducer(address);
final int numMessages = 100;
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(false);
producer.send(message);
}
final CountDownLatch received = new CountDownLatch(1);
final CountDownLatch waitingToProceed = new CountDownLatch(1);
class MyHandler implements MessageHandler {
@Override
public void onMessage(final ClientMessage message) {
try {
received.countDown();
waitingToProceed.await();
} catch (Exception e) {
}
}
}
consumer.setMessageHandler(new MyHandler());
session.start();
assertTrue(received.await(5, TimeUnit.SECONDS));
long timeout = System.currentTimeMillis() + 1000;
// Instead of waiting a long time (like 1 second) we just make sure the buffer is full on the client
while (((ClientConsumerImpl) consumer).getBufferSize() < 2 && System.currentTimeMillis() > timeout) {
Thread.sleep(10);
}
waitingToProceed.countDown();
// Close shouldn't wait for all messages to be processed before closing
long start = System.currentTimeMillis();
consumer.close();
long end = System.currentTimeMillis();
Assert.assertTrue(end - start <= 1500);
}
use of org.apache.activemq.artemis.api.core.client.ClientConsumer in project activemq-artemis by apache.
the class ConsumerRoundRobinTest method testConsumersRoundRobinCorrectly.
@Test
public void testConsumersRoundRobinCorrectly() throws Exception {
ActiveMQServer server = createServer(false);
server.start();
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession session = addClientSession(cf.createSession(false, true, true));
session.createQueue(addressA, queueA, false);
ClientConsumer[] consumers = new ClientConsumer[5];
// start the session before we create the consumers, this is because start is non blocking and we have to
// guarantee
// all consumers have been started before sending messages
session.start();
consumers[0] = session.createConsumer(queueA);
consumers[1] = session.createConsumer(queueA);
consumers[2] = session.createConsumer(queueA);
consumers[3] = session.createConsumer(queueA);
consumers[4] = session.createConsumer(queueA);
ClientProducer cp = session.createProducer(addressA);
int numMessage = 10;
for (int i = 0; i < numMessage; i++) {
ClientMessage cm = session.createMessage(false);
cm.getBodyBuffer().writeInt(i);
cp.send(cm);
}
int currMessage = 0;
for (int i = 0; i < numMessage / 5; i++) {
log.info("i is " + i);
for (int j = 0; j < 5; j++) {
log.info("j is " + j);
ClientMessage cm = consumers[j].receive(5000);
Assert.assertNotNull(cm);
Assert.assertEquals(currMessage++, cm.getBodyBuffer().readInt());
cm.acknowledge();
}
}
}
Aggregations