Configuration config = createDefaultConfig();
config.setJournalSyncNonTransactional(false);
HornetQServer server = createServer(true,
config,
PagingTest.PAGE_SIZE,
PagingTest.PAGE_MAX,
new HashMap<String, AddressSettings>());
if (divert)
{
DivertConfiguration divert1 = new DivertConfiguration("dv1",
"nm1",
PagingTest.ADDRESS.toString(),
PagingTest.ADDRESS.toString() + "-1",
true,
null,
null);
DivertConfiguration divert2 = new DivertConfiguration("dv2",
"nm2",
PagingTest.ADDRESS.toString(),
PagingTest.ADDRESS.toString() + "-2",
true,
null,
null);
ArrayList<DivertConfiguration> divertList = new ArrayList<DivertConfiguration>();
divertList.add(divert1);
divertList.add(divert2);
config.setDivertConfigurations(divertList);
}
server.start();
final int messageSize = 1024;
final int numberOfMessages = 3000;
final byte[] body = new byte[messageSize];
ByteBuffer bb = ByteBuffer.wrap(body);
for (int j = 1; j <= messageSize; j++)
{
bb.put(getSamplebyte(j));
}
final AtomicBoolean running = new AtomicBoolean(true);
class TCount extends Thread
{
Queue queue;
TCount(Queue queue)
{
this.queue = queue;
}
public void run()
{
try
{
while (running.get())
{
// log.info("Message count = " + queue.getMessageCount() + " on queue " + queue.getName());
queue.getMessagesAdded();
queue.getMessageCount();
// log.info("Message added = " + queue.getMessagesAdded() + " on queue " + queue.getName());
Thread.sleep(10);
}
}
catch (InterruptedException e)
{
log.info("Thread interrupted");
}
}
};
TCount tcount1 = null;
TCount tcount2 = null;
try
{
{
ServerLocator locator = createInVMNonHALocator();
locator.setBlockOnNonDurableSend(true);
locator.setBlockOnDurableSend(true);
locator.setBlockOnAcknowledge(true);
ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession(false, false, false);
if (divert)
{
session.createQueue(PagingTest.ADDRESS + "-1", PagingTest.ADDRESS + "-1", null, true);
session.createQueue(PagingTest.ADDRESS + "-2", PagingTest.ADDRESS + "-2", null, true);
}
else
{
session.createQueue(PagingTest.ADDRESS.toString(), PagingTest.ADDRESS + "-1", null, true);
session.createQueue(PagingTest.ADDRESS.toString(), PagingTest.ADDRESS + "-2", null, true);
}
ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
ClientMessage message = null;
for (int i = 0; i < numberOfMessages; i++)
{
if (i % 500 == 0)
{
log.info("Sent " + i + " messages");
session.commit();
}
message = session.createMessage(true);
HornetQBuffer bodyLocal = message.getBodyBuffer();
bodyLocal.writeBytes(body);
message.putIntProperty(new SimpleString("id"), i);
producer.send(message);
}
session.commit();
session.close();
server.stop();
sf.close();
locator.close();
}
server = createServer(true,
config,
PagingTest.PAGE_SIZE,
PagingTest.PAGE_MAX,
new HashMap<String, AddressSettings>());
server.start();
Queue queue1 = server.locateQueue(PagingTest.ADDRESS.concat("-1"));
Queue queue2 = server.locateQueue(PagingTest.ADDRESS.concat("-2"));
assertNotNull(queue1);
assertNotNull(queue2);
assertNotSame(queue1, queue2);
tcount1 = new TCount(queue1);
tcount2 = new TCount(queue2);
tcount1.start();
tcount2.start();
ServerLocator locator = createInVMNonHALocator();
final ClientSessionFactory sf2 = locator.createSessionFactory();
final AtomicInteger errors = new AtomicInteger(0);
Thread threads[] = new Thread[2];
for (int start = 1; start <= 2; start++)
{
final String addressToSubscribe = PagingTest.ADDRESS + "-" + start;
threads[start - 1] = new Thread()
{
public void run()
{
try
{
ClientSession session = sf2.createSession(null, null, false, true, true, false, 0);
ClientConsumer consumer = session.createConsumer(addressToSubscribe);
session.start();
for (int i = 0; i < numberOfMessages; i++)
{
ClientMessage message2 = consumer.receive(PagingTest.RECEIVE_TIMEOUT);
Assert.assertNotNull(message2);
Assert.assertEquals(i, message2.getIntProperty("id").intValue());
message2.acknowledge();
Assert.assertNotNull(message2);
if (i % 100 == 0)
{
if (i % 5000 == 0)
{
log.info(addressToSubscribe + " consumed " + i + " messages");
}
session.commit();
}
try
{
assertBodiesEqual(body, message2.getBodyBuffer());
}
catch (AssertionFailedError e)
{
PagingTest.log.info("Expected buffer:" + UnitTestCase.dumbBytesHex(body, 40));
PagingTest.log.info("Arriving buffer:" + UnitTestCase.dumbBytesHex(message2.getBodyBuffer()
.toByteBuffer()
.array(), 40));
throw e;
}
}
session.commit();
consumer.close();
session.close();
}
catch (Throwable e)
{
e.printStackTrace();
errors.incrementAndGet();
}
}
};
}
for (int i = 0; i < 2; i++)
{
threads[i].start();
}
for (int i = 0; i < 2; i++)
{
threads[i].join();
}
sf2.close();
locator.close();
assertEquals(0, errors.get());
for (int i = 0; i < 20 && server.getPostOffice().getPagingManager().getTransactions().size() != 0; i++)
{
if (server.getPostOffice().getPagingManager().getTransactions().size() != 0)
{
// The delete may be asynchronous, giving some time case it eventually happen asynchronously
Thread.sleep(500);
}
}
assertEquals(0, server.getPostOffice().getPagingManager().getTransactions().size());
}
finally
{
running.set(false);
if (tcount1 != null)
{
tcount1.interrupt();
tcount1.join();
}
if (tcount2 != null)
{
tcount2.interrupt();
tcount2.join();
}
try
{
server.stop();
}
catch (Throwable ignored)
{
}
}