use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class ExpiryLargeMessageTest method testExpiryMessagesThenDLQ.
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testExpiryMessagesThenDLQ() throws Exception {
ActiveMQServer server = createServer(true);
server.getConfiguration().setMessageExpiryScanPeriod(600000);
AddressSettings setting = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE).setMaxDeliveryAttempts(5).setMaxSizeBytes(50 * 1024).setPageSizeBytes(10 * 1024).setExpiryAddress(EXPIRY).setDeadLetterAddress(DLQ);
server.getAddressSettingsRepository().addMatch(MY_QUEUE.toString(), setting);
server.getAddressSettingsRepository().addMatch(EXPIRY.toString(), setting);
server.start();
server.createQueue(EXPIRY, RoutingType.ANYCAST, EXPIRY, null, true, false);
server.createQueue(DLQ, RoutingType.ANYCAST, DLQ, null, true, false);
server.createQueue(MY_QUEUE, RoutingType.ANYCAST, MY_QUEUE, null, true, false);
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
ClientSession session = sf.createSession(true, true, 0);
byte[] bufferSample = new byte[messageSize];
for (int i = 0; i < bufferSample.length; i++) {
bufferSample[i] = getSamplebyte(i);
}
ClientProducer producer = session.createProducer(MY_QUEUE);
long timeToExpiry = System.currentTimeMillis() + 1000;
for (int i = 0; i < numberOfMessages; i++) {
ClientMessage message = session.createMessage(true);
message.putIntProperty("count", i);
// Send a few regular messages first, then all is just large messages
if (i % 2 == 0) {
message.putBooleanProperty("tst-large", false);
message.getBodyBuffer().writeBytes(bufferSample);
} else {
message.putBooleanProperty("tst-large", true);
message.setBodyInputStream(createFakeLargeStream(messageSize));
}
message.setExpiration(timeToExpiry);
producer.send(message);
}
session.close();
server.stop();
server.start();
Queue queueExpiry = server.locateQueue(EXPIRY);
Queue myQueue = server.locateQueue(MY_QUEUE);
sf = createSessionFactory(locator);
Thread.sleep(1500);
long timeout = System.currentTimeMillis() + 5000;
while (timeout > System.currentTimeMillis() && getMessageCount(queueExpiry) != numberOfMessages) {
// What the Expiry Scan would be doing
myQueue.expireReferences();
Thread.sleep(50);
}
assertEquals(50, getMessageCount(queueExpiry));
session = sf.createSession(false, false);
ClientConsumer cons = session.createConsumer(EXPIRY);
session.start();
// Consume half of the messages to make sure all the messages are paging (on the second try)
for (int i = 0; i < numberOfMessages / 2; i++) {
ClientMessage msg = cons.receive(5000);
assertNotNull(msg);
msg.acknowledge();
}
session.commit();
cons.close();
for (int rep = 0; rep < 6; rep++) {
cons = session.createConsumer(EXPIRY);
session.start();
log.info("Trying " + rep);
for (int i = 0; i < numberOfMessages / 2; i++) {
ClientMessage message = cons.receive(5000);
assertNotNull(message);
if (i % 10 == 0) {
System.out.println("Received " + i);
}
for (int location = 0; location < messageSize; location++) {
assertEquals(getSamplebyte(location), message.getBodyBuffer().readByte());
}
message.acknowledge();
}
session.rollback();
cons.close();
session.close();
sf.close();
if (rep == 0) {
// restart the server at the first try
server.stop();
server.start();
}
sf = createSessionFactory(locator);
session = sf.createSession(false, false);
session.start();
}
cons = session.createConsumer(EXPIRY);
session.start();
assertNull(cons.receiveImmediate());
cons.close();
session.close();
sf.close();
for (int rep = 0; rep < 2; rep++) {
sf = createSessionFactory(locator);
session = sf.createSession(false, false);
cons = session.createConsumer(DLQ);
session.start();
for (int i = 0; i < numberOfMessages / 2; i++) {
ClientMessage message = cons.receive(5000);
assertNotNull(message);
if (i % 10 == 0) {
System.out.println("Received " + i);
}
for (int location = 0; location < messageSize; location++) {
assertEquals(getSamplebyte(location), message.getBodyBuffer().readByte());
}
message.acknowledge();
}
if (rep == 0) {
session.rollback();
session.close();
sf.close();
server.stop();
server.start();
}
}
session.commit();
assertNull(cons.receiveImmediate());
session.close();
sf.close();
locator.close();
validateNoFilesOnLargeDir();
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class FullQualifiedQueueTest method testMixedQueues.
@Test
public void testMixedQueues() throws Exception {
server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ1, null, true, false, -1, false, true);
server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ2, null, true, false, -1, false, true);
server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ3, null, true, false, -1, false, true);
server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ2, null, true, false, -1, false, true);
server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ3, null, true, false, -1, false, true);
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession session = cf.createSession();
session.start();
// send 3 messages
ClientProducer producer = session.createProducer(mixedAddress);
final int num = 3;
sendMessages(session, producer, num);
ClientConsumer consumer1 = session.createConsumer(toFullQN(mixedAddress, anycastQ1));
ClientConsumer consumer2 = session.createConsumer(toFullQN(mixedAddress, anycastQ2));
ClientConsumer consumer3 = session.createConsumer(toFullQN(mixedAddress, anycastQ3));
ClientConsumer consumer4 = session.createConsumer(toFullQN(mixedAddress, multicastQ1));
ClientConsumer consumer5 = session.createConsumer(toFullQN(mixedAddress, multicastQ2));
ClientConsumer consumer6 = session.createConsumer(toFullQN(mixedAddress, multicastQ3));
session.start();
// each anycast consumer receives one, each multicast receives three.
ClientMessage m = consumer1.receive(2000);
assertNotNull(m);
System.out.println("consumer1 : " + m);
m.acknowledge();
m = consumer2.receive(2000);
assertNotNull(m);
System.out.println("consumer2 : " + m);
m.acknowledge();
m = consumer3.receive(2000);
assertNotNull(m);
System.out.println("consumer3 : " + m);
m.acknowledge();
for (int i = 0; i < num; i++) {
m = consumer4.receive(2000);
assertNotNull(m);
System.out.println("consumer4 : " + m);
m.acknowledge();
m = consumer5.receive(2000);
assertNotNull(m);
System.out.println("consumer5 : " + m);
m.acknowledge();
m = consumer6.receive(2000);
assertNotNull(m);
System.out.println("consumer6 : " + m);
m.acknowledge();
}
session.commit();
// queues are empty now
for (SimpleString q : new SimpleString[] { anycastQ1, anycastQ2, anycastQ3, multicastQ1, multicastQ2, multicastQ3 }) {
QueueQueryResult query = server.queueQuery(toFullQN(mixedAddress, q));
assertTrue(query.isExists());
assertEquals(mixedAddress, query.getAddress());
assertEquals(toFullQN(mixedAddress, q), query.getName());
assertEquals(0, query.getMessageCount());
}
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class FullQualifiedQueueTest method testAnycastQueues.
@Test
public void testAnycastQueues() throws Exception {
server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ2, null, true, false, -1, false, true);
server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ3, null, true, false, -1, false, true);
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession session = cf.createSession();
session.start();
// send 3 messages
ClientProducer producer = session.createProducer(anycastAddress);
sendMessages(session, producer, 3);
ClientConsumer consumer1 = session.createConsumer(toFullQN(anycastAddress, anycastQ1));
ClientConsumer consumer2 = session.createConsumer(toFullQN(anycastAddress, anycastQ2));
ClientConsumer consumer3 = session.createConsumer(toFullQN(anycastAddress, anycastQ3));
session.start();
// each consumer receives one
ClientMessage m = consumer1.receive(2000);
assertNotNull(m);
m.acknowledge();
m = consumer2.receive(2000);
assertNotNull(m);
m.acknowledge();
m = consumer3.receive(2000);
assertNotNull(m);
m.acknowledge();
session.commit();
// queues are empty now
for (SimpleString q : new SimpleString[] { anycastQ1, anycastQ2, anycastQ3 }) {
QueueQueryResult query = server.queueQuery(toFullQN(anycastAddress, q));
assertTrue(query.isExists());
assertEquals(anycastAddress, query.getAddress());
assertEquals(toFullQN(anycastAddress, q), query.getName());
assertEquals(0, query.getMessageCount());
}
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class FullQualifiedQueueTest method testSpecialCase.
@Test
public void testSpecialCase() throws Exception {
server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
ClientSessionFactory cf = createSessionFactory(locator);
ClientSession session = cf.createSession();
session.start();
ClientProducer producer = session.createProducer(anycastAddress);
sendMessages(session, producer, 1);
// ::queue
ClientConsumer consumer1 = session.createConsumer(toFullQN(new SimpleString(""), anycastQ1));
session.start();
ClientMessage m = consumer1.receive(2000);
assertNotNull(m);
m.acknowledge();
session.commit();
consumer1.close();
try {
// queue::
session.createConsumer(toFullQN(anycastQ1, new SimpleString("")));
fail("should get exception");
} catch (ActiveMQNonExistentQueueException e) {
// expected.
}
try {
// ::
session.createConsumer(toFullQN(new SimpleString(""), new SimpleString("")));
fail("should get exception");
} catch (ActiveMQNonExistentQueueException e) {
// expected.
}
}
use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.
the class HangConsumerTest method testHangOnDelivery.
@Test
public void testHangOnDelivery() throws Exception {
queue = server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
try {
ClientSessionFactory factory = locator.createSessionFactory();
ClientSession sessionProducer = factory.createSession(false, false, false);
ServerLocator consumerLocator = createInVMNonHALocator();
ClientSessionFactory factoryConsumer = consumerLocator.createSessionFactory();
ClientSession sessionConsumer = factoryConsumer.createSession();
ClientProducer producer = sessionProducer.createProducer(QUEUE);
ClientConsumer consumer = sessionConsumer.createConsumer(QUEUE);
producer.send(sessionProducer.createMessage(true));
blockConsumers();
sessionProducer.commit();
sessionConsumer.start();
awaitBlocking();
// this shouldn't lock
producer.send(sessionProducer.createMessage(true));
sessionProducer.commit();
// These three operations should finish without the test hanging
queue.getMessagesAdded();
queue.getMessageCount();
releaseConsumers();
// a rollback to make sure everything will be reset on the deliveries
// and that both consumers will receive each a message
// this is to guarantee the server will have both consumers regsitered
sessionConsumer.rollback();
// a flush to guarantee any pending task is finished on flushing out delivery and pending msgs
queue.flushExecutor();
Wait.waitFor(() -> getMessageCount(queue) == 2);
Wait.assertEquals(2, queue::getMessageCount);
Wait.assertEquals(2, queue::getMessagesAdded);
ClientMessage msg = consumer.receive(5000);
Assert.assertNotNull(msg);
msg.acknowledge();
msg = consumer.receive(5000);
Assert.assertNotNull(msg);
msg.acknowledge();
sessionProducer.commit();
sessionConsumer.commit();
sessionProducer.close();
sessionConsumer.close();
} finally {
releaseConsumers();
}
}
Aggregations