org.apache.activemq.artemis.core.journal.Journal

Here are the examples of the java api class org.apache.activemq.artemis.core.journal.Journal taken from open source projects.

1. XmlDataExporter#getBindings()

Project: activemq-artemis
File: XmlDataExporter.java
/**
    * Open the bindings journal and extract all bindings data.
    *
    * @throws Exception will be thrown if anything goes wrong reading the bindings journal
    */
private void getBindings() throws Exception {
    List<RecordInfo> records = new LinkedList<>();
    Journal bindingsJournal = storageManager.getBindingsJournal();
    bindingsJournal.start();
    ActiveMQServerLogger.LOGGER.debug("Reading bindings journal from " + config.getBindingsDirectory());
    ((JournalImpl) bindingsJournal).load(records, null, null, false);
    for (RecordInfo info : records) {
        if (info.getUserRecordType() == JournalRecordIds.QUEUE_BINDING_RECORD) {
            PersistentQueueBindingEncoding bindingEncoding = (PersistentQueueBindingEncoding) DescribeJournal.newObjectEncoding(info, null);
            queueBindings.put(bindingEncoding.getId(), bindingEncoding);
        }
    }
    bindingsJournal.stop();
}

2. ReplicationEndpoint#handleAppendAddRecord()

Project: activemq-artemis
File: ReplicationEndpoint.java
/**
    * @param packet
    * @throws Exception
    */
private void handleAppendAddRecord(final ReplicationAddMessage packet) throws Exception {
    Journal journalToUse = getJournal(packet.getJournalID());
    if (packet.getRecord() == ADD_OPERATION_TYPE.UPDATE) {
        if (logger.isTraceEnabled()) {
            logger.trace("Endpoint appendUpdate id = " + packet.getId());
        }
        journalToUse.appendUpdateRecord(packet.getId(), packet.getJournalRecordType(), packet.getRecordData(), noSync);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("Endpoint append id = " + packet.getId());
        }
        journalToUse.appendAddRecord(packet.getId(), packet.getJournalRecordType(), packet.getRecordData(), noSync);
    }
}

3. ReplicationTest#testSendPackets()

Project: activemq-artemis
File: ReplicationTest.java
@Test
public void testSendPackets() throws Exception {
    setupServer(true);
    StorageManager storage = getStorage();
    manager = liveServer.getReplicationManager();
    waitForComponent(manager);
    Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
    replicatedJournal.appendPrepareRecord(1, new FakeData(), false);
    replicatedJournal.appendAddRecord(1, (byte) 1, new FakeData(), false);
    replicatedJournal.appendUpdateRecord(1, (byte) 2, new FakeData(), false);
    replicatedJournal.appendDeleteRecord(1, false);
    replicatedJournal.appendAddRecordTransactional(2, 2, (byte) 1, new FakeData());
    replicatedJournal.appendUpdateRecordTransactional(2, 2, (byte) 2, new FakeData());
    replicatedJournal.appendCommitRecord(2, false);
    replicatedJournal.appendDeleteRecordTransactional(3, 4, new FakeData());
    replicatedJournal.appendPrepareRecord(3, new FakeData(), false);
    replicatedJournal.appendRollbackRecord(3, false);
    blockOnReplication(storage, manager);
    Assert.assertTrue("Expecting no active tokens:" + manager.getActiveTokens(), manager.getActiveTokens().isEmpty());
    ServerMessage msg = new ServerMessageImpl(1, 1024);
    SimpleString dummy = new SimpleString("dummy");
    msg.setAddress(dummy);
    replicatedJournal.appendAddRecordTransactional(23, 24, (byte) 1, new FakeData());
    PagedMessage pgmsg = new PagedMessageImpl(msg, new long[0]);
    manager.pageWrite(pgmsg, 1);
    manager.pageWrite(pgmsg, 2);
    manager.pageWrite(pgmsg, 3);
    manager.pageWrite(pgmsg, 4);
    blockOnReplication(storage, manager);
    PagingManager pagingManager = createPageManager(backupServer.getStorageManager(), backupServer.getConfiguration(), backupServer.getExecutorFactory(), backupServer.getAddressSettingsRepository());
    PagingStore store = pagingManager.getPageStore(dummy);
    store.start();
    Assert.assertEquals(4, store.getNumberOfPages());
    store.stop();
    manager.pageDeleted(dummy, 1);
    manager.pageDeleted(dummy, 2);
    manager.pageDeleted(dummy, 3);
    manager.pageDeleted(dummy, 4);
    manager.pageDeleted(dummy, 5);
    manager.pageDeleted(dummy, 6);
    blockOnReplication(storage, manager);
    ServerMessageImpl serverMsg = new ServerMessageImpl();
    serverMsg.setMessageID(500);
    serverMsg.setAddress(new SimpleString("tttt"));
    ActiveMQBuffer buffer = ActiveMQBuffers.dynamicBuffer(100);
    serverMsg.encodeHeadersAndProperties(buffer);
    manager.largeMessageBegin(500);
    manager.largeMessageWrite(500, new byte[1024]);
    manager.largeMessageDelete(Long.valueOf(500));
    blockOnReplication(storage, manager);
    store.start();
    Assert.assertEquals(0, store.getNumberOfPages());
}

