org.apache.activemq.artemis.core.paging.cursor.PagedReference

Here are the examples of the java api class org.apache.activemq.artemis.core.paging.cursor.PagedReference taken from open source projects.

1. PageCursorStressTest#testMultipleIterators()

View license
@Test
public void testMultipleIterators() throws Exception {
    final int NUM_MESSAGES = 10;
    int numberOfPages = addMessages(NUM_MESSAGES, 1024 * 1024);
    System.out.println("NumberOfPages = " + numberOfPages);
    PageCursorProvider cursorProvider = lookupCursorProvider();
    PageSubscription cursor = cursorProvider.getSubscription(queue.getID());
    LinkedListIterator<PagedReference> iter = cursor.iterator();
    LinkedListIterator<PagedReference> iter2 = cursor.iterator();
    assertTrue(iter.hasNext());
    PagedReference msg1 = iter.next();
    PagedReference msg2 = iter2.next();
    assertEquals(tstProperty(msg1.getMessage()), tstProperty(msg2.getMessage()));
    System.out.println("property = " + tstProperty(msg1.getMessage()));
    msg1 = iter.next();
    assertEquals(1, tstProperty(msg1.getMessage()));
    iter.remove();
    msg2 = iter2.next();
    assertEquals(2, tstProperty(msg2.getMessage()));
    iter2.repeat();
    msg2 = iter2.next();
    assertEquals(2, tstProperty(msg2.getMessage()));
    iter2.repeat();
    assertEquals(2, tstProperty(msg2.getMessage()));
    msg1 = iter.next();
    assertEquals(2, tstProperty(msg1.getMessage()));
    iter.repeat();
    msg1 = iter.next();
    assertEquals(2, tstProperty(msg1.getMessage()));
    assertTrue(iter2.hasNext());
}

2. PageCursorStressTest#testSimpleCursor()

View license
@Test
public void testSimpleCursor() throws Exception {
    final int NUM_MESSAGES = 100;
    PageSubscription cursor = lookupPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    Iterator<PagedReference> iterEmpty = cursor.iterator();
    int numberOfPages = addMessages(NUM_MESSAGES, 1024 * 1024);
    System.out.println("NumberOfPages = " + numberOfPages);
    PagedReference msg;
    LinkedListIterator<PagedReference> iterator = cursor.iterator();
    int key = 0;
    while ((msg = iterator.next()) != null) {
        assertEquals(key++, msg.getMessage().getIntProperty("key").intValue());
        cursor.confirmPosition(msg.getPosition());
    }
    assertEquals(NUM_MESSAGES, key);
    server.getStorageManager().waitOnOperations();
    waitCleanup();
    assertFalse(lookupPageStore(ADDRESS).isPaging());
    assertEquals(1, lookupPageStore(ADDRESS).getNumberOfPages());
    forceGC();
    server.stop();
    createServer();
    waitCleanup();
    assertEquals(1, lookupPageStore(ADDRESS).getNumberOfPages());
}

3. PageCursorStressTest#testSimpleCursorWithFilter()

View license
@Test
public void testSimpleCursorWithFilter() throws Exception {
    final int NUM_MESSAGES = 100;
    PageSubscription cursorEven = createNonPersistentCursor(new Filter() {

        @Override
        public boolean match(ServerMessage message) {
            Boolean property = message.getBooleanProperty("even");
            if (property == null) {
                return false;
            } else {
                return property.booleanValue();
            }
        }

        @Override
        public SimpleString getFilterString() {
            return new SimpleString("even=true");
        }
    });
    PageSubscription cursorOdd = createNonPersistentCursor(new Filter() {

        @Override
        public boolean match(ServerMessage message) {
            Boolean property = message.getBooleanProperty("even");
            if (property == null) {
                return false;
            } else {
                return !property.booleanValue();
            }
        }

        @Override
        public SimpleString getFilterString() {
            return new SimpleString("even=true");
        }
    });
    int numberOfPages = addMessages(NUM_MESSAGES, 1024 * 1024);
    System.out.println("NumberOfPages = " + numberOfPages);
    queue.getPageSubscription().destroy();
    PagedReference msg;
    LinkedListIterator<PagedReference> iteratorEven = cursorEven.iterator();
    LinkedListIterator<PagedReference> iteratorOdd = cursorOdd.iterator();
    int key = 0;
    while ((msg = iteratorEven.next()) != null) {
        System.out.println("Received" + msg);
        assertEquals(key, msg.getMessage().getIntProperty("key").intValue());
        assertTrue(msg.getMessage().getBooleanProperty("even").booleanValue());
        key += 2;
        cursorEven.confirmPosition(msg.getPosition());
    }
    assertEquals(NUM_MESSAGES, key);
    key = 1;
    while ((msg = iteratorOdd.next()) != null) {
        assertEquals(key, msg.getMessage().getIntProperty("key").intValue());
        assertFalse(msg.getMessage().getBooleanProperty("even").booleanValue());
        key += 2;
        cursorOdd.confirmPosition(msg.getPosition());
    }
    assertEquals(NUM_MESSAGES + 1, key);
    forceGC();
    // assertTrue(lookupCursorProvider().getCacheSize() < numberOfPages);
    server.stop();
    createServer();
    waitCleanup();
    assertEquals(1, lookupPageStore(ADDRESS).getNumberOfPages());
}

