0

I would like to write different types of messages to a chronicle-queue, and process messages in consumers depending on their types.

How can I do that?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
xiagao1982
  • 1,077
  • 1
  • 13
  • 25

3 Answers3

1

Chronicle-Queue provides low level building blocks you can use to write any kind of message so it is up to you to choose the right data structure.

As example, you can prefix the data you write to a chronicle with a small header with some meta-data and then use it as discriminator for data processing.

Luca Burgazzoli
  • 1,216
  • 7
  • 9
1

To achieve this I use Wire

try (DocumentContext dc = appender.writingDocument())
{
    final Wire wire = dc.wire();
    final ValueOut v = wire.getValueOut();
    valueOut.typePrefix(m.getClass());
    valueOut.marshallable(m);
}

When reading back I:

try (DocumentContext dc = tailer.readingDocument())
{
   final Wire wire = dc.wire();
   final ValueIn valueIn = wire.getValueIn();
   final Class clazz = valueIn.typePrefix();
   // msgPool is a prealloacted hashmap containing the messages I read
   final ReadMarshallable readObject = msgPool.get(clazz);  
   valueIn.readMarshallable(readObject)
   // readObject can now be used
}
0

You can also write/read a generic object. This will be slightly slower than using your own scheme, but is it a simple way to always read the type you wrote.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130