4. BatchIDGeneratorUnitTest#testSequence()

Project: activemq-artemis
File: BatchIDGeneratorUnitTest.java
@Test
public void testSequence() throws Exception {
    NIOSequentialFileFactory factory = new NIOSequentialFileFactory(new File(getTestDir()), 1);
    Journal journal = new JournalImpl(10 * 1024, 2, 2, 0, 0, factory, "activemq-bindings", "bindings", 1);
    journal.start();
    journal.load(new ArrayList<RecordInfo>(), new ArrayList<PreparedTransactionInfo>(), null);
    BatchingIDGenerator batch = new BatchingIDGenerator(0, 1000, getJournalStorageManager(journal));
    long id1 = batch.generateID();
    long id2 = batch.generateID();
    Assert.assertTrue(id2 > id1);
    journal.stop();
    batch = new BatchingIDGenerator(0, 1000, getJournalStorageManager(journal));
    loadIDs(journal, batch);
    long id3 = batch.generateID();
    Assert.assertEquals(1000, id3);
    long id4 = batch.generateID();
    Assert.assertTrue(id4 > id3 && id4 < 2000);
    batch.persistCurrentID();
    journal.stop();
    batch = new BatchingIDGenerator(0, 1000, getJournalStorageManager(journal));
    loadIDs(journal, batch);
    long id5 = batch.generateID();
    Assert.assertTrue(id5 > id4 && id5 < 2000);
    long lastId = id5;
    boolean close = true;
    for (int i = 0; i < 100000; i++) {
        if (i % 1000 == 0) {
            // interchanging closes and simulated crashes
            if (close) {
                batch.persistCurrentID();
            }
            close = !close;
            journal.stop();
            batch = new BatchingIDGenerator(0, 1000, getJournalStorageManager(journal));
            loadIDs(journal, batch);
        }
        long id = batch.generateID();
        Assert.assertTrue(id > lastId);
        lastId = id;
    }
    batch.persistCurrentID();
    journal.stop();
    batch = new BatchingIDGenerator(0, 1000, getJournalStorageManager(journal));
    loadIDs(journal, batch);
    lastId = batch.getCurrentID();
    journal.stop();
    batch = new BatchingIDGenerator(0, 1000, getJournalStorageManager(journal));
    loadIDs(journal, batch);
    Assert.assertEquals("No Ids were generated, so the currentID was supposed to stay the same", lastId, batch.getCurrentID());
    journal.stop();
}

5. JournalImplTestUnit#internaltestSpeedNonTransactional()

