diff --git a/libraries/pom.xml b/libraries/pom.xml index c3535f884a..231233afbb 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -331,6 +331,11 @@ datanucleus-xml 5.0.0-release + + net.openhft + chronicle + 3.6.4 + org.springframework spring-web diff --git a/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java b/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java new file mode 100644 index 0000000000..b7770e0b78 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/chronicle/queue/ChronicleQueue.java @@ -0,0 +1,23 @@ +package com.baeldung.chronicle.queue; + +import java.io.IOException; + +import net.openhft.chronicle.Chronicle; +import net.openhft.chronicle.ExcerptAppender; + +public class ChronicleQueue { + + public static void writeToQueue( + Chronicle chronicle, String stringValue, int intValue, long longValue, double doubleValue) + throws IOException { + ExcerptAppender appender = chronicle.createAppender(); + appender.startExcerpt(); + appender.writeUTF(stringValue); + appender.writeInt(intValue); + appender.writeLong(longValue); + appender.writeDouble(doubleValue); + appender.finish(); + appender.close(); + } + +} diff --git a/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueTest.java b/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueTest.java new file mode 100644 index 0000000000..e64aaed544 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/chronicle/queue/ChronicleQueueTest.java @@ -0,0 +1,43 @@ +package com.baeldung.chronicle.queue; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import org.junit.Test; + +import net.openhft.chronicle.Chronicle; +import net.openhft.chronicle.ChronicleQueueBuilder; +import net.openhft.chronicle.ExcerptTailer; +import net.openhft.chronicle.tools.ChronicleTools; + +public class ChronicleQueueTest { + + @Test + public void givenSetOfValues_whenWriteToQueue_thenWriteSuccesfully() throws IOException { + File queueDir = Files.createTempDirectory("chronicle-queue").toFile(); + ChronicleTools.deleteOnExit(queueDir.getPath()); + + Chronicle chronicle = ChronicleQueueBuilder.indexed(queueDir).build(); + String stringVal = "Hello World"; + int intVal = 101; + long longVal = System.currentTimeMillis(); + double doubleVal = 90.00192091d; + + ChronicleQueue.writeToQueue(chronicle, stringVal, intVal, longVal, doubleVal); + + ExcerptTailer tailer = chronicle.createTailer(); + while (tailer.nextIndex()) { + assertEquals(stringVal, tailer.readUTF()); + assertEquals(intVal, tailer.readInt()); + assertEquals(longVal, tailer.readLong()); + assertEquals((Double) doubleVal, (Double) tailer.readDouble()); + } + tailer.finish(); + tailer.close(); + chronicle.close(); + } + +}