Search in sources :

Example 96 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingStoreImplTest method testAddAndRemoveMessages.

@Test
public void testAddAndRemoveMessages() {
    long id1 = RandomUtil.randomLong();
    long id2 = RandomUtil.randomLong();
    PageTransactionInfo trans = new PageTransactionInfoImpl(id2);
    trans.setRecordID(id1);
    // anything between 2 and 100
    int nr1 = RandomUtil.randomPositiveInt() % 98 + 2;
    for (int i = 0; i < nr1; i++) {
        trans.increment(1, 0);
    }
    Assert.assertEquals(nr1, trans.getNumberOfMessages());
    ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(trans.getEncodeSize());
    trans.encode(buffer);
    PageTransactionInfo trans2 = new PageTransactionInfoImpl(id1);
    trans2.decode(buffer);
    Assert.assertEquals(id2, trans2.getTransactionID());
    Assert.assertEquals(nr1, trans2.getNumberOfMessages());
}
Also used : PageTransactionInfo(org.apache.activemq.artemis.core.paging.PageTransactionInfo) PageTransactionInfoImpl(org.apache.activemq.artemis.core.paging.impl.PageTransactionInfoImpl) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 97 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingStoreImplTest method testDepageMultiplePages.

@Test
public void testDepageMultiplePages() throws Exception {
    SequentialFileFactory factory = new FakeSequentialFileFactory();
    SimpleString destination = new SimpleString("test");
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    PagingStoreImpl store = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true);
    store.start();
    Assert.assertEquals(0, store.getNumberOfPages());
    store.startPaging();
    Assert.assertEquals(1, store.getNumberOfPages());
    List<ActiveMQBuffer> buffers = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        ActiveMQBuffer buffer = createRandomBuffer(i + 1L, 10);
        buffers.add(buffer);
        if (i == 5) {
            store.forceAnotherPage();
        }
        Message msg = createMessage(i, store, destination, buffer);
        final RoutingContextImpl ctx = new RoutingContextImpl(null);
        Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
    }
    Assert.assertEquals(2, store.getNumberOfPages());
    store.sync();
    int sequence = 0;
    for (int pageNr = 0; pageNr < 2; pageNr++) {
        Page page = store.depage();
        System.out.println("numberOfPages = " + store.getNumberOfPages());
        page.open();
        List<PagedMessage> msg = page.read(new NullStorageManager());
        page.close();
        Assert.assertEquals(5, msg.size());
        for (int i = 0; i < 5; i++) {
            Assert.assertEquals(sequence++, msg.get(i).getMessage().getMessageID());
            ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(pageNr * 5 + i), msg.get(i).getMessage().toCore().getBodyBuffer());
        }
    }
    Assert.assertEquals(1, store.getNumberOfPages());
    Assert.assertTrue(store.isPaging());
    Message msg = createMessage(1, store, destination, buffers.get(0));
    final RoutingContextImpl ctx = new RoutingContextImpl(null);
    Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
    Page newPage = store.depage();
    newPage.open();
    Assert.assertEquals(1, newPage.read(new NullStorageManager()).size());
    newPage.delete(null);
    Assert.assertEquals(1, store.getNumberOfPages());
    Assert.assertTrue(store.isPaging());
    Assert.assertNull(store.depage());
    Assert.assertFalse(store.isPaging());
    {
        final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
        Assert.assertFalse(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
    }
    store.startPaging();
    {
        final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
        Assert.assertTrue(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
    }
    Page page = store.depage();
    page.open();
    List<PagedMessage> msgs = page.read(new NullStorageManager());
    Assert.assertEquals(1, msgs.size());
    Assert.assertEquals(1L, msgs.get(0).getMessage().getMessageID());
    ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(0), msgs.get(0).getMessage().toCore().getBodyBuffer());
    Assert.assertEquals(1, store.getNumberOfPages());
    Assert.assertTrue(store.isPaging());
    Assert.assertNull(store.depage());
    Assert.assertEquals(0, store.getNumberOfPages());
    page.open();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Page(org.apache.activemq.artemis.core.paging.impl.Page) FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) SequentialFileFactory(org.apache.activemq.artemis.core.io.SequentialFileFactory) RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) NullStorageManager(org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager) PagingStoreFactory(org.apache.activemq.artemis.core.paging.PagingStoreFactory) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 98 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class PagingStoreImplTest method testStore.

@Test
public void testStore() throws Exception {
    SequentialFileFactory factory = new FakeSequentialFileFactory();
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    AddressSettings addressSettings = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
    PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, addressSettings, getExecutorFactory().getExecutor(), true);
    storeImpl.start();
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    storeImpl.startPaging();
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    List<ActiveMQBuffer> buffers = new ArrayList<>();
    ActiveMQBuffer buffer = createRandomBuffer(0, 10);
    buffers.add(buffer);
    SimpleString destination = new SimpleString("test");
    Message msg = createMessage(1, storeImpl, destination, buffer);
    Assert.assertTrue(storeImpl.isPaging());
    final RoutingContextImpl ctx = new RoutingContextImpl(null);
    Assert.assertTrue(storeImpl.page(msg, ctx.getTransaction(), ctx.getContextListing(storeImpl.getStoreName()), lock));
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    storeImpl.sync();
    storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, addressSettings, getExecutorFactory().getExecutor(), true);
    storeImpl.start();
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) SequentialFileFactory(org.apache.activemq.artemis.core.io.SequentialFileFactory) RoutingContextImpl(org.apache.activemq.artemis.core.server.impl.RoutingContextImpl) FakeSequentialFileFactory(org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.FakeSequentialFileFactory) PagingStoreImpl(org.apache.activemq.artemis.core.paging.impl.PagingStoreImpl) PagingStoreFactory(org.apache.activemq.artemis.core.paging.PagingStoreFactory) PagingStore(org.apache.activemq.artemis.core.paging.PagingStore) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 99 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class NettyAcceptorTest method testStartStop.