Project: activemq-artemis
File: JournalImplTestUnit.java
private void internaltestSpeedNonTransactional() throws Exception {
    final long numMessages = 10000;
    int numFiles = (int) ((numMessages * 1024 + 512) / (10 * 1024 * 1024) * 1.3);
    if (numFiles < 2) {
        numFiles = 2;
    }
    JournalImplTestUnit.log.debug("num Files=" + numFiles);
    Journal journal = new JournalImpl(10 * 1024 * 1024, numFiles, numFiles, 0, 0, getFileFactory(), "activemq-data", "amq", 5000);
    journal.start();
    journal.load(new ArrayList<RecordInfo>(), null, null);
    JournalImplTestUnit.log.debug("Adding data");
    SimpleEncoding data = new SimpleEncoding(700, (byte) 'j');
    long start = System.currentTimeMillis();
    for (int i = 0; i < numMessages; i++) {
        journal.appendAddRecord(i, (byte) 0, data, true);
    }
    long end = System.currentTimeMillis();
    double rate = 1000 * (double) numMessages / (end - start);
    JournalImplTestUnit.log.info("Rate " + rate + " records/sec");
    journal.stop();
    journal = new JournalImpl(10 * 1024 * 1024, numFiles, numFiles, 0, 0, getFileFactory(), "activemq-data", "amq", 5000);
    journal.start();
    journal.load(new ArrayList<RecordInfo>(), null, null);
    journal.stop();
}

6. JournalImplTestUnit#testSpeedTransactional()

Project: activemq-artemis
File: JournalImplTestUnit.java
@Test
public void testSpeedTransactional() throws Exception {
    Journal journal = new JournalImpl(10 * 1024 * 1024, 10, 10, 0, 0, getFileFactory(), "activemq-data", "amq", 5000);
    journal.start();
    journal.load(new ArrayList<RecordInfo>(), null, null);
    try {
        final int numMessages = 50050;
        SimpleEncoding data = new SimpleEncoding(1024, (byte) 'j');
        long start = System.currentTimeMillis();
        int count = 0;
        double[] rates = new double[50];
        for (int i = 0; i < 50; i++) {
            long startTrans = System.currentTimeMillis();
            for (int j = 0; j < 1000; j++) {
                journal.appendAddRecordTransactional(i, count++, (byte) 0, data);
            }
            journal.appendCommitRecord(i, true);
            long endTrans = System.currentTimeMillis();
            rates[i] = 1000 * (double) 1000 / (endTrans - startTrans);
        }
        long end = System.currentTimeMillis();
        for (double rate : rates) {
            JournalImplTestUnit.log.info("Transaction Rate = " + rate + " records/sec");
        }
        double rate = 1000 * (double) numMessages / (end - start);
        JournalImplTestUnit.log.info("Rate " + rate + " records/sec");
    } finally {
        journal.stop();
    }
}

7. XmlDataExporter#getJmsBindings()

Project: activemq-artemis
File: XmlDataExporter.java
private void getJmsBindings() throws Exception {
    SequentialFileFactory bindingsJMS = new NIOSequentialFileFactory(config.getBindingsLocation(), 1);
    Journal jmsJournal = new JournalImpl(1024 * 1024, 2, 2, config.getJournalCompactMinFiles(), config.getJournalCompactPercentage(), bindingsJMS, "activemq-jms", "jms", 1);
    jmsJournal.start();
    List<RecordInfo> data = new ArrayList<>();
    ArrayList<PreparedTransactionInfo> list = new ArrayList<>();
    ActiveMQServerLogger.LOGGER.debug("Reading jms bindings journal from " + config.getBindingsDirectory());
    jmsJournal.load(data, list, null);
    for (RecordInfo record : data) {
        long id = record.id;
        ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(record.data);
        byte rec = record.getUserRecordType();
        if (rec == JMSJournalStorageManagerImpl.CF_RECORD) {
            PersistedConnectionFactory cf = new PersistedConnectionFactory();
            cf.decode(buffer);
            cf.setId(id);
            ActiveMQServerLogger.LOGGER.info("Found JMS connection factory: " + cf.getName());
            jmsConnectionFactories.put(cf.getName(), cf);
        } else if (rec == JMSJournalStorageManagerImpl.DESTINATION_RECORD) {
            PersistedDestination destination = new PersistedDestination();
            destination.decode(buffer);
            destination.setId(id);
            ActiveMQServerLogger.LOGGER.info("Found JMS destination: " + destination.getName());
            jmsDestinations.put(new Pair<>(destination.getType(), destination.getName()), destination);
        } else if (rec == JMSJournalStorageManagerImpl.BINDING_RECORD) {
            PersistedBindings jndi = new PersistedBindings();
            jndi.decode(buffer);
            jndi.setId(id);
            Pair<PersistedType, String> key = new Pair<>(jndi.getType(), jndi.getName());
            StringBuilder builder = new StringBuilder();
            for (String binding : jndi.getBindings()) {
                builder.append(binding).append(" ");
            }
            ActiveMQServerLogger.LOGGER.info("Found JMS JNDI binding data for " + jndi.getType() + " " + jndi.getName() + ": " + builder.toString());
            jmsJNDI.put(key, jndi);
        } else {
            throw new IllegalStateException("Invalid record type " + rec);
        }
    }
}

