// Step 8. We don't need persistent messages in order to use paging. (This step is optional)
pageMessageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Step 9. Create a Binary Bytes Message with 10K arbitrary bytes
BytesMessage message = session.createBytesMessage();
message.writeBytes(new byte[10 * 1024]);
// Step 10. Send only 20 messages to the Queue. This will be already enough for pagingQueue. Look at
// ./paging/config/hornetq-queues.xml for the config.
for (int i = 0; i < 20; i++)
{
pageMessageProducer.send(message);
}
// Step 11. Create a JMS Message Producer
MessageProducer messageProducer = session.createProducer(queue);
// Step 12. We don't need persistent messages in order to use paging. (This step is optional)
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Step 13. Send the message for about 1K, which should be over the memory limit imposed by the server
for (int i = 0; i < 1000; i++)
{
messageProducer.send(message);
}
// Step 14. if you pause this example here, you will see several files under ./build/data/paging
// Thread.sleep(30000); // if you want to just our of curiosity, you can sleep here and inspect the created
// files just for
// Step 15. Create a JMS Message Consumer
MessageConsumer messageConsumer = session.createConsumer(queue);
// Step 16. Start the JMS Connection. This step will activate the subscribers to receive messages.
connection.start();
// Step 17. Receive the messages. It's important to ACK for messages as HornetQ will not read messages from
// paging
// until messages are ACKed
for (int i = 0; i < 1000; i++)
{
message = (BytesMessage)messageConsumer.receive(3000);
if (i % 100 == 0)
{
System.out.println("Received " + i + " messages");
message.acknowledge();
}
}
message.acknowledge();
// Step 18. Receive the messages from the Queue names pageQueue. Create the proper consumer for that
messageConsumer.close();
messageConsumer = session.createConsumer(pageQueue);
for (int i = 0; i < 20; i++)
{
message = (BytesMessage)messageConsumer.receive(1000);
System.out.println("Received message " + i + " from pageQueue");
message.acknowledge();
}
return true;
}