Search in sources :

Example 6 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class ChronicleQueueMethodsWithoutParameters method test.

@Test
public void test() throws IOException, InterruptedException {
    File file = getTmpDir();
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(file).testBlockSize().rollCycle(TEST_DAILY).build()) {
        SomeListener someListener = queue.acquireAppender().methodWriter(SomeListener.class);
        SomeManager someManager = new SomeManager();
        MethodReader reader = queue.createTailer().methodReader(someManager);
        LOG.debug("Writing to queue");
        someListener.methodWithOneParam(1);
        someListener.methodWithoutParams();
        LOG.debug("Reading from queue");
        assertTrue(reader.readOne());
        assertTrue(reader.readOne());
        assertFalse(reader.readOne());
        // one param method was invoked
        assertTrue(someManager.methodWithOneParamInvoked);
        // no params method was NOT invoked
        assertTrue(someManager.methodWithoutParamsInvoked);
        LOG.warn(queue.dump());
    }
}
Also used : File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Example 7 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class JDBCServiceTest method doCreateTable.

public void doCreateTable(int repeats, int noUpdates) throws SQLException {
    for (int t = 0; t < repeats; t++) {
        long start = System.nanoTime(), written;
        File path1 = DirectoryUtils.tempDir("createTable1");
        File path2 = DirectoryUtils.tempDir("createTable2");
        File file = new File(OS.TARGET, "hsqldb-" + System.nanoTime());
        file.deleteOnExit();
        try (ChronicleQueue in = SingleChronicleQueueBuilder.binary(path1).testBlockSize().build();
            ChronicleQueue out = SingleChronicleQueueBuilder.binary(path2).testBlockSize().build()) {
            JDBCService service = new JDBCService(in, out, () -> DriverManager.getConnection("jdbc:hsqldb:file:" + file.getAbsolutePath(), "SA", ""));
            JDBCStatement writer = service.createWriter();
            writer.executeUpdate("CREATE TABLE tableName (\n" + "name VARCHAR(64) NOT NULL,\n" + "num INT\n" + ")\n");
            for (int i = 1; i < (long) noUpdates; i++) writer.executeUpdate("INSERT INTO tableName (name, num)\n" + "VALUES (?, ?)", "name", i);
            written = System.nanoTime() - start;
            AtomicLong queries = new AtomicLong();
            AtomicLong updates = new AtomicLong();
            CountingJDBCResult countingJDBCResult = new CountingJDBCResult(queries, updates);
            MethodReader methodReader = service.createReader(countingJDBCResult);
            int counter = 0;
            while (updates.get() < noUpdates) {
                if (methodReader.readOne())
                    counter++;
                else
                    Thread.yield();
            }
            Closeable.closeQuietly(service);
            // System.out.println(in.dump());
            // System.out.println(out.dump());
            long time = System.nanoTime() - start;
            System.out.printf("Average time to write each update %.1f us, average time to perform each update %.1f us%n", written / noUpdates / 1e3, time / noUpdates / 1e3);
        } finally {
            try {
                IOTools.deleteDirWithFiles(path1, 2);
                IOTools.deleteDirWithFiles(path2, 2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Example 8 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class MessageHistoryTest method shouldAccessMessageHistory.

@Test
public void shouldAccessMessageHistory() throws Exception {
    try (final SingleChronicleQueue inputQueue = createQueue(inputQueueDir, 1);
        final SingleChronicleQueue outputQueue = createQueue(outputQueueDir, 2)) {
        generateTestData(inputQueue, outputQueue);
        final ExcerptTailer tailer = outputQueue.createTailer();
        final ValidatingSecond validatingSecond = new ValidatingSecond();
        final MethodReader validator = tailer.methodReader(validatingSecond);
        assertThat(validator.readOne(), is(true));
        assertThat(validatingSecond.messageHistoryPresent(), is(true));
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 9 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class MessageHistoryTest method generateTestData.

private void generateTestData(final SingleChronicleQueue inputQueue, final SingleChronicleQueue outputQueue) {
    final First first = inputQueue.acquireAppender().methodWriterBuilder(First.class).recordHistory(true).build();
    first.say("one");
    first.say("two");
    first.say("three");
    final LoggingFirst loggingFirst = new LoggingFirst(outputQueue.acquireAppender().methodWriterBuilder(Second.class).build());
    final MethodReader reader = inputQueue.createTailer().methodReaderBuilder().build(loggingFirst);
    assertThat(reader.readOne(), is(true));
    assertThat(reader.readOne(), is(true));
    // roll queue file
    clock.addAndGet(TimeUnit.DAYS.toMillis(2));
    assertThat(reader.readOne(), is(true));
    assertThat(reader.readOne(), is(false));
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader)

Example 10 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class StageMain method main.

public static void main(String[] args) throws IOException {
    // MlockAll.doMlockall();
    if (args.length != 2) {
        throw new IllegalArgumentException("Usage: <program> [resource-name] [stage-index]");
    }
    final ConfigParser configParser = new ConfigParser(args[0]);
    final StageConfig stageConfig = configParser.getStageConfig(Integer.parseInt(args[1]));
    final ExecutorService service = Executors.newFixedThreadPool(stageConfig.getStageIndices().size() + 1);
    service.submit(new PretoucherTask(outputQueue(stageConfig.getOutputPath(), UNSET_SOURCE), configParser.getPretouchIntervalMillis()));
    for (Integer index : stageConfig.getStageIndices()) {
        service.submit(() -> {
            final Stage stage = new Stage(createOutput(stageConfig.getOutputPath(), index + 1), index);
            final MethodReader reader = createReader(stageConfig.getInputPath(), stage);
            Thread.currentThread().setName("load.stage-consumer-" + index);
            boolean warnOnce = false;
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    reader.readOne();
                } catch (Exception e) {
                    if (!warnOnce) {
                        e.printStackTrace();
                        warnOnce = true;
                    }
                }
            }
        });
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ConfigParser(net.openhft.load.config.ConfigParser) MethodReader(net.openhft.chronicle.bytes.MethodReader) StageConfig(net.openhft.load.config.StageConfig) IOException(java.io.IOException)

Aggregations

MethodReader (net.openhft.chronicle.bytes.MethodReader)22 Test (org.junit.Test)13 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)10 File (java.io.File)9 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)3 OS (net.openhft.chronicle.core.OS)3 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)3 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)3 ConfigParser (net.openhft.load.config.ConfigParser)3 StageConfig (net.openhft.load.config.StageConfig)3 FileWriter (java.io.FileWriter)2 Writer (java.io.Writer)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 IOTools (net.openhft.chronicle.core.io.IOTools)2 RollCycles (net.openhft.chronicle.queue.RollCycles)2 MessageHistory (net.openhft.chronicle.wire.MessageHistory)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2