4. PageCursorStressTest#testConsumeLivePage()

View license
@Test
public void testConsumeLivePage() throws Exception {
    PagingStoreImpl pageStore = lookupPageStore(ADDRESS);
    pageStore.startPaging();
    final int NUM_MESSAGES = 100;
    final int messageSize = 1024 * 1024;
    PageCursorProvider cursorProvider = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider();
    System.out.println("cursorProvider = " + cursorProvider);
    PageSubscription cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    System.out.println("Cursor: " + cursor);
    RoutingContextImpl ctx = generateCTX();
    LinkedListIterator<PagedReference> iterator = cursor.iterator();
    for (int i = 0; i < NUM_MESSAGES; i++) {
        // if (i % 100 == 0)
        System.out.println("read/written " + i);
        ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
        ServerMessage msg = new ServerMessageImpl(i, buffer.writerIndex());
        msg.putIntProperty("key", i);
        msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
        Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
        PagedReference readMessage = iterator.next();
        assertNotNull(readMessage);
        assertEquals(i, readMessage.getMessage().getIntProperty("key").intValue());
        assertNull(iterator.next());
    }
    OperationContextImpl.clearContext();
    ctx = generateCTX();
    pageStore = lookupPageStore(ADDRESS);
    cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    iterator = cursor.iterator();
    for (int i = 0; i < NUM_MESSAGES * 2; i++) {
        if (i % 100 == 0)
            System.out.println("Paged " + i);
        if (i >= NUM_MESSAGES) {
            ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
            ServerMessage msg = new ServerMessageImpl(i, buffer.writerIndex());
            msg.putIntProperty("key", i);
            msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
            Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
        }
        PagedReference readMessage = iterator.next();
        assertNotNull(readMessage);
        assertEquals(i, readMessage.getMessage().getIntProperty("key").intValue());
    }
    OperationContextImpl.clearContext();
    pageStore = lookupPageStore(ADDRESS);
    cursor = this.server.getPagingManager().getPageStore(ADDRESS).getCursorProvider().getSubscription(queue.getID());
    iterator = cursor.iterator();
    for (int i = 0; i < NUM_MESSAGES * 3; i++) {
        if (i % 100 == 0)
            System.out.println("Paged " + i);
        if (i >= NUM_MESSAGES * 2 - 1) {
            ActiveMQBuffer buffer = RandomUtil.randomBuffer(messageSize, i + 1L);
            ServerMessage msg = new ServerMessageImpl(i, buffer.writerIndex());
            msg.putIntProperty("key", i + 1);
            msg.getBodyBuffer().writeBytes(buffer, 0, buffer.writerIndex());
            Assert.assertTrue(pageStore.page(msg, ctx.getTransaction(), ctx.getContextListing(ADDRESS), lock));
        }
        PagedReference readMessage = iterator.next();
        assertNotNull(readMessage);
        cursor.ack(readMessage);
        assertEquals(i, readMessage.getMessage().getIntProperty("key").intValue());
    }
    PagedReference readMessage = iterator.next();
    assertEquals(NUM_MESSAGES * 3, readMessage.getMessage().getIntProperty("key").intValue());
    cursor.ack(readMessage);
    server.getStorageManager().waitOnOperations();
    pageStore.flushExecutors();
    assertFalse(pageStore.isPaging());
    server.stop();
    createServer();
    assertFalse(pageStore.isPaging());
    waitCleanup();
    assertFalse(lookupPageStore(ADDRESS).isPaging());
}