Ultra fast Reliable Messaging in Java
Join the DZone community and get the full member experience.
Join For FreeAfter cloning project from GitHub we can find two major in this context classes: ChronicleSource and ChronicleSink. The first will be our master server, which will be used as endpoint for getting new excerpts (in this post you can assume that is the same as message). It will use the same datastore which is used by message producer. ChronicleSink will connect to source server and will replicate messages into new datastore, even on remote server as it works over TCP.
Ok, at first we've to implement our producer class:
public class ChronicleProducer { private static final int MAX_MESSAGES = 10000000; public static void main(String[] args) throws IOException { IndexedChronicle chronicle = new IndexedChronicle("/tmp/chronicle_in"); Excerpt excerpt = chronicle.createExcerpt(); System.out.println("TestMessageInTheBottle".length()); for (int i = 1; i < MAX_MESSAGES + 1; i++) { excerpt.startExcerpt(36); excerpt.writeLong(System.nanoTime()); excerpt.writeBytes("TestMessageInTheBottle"); excerpt.writeInt(i); excerpt.writeBoolean(i == MAX_MESSAGES); excerpt.finish(); } chronicle.close(); } }
Then we need something to consume our messages:
public class ChronicleConsumer { public static void main(String[] args) throws IOException { IndexedChronicle chronicle = new IndexedChronicle("/tmp/chronicle_out"); Excerpt excerpt = chronicle.createExcerpt(); while (true) { if (excerpt.nextIndex()) { long timestamp = excerpt.readLong(); String message = excerpt.readByteString(); int index = excerpt.readInt(); System.out.println(index + " message: " + message + " created at timestamp " + timestamp); if (excerpt.readBoolean()) { break; } } else { LockSupport.parkNanos(TimeUnit.MICROSECONDS.toNanos(1)); } } chronicle.close(); } }
Now we can start up all service!
- $ java com.higherfrequencytrading.chronicle.tcp.ChronicleSource /tmp/chronicle_in 8099
- $ java com.higherfrequencytrading.chronicle.tcp.ChronicleSink /tmp/chronicle_out localhost 8099
- $ java ChronicleConsumer
- $ java ChronicleProducer
Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments