// Public --------------------------------------------------------
public void testExpiryMessagesThenDLQ() throws Exception
{
HornetQServer server = createServer(true);
server.getConfiguration().setMessageExpiryScanPeriod(600000);
AddressSettings setting = new AddressSettings();
setting.setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
setting.setMaxDeliveryAttempts(5);
setting.setMaxSizeBytes(50 * 1024);
setting.setPageSizeBytes(10 * 1024);
setting.setExpiryAddress(EXPIRY);
setting.setDeadLetterAddress(DLQ);
server.getAddressSettingsRepository().addMatch(MY_QUEUE.toString(), setting);
setting.setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
setting.setMaxDeliveryAttempts(5);
setting.setMaxSizeBytes(50 * 1024);
setting.setPageSizeBytes(10 * 1024);
setting.setDeadLetterAddress(DLQ);
server.getAddressSettingsRepository().addMatch(EXPIRY.toString(), setting);
server.start();
try
{
server.createQueue(EXPIRY, EXPIRY, null, true, false);
server.createQueue(DLQ, DLQ, null, true, false);
server.createQueue(MY_QUEUE, MY_QUEUE, null, true, false);
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory sf = locator.createSessionFactory();
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);
}
server.stop();
server.start();
sf = locator.createSessionFactory();
session = sf.createSession(true, true, 0);
Thread.sleep(1500);
// just to try expiring
ClientConsumer cons = session.createConsumer(MY_QUEUE);
assertNull(cons.receive(1000));
session.close();
session = sf.createSession(false, false);
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();
System.out.println("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((long)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 = locator.createSessionFactory();
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 = locator.createSessionFactory();
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((long)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();
}
finally
{
server.stop();
}
}