@Test
public void testStartStop() throws Exception {
    BufferHandler handler = new BufferHandler() {

        @Override
        public void bufferReceived(final Object connectionID, final ActiveMQBuffer buffer) {
        }
    };
    Map<String, Object> params = new HashMap<>();
    ServerConnectionLifeCycleListener listener = new ServerConnectionLifeCycleListener() {

        @Override
        public void connectionException(final Object connectionID, final ActiveMQException me) {
        }

        @Override
        public void connectionDestroyed(final Object connectionID) {
        }

        @Override
        public void connectionCreated(final ActiveMQComponent component, final Connection connection, final ProtocolManager protocol) {
        }

        @Override
        public void connectionReadyForWrites(Object connectionID, boolean ready) {
        }
    };
    pool2 = Executors.newScheduledThreadPool(ActiveMQDefaultConfiguration.getDefaultScheduledThreadPoolMaxSize(), ActiveMQThreadFactory.defaultThreadFactory());
    NettyAcceptor acceptor = new NettyAcceptor("netty", null, params, handler, listener, pool2, new HashMap<String, ProtocolManager>());
    addActiveMQComponent(acceptor);
    acceptor.start();
    Assert.assertTrue(acceptor.isStarted());
    acceptor.stop();
    Assert.assertFalse(acceptor.isStarted());
    ActiveMQTestBase.checkFreePort(TransportConstants.DEFAULT_PORT);
    acceptor.start();
    Assert.assertTrue(acceptor.isStarted());
    acceptor.stop();
    Assert.assertFalse(acceptor.isStarted());
    ActiveMQTestBase.checkFreePort(TransportConstants.DEFAULT_PORT);
    pool2.shutdown();
    pool2.awaitTermination(1, TimeUnit.SECONDS);
}
Also used : ActiveMQComponent(org.apache.activemq.artemis.core.server.ActiveMQComponent) HashMap(java.util.HashMap) NettyAcceptor(org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor) Connection(org.apache.activemq.artemis.spi.core.remoting.Connection) ServerConnectionLifeCycleListener(org.apache.activemq.artemis.spi.core.remoting.ServerConnectionLifeCycleListener) BufferHandler(org.apache.activemq.artemis.spi.core.remoting.BufferHandler) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ProtocolManager(org.apache.activemq.artemis.spi.core.protocol.ProtocolManager) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 100 with ActiveMQBuffer

use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.

the class SequentialFileFactoryTestBase method testWriteandRead.

@Test
public void testWriteandRead() throws Exception {
    SequentialFile sf = factory.createSequentialFile("write.amq");
    sf.open();
    String s1 = "aardvark";
    byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
    ActiveMQBuffer bb1 = wrapBuffer(bytes1);
    String s2 = "hippopotamus";
    byte[] bytes2 = s2.getBytes(StandardCharsets.UTF_8);
    ActiveMQBuffer bb2 = wrapBuffer(bytes2);
    String s3 = "echidna";
    byte[] bytes3 = s3.getBytes(StandardCharsets.UTF_8);
    ActiveMQBuffer bb3 = wrapBuffer(bytes3);
    long initialPos = sf.position();
    sf.write(bb1, true);
    long bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
    initialPos = sf.position();
    sf.write(bb2, true);
    bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesWritten);
    initialPos = sf.position();
    sf.write(bb3, true);
    bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesWritten);
    sf.position(0);
    ByteBuffer rb1 = factory.newBuffer(bytes1.length);
    ByteBuffer rb2 = factory.newBuffer(bytes2.length);
    ByteBuffer rb3 = factory.newBuffer(bytes3.length);
    int bytesRead = sf.read(rb1);
    Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesRead);
    for (int i = 0; i < bytes1.length; i++) {
        Assert.assertEquals(bytes1[i], rb1.get(i));
    }
    bytesRead = sf.read(rb2);
    Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesRead);
    for (int i = 0; i < bytes2.length; i++) {
        Assert.assertEquals(bytes2[i], rb2.get(i));
    }
    bytesRead = sf.read(rb3);
    Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesRead);
    for (int i = 0; i < bytes3.length; i++) {
        Assert.assertEquals(bytes3[i], rb3.get(i));
    }
    sf.close();
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) ByteBuffer(java.nio.ByteBuffer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Aggregations

ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)150 Test (org.junit.Test)81 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)49 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)44 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)39 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)38 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)35 ByteBuffer (java.nio.ByteBuffer)34 Configuration (org.apache.activemq.artemis.core.config.Configuration)34 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)27 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)25 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)25 ArrayList (java.util.ArrayList)21 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)21 Queue (org.apache.activemq.artemis.core.server.Queue)18 HashMap (java.util.HashMap)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)15 RecordInfo (org.apache.activemq.artemis.core.journal.RecordInfo)15 CoreMessage (org.apache.activemq.artemis.core.message.impl.CoreMessage)14