}
else
{
numberOfPages--;
final Page returnPage;
// We are out of old pages, all that is left now is the current page.
// On that case we need to replace it by a new empty page, and return the current page immediately
if (currentPageId == firstPageId)
{
firstPageId = Integer.MAX_VALUE;
if (currentPage == null)
{
// sanity check... it shouldn't happen!
throw new IllegalStateException("CurrentPage is null");
}
returnPage = currentPage;
returnPage.close();
currentPage = null;
// The current page is empty... which means we reached the end of the pages
if (returnPage.getNumberOfMessages() == 0)
{
stopPaging();
returnPage.open();
returnPage.delete();
// This will trigger this address to exit the page mode,
// and this will make HornetQ start using the journal again
return null;
}