8. XmlDataExporter#processMessageJournal()

Project: activemq-artemis
File: XmlDataExporter.java
/**
    * Read through the message journal and stuff all the events/data we care about into local data structures.  We'll
    * use this data later to print all the right information.
    *
    * @throws Exception will be thrown if anything goes wrong reading the journal
    */
private void processMessageJournal() throws Exception {
    ArrayList<RecordInfo> acks = new ArrayList<>();
    List<RecordInfo> records = new LinkedList<>();
    // We load these, but don't use them.
    List<PreparedTransactionInfo> preparedTransactions = new LinkedList<>();
    Journal messageJournal = storageManager.getMessageJournal();
    ActiveMQServerLogger.LOGGER.debug("Reading journal from " + config.getJournalDirectory());
    messageJournal.start();
    // Just logging these, no action necessary
    TransactionFailureCallback transactionFailureCallback = new TransactionFailureCallback() {

        @Override
        public void failedTransaction(long transactionID, List<RecordInfo> records1, List<RecordInfo> recordsToDelete) {
            StringBuilder message = new StringBuilder();
            message.append("Encountered failed journal transaction: ").append(transactionID);
            for (int i = 0; i < records1.size(); i++) {
                if (i == 0) {
                    message.append("; Records: ");
                }
                message.append(records1.get(i));
                if (i != (records1.size() - 1)) {
                    message.append(", ");
                }
            }
            for (int i = 0; i < recordsToDelete.size(); i++) {
                if (i == 0) {
                    message.append("; RecordsToDelete: ");
                }
                message.append(recordsToDelete.get(i));
                if (i != (recordsToDelete.size() - 1)) {
                    message.append(", ");
                }
            }
            ActiveMQServerLogger.LOGGER.debug(message.toString());
        }
    };
    ((JournalImpl) messageJournal).load(records, preparedTransactions, transactionFailureCallback, false);
    // Since we don't use these nullify the reference so that the garbage collector can clean them up
    preparedTransactions = null;
    for (RecordInfo info : records) {
        byte[] data = info.data;
        ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(data);
        Object o = DescribeJournal.newObjectEncoding(info, storageManager);
        if (info.getUserRecordType() == JournalRecordIds.ADD_MESSAGE) {
            messages.put(info.id, ((MessageDescribe) o).getMsg());
        } else if (info.getUserRecordType() == JournalRecordIds.ADD_LARGE_MESSAGE) {
            messages.put(info.id, ((MessageDescribe) o).getMsg());
        } else if (info.getUserRecordType() == JournalRecordIds.ADD_REF) {
            ReferenceDescribe ref = (ReferenceDescribe) o;
            HashMap<Long, ReferenceDescribe> map = messageRefs.get(info.id);
            if (map == null) {
                HashMap<Long, ReferenceDescribe> newMap = new HashMap<>();
                newMap.put(ref.refEncoding.queueID, ref);
                messageRefs.put(info.id, newMap);
            } else {
                map.put(ref.refEncoding.queueID, ref);
            }
        } else if (info.getUserRecordType() == JournalRecordIds.ACKNOWLEDGE_REF) {
            acks.add(info);
        } else if (info.userRecordType == JournalRecordIds.ACKNOWLEDGE_CURSOR) {
            CursorAckRecordEncoding encoding = new CursorAckRecordEncoding();
            encoding.decode(buff);
            Set<PagePosition> set = cursorRecords.get(encoding.queueID);
            if (set == null) {
                set = new HashSet<>();
                cursorRecords.put(encoding.queueID, set);
            }
            set.add(encoding.position);
        } else if (info.userRecordType == JournalRecordIds.PAGE_TRANSACTION) {
            if (info.isUpdate) {
                PageUpdateTXEncoding pageUpdate = new PageUpdateTXEncoding();
                pageUpdate.decode(buff);
                pgTXs.add(pageUpdate.pageTX);
            } else {
                PageTransactionInfoImpl pageTransactionInfo = new PageTransactionInfoImpl();
                pageTransactionInfo.decode(buff);
                pageTransactionInfo.setRecordID(info.id);
                pgTXs.add(pageTransactionInfo.getTransactionID());
            }
        }
    }
    messageJournal.stop();
    removeAcked(acks);
}

9. ReplicationTest#testNoActions()

Project: activemq-artemis
File: ReplicationTest.java
@Test
public void testNoActions() throws Exception {
    setupServer(true);
    StorageManager storage = getStorage();
    manager = liveServer.getReplicationManager();
    waitForComponent(manager);
    Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
    replicatedJournal.appendPrepareRecord(1, new FakeData(), false);
    final CountDownLatch latch = new CountDownLatch(1);
    storage.afterCompleteOperations(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
        }

        @Override
        public void done() {
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
    Assert.assertEquals("should be empty " + manager.getActiveTokens(), 0, manager.getActiveTokens().size());
}

10. ReplicationEndpoint#handleAppendDelete()

Project: activemq-artemis
File: ReplicationEndpoint.java
/**
    * @param packet
    */
private void handleAppendDelete(final ReplicationDeleteMessage packet) throws Exception {
    Journal journalToUse = getJournal(packet.getJournalID());
    journalToUse.appendDeleteRecord(packet.getId(), noSync);
}

11. ReplicationEndpoint#handleAppendDeleteTX()

Project: activemq-artemis
File: ReplicationEndpoint.java
/**
    * @param packet
    */
private void handleAppendDeleteTX(final ReplicationDeleteTXMessage packet) throws Exception {
    Journal journalToUse = getJournal(packet.getJournalID());
    journalToUse.appendDeleteRecordTransactional(packet.getTxId(), packet.getId(), packet.getRecordData());
}

12. ReplicationEndpoint#handlePrepare()

Project: activemq-artemis
File: ReplicationEndpoint.java
/**
    * @param packet
    */
private void handlePrepare(final ReplicationPrepareMessage packet) throws Exception {
    Journal journalToUse = getJournal(packet.getJournalID());
    journalToUse.appendPrepareRecord(packet.getTxId(), packet.getRecordData(), noSync);
}

13. JournalStorageManager#init()

Project: activemq-artemis
File: JournalStorageManager.java
@Override
protected void init(Configuration config, IOCriticalErrorListener criticalErrorListener) {
    if (config.getJournalType() != JournalType.NIO && config.getJournalType() != JournalType.ASYNCIO) {
        throw ActiveMQMessageBundle.BUNDLE.invalidJournal();
    }
    SequentialFileFactory bindingsFF = new NIOSequentialFileFactory(config.getBindingsLocation(), criticalErrorListener, config.getJournalMaxIO_NIO());
    Journal localBindings = new JournalImpl(1024 * 1024, 2, config.getJournalCompactMinFiles(), config.getJournalPoolFiles(), config.getJournalCompactPercentage(), bindingsFF, "activemq-bindings", "bindings", 1);
    bindingsJournal = localBindings;
    originalBindingsJournal = localBindings;
    if (config.getJournalType() == JournalType.ASYNCIO) {
        ActiveMQServerLogger.LOGGER.journalUseAIO();
        journalFF = new AIOSequentialFileFactory(config.getJournalLocation(), config.getJournalBufferSize_AIO(), config.getJournalBufferTimeout_AIO(), config.getJournalMaxIO_AIO(), config.isLogJournalWriteRate(), criticalErrorListener);
    } else if (config.getJournalType() == JournalType.NIO) {
        ActiveMQServerLogger.LOGGER.journalUseNIO();
        journalFF = new NIOSequentialFileFactory(config.getJournalLocation(), true, config.getJournalBufferSize_NIO(), config.getJournalBufferTimeout_NIO(), config.getJournalMaxIO_NIO(), config.isLogJournalWriteRate(), criticalErrorListener);
    } else {
        throw ActiveMQMessageBundle.BUNDLE.invalidJournalType2(config.getJournalType());
    }
    Journal localMessage = new JournalImpl(config.getJournalFileSize(), config.getJournalMinFiles(), config.getJournalPoolFiles(), config.getJournalCompactMinFiles(), config.getJournalCompactPercentage(), journalFF, "activemq-data", "amq", config.getJournalType() == JournalType.ASYNCIO ? config.getJournalMaxIO_AIO() : config.getJournalMaxIO_NIO());
    messageJournal = localMessage;
    originalMessageJournal = localMessage;
    largeMessagesDirectory = config.getLargeMessagesDirectory();
    largeMessagesFactory = new NIOSequentialFileFactory(config.getLargeMessagesLocation(), false, criticalErrorListener, 1);
    perfBlastPages = config.getJournalPerfBlastPages();
    if (config.getPageMaxConcurrentIO() != 1) {
        pageMaxConcurrentIO = new Semaphore(config.getPageMaxConcurrentIO());
    } else {
        pageMaxConcurrentIO = null;
    }
}

14. ReplicationTest#testOrderOnNonPersistency()

Project: activemq-artemis
File: ReplicationTest.java
@Test
public void testOrderOnNonPersistency() throws Exception {
    setupServer(true);
    final ArrayList<Integer> executions = new ArrayList<>();
    StorageManager storage = getStorage();
    manager = liveServer.getReplicationManager();
    Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
    int numberOfAdds = 200;
    final CountDownLatch latch = new CountDownLatch(numberOfAdds);
    OperationContext ctx = storage.getContext();
    for (int i = 0; i < numberOfAdds; i++) {
        final int nAdd = i;
        if (i % 2 == 0) {
            replicatedJournal.appendPrepareRecord(i, new FakeData(), false);
        }
        ctx.executeOnCompletion(new IOCallback() {

            @Override
            public void onError(final int errorCode, final String errorMessage) {
            }

            @Override
            public void done() {
                executions.add(nAdd);
                latch.countDown();
            }
        });
    }
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    for (int i = 0; i < numberOfAdds; i++) {
        Assert.assertEquals(i, executions.get(i).intValue());
    }
    Assert.assertEquals(0, manager.getActiveTokens().size());
}

15. ReplicationEndpoint#handleAppendAddTXRecord()

Project: activemq-artemis
File: ReplicationEndpoint.java
/**
    * @param packet
    */
private void handleAppendAddTXRecord(final ReplicationAddTXMessage packet) throws Exception {
    Journal journalToUse = getJournal(packet.getJournalID());
    if (packet.getOperation() == ADD_OPERATION_TYPE.UPDATE) {
        journalToUse.appendUpdateRecordTransactional(packet.getTxId(), packet.getId(), packet.getRecordType(), packet.getRecordData());
    } else {
        journalToUse.appendAddRecordTransactional(packet.getTxId(), packet.getId(), packet.getRecordType(), packet.getRecordData());
    }
}

16. ReplicationEndpoint#handleCommitRollback()

Project: activemq-artemis
File: ReplicationEndpoint.java
/**
    * @param packet
    */
private void handleCommitRollback(final ReplicationCommitMessage packet) throws Exception {
    Journal journalToUse = getJournal(packet.getJournalID());
    if (packet.isRollback()) {
        journalToUse.appendRollbackRecord(packet.getTxId(), noSync);
    } else {
        journalToUse.appendCommitRecord(packet.getTxId(), noSync);
    }
}