简体   繁体   English

Chronicle Queue 使用 readDocument 读取任何类型的消息

[英]Chronicle Queue reading any kind of message with readDocument

In the Chronicle Queue I have two types of messages written.在编年史队列中,我写了两种类型的消息。 I wanna read this messages using the same tailer and if it is possible with the same method for example using tailer.readDocument().我想使用相同的 tailer 阅读此消息,如果可以使用相同的方法,例如使用 tailer.readDocument()。

Anyone now if it is possible, the message types are from different kind of objects.现在任何人都可以,如果可能的话,消息类型来自不同类型的对象。 They haven't relationship.他们没有关系。

In my actual reading logic I need to read all the entries of the queue and the order is important, for example:在我的实际阅读逻辑中,我需要阅读队列的所有条目,并且顺序很重要,例如:

Queue MessageA MessageA MessageB队列 MessageA MessageA MessageB

I need to read message B only after message A in this example, because of that I am looking for a method that read all the entries independent of message type.在此示例中,我只需要在消息 A 之后读取消息 B,因此我正在寻找一种方法来读取与消息类型无关的所有条目。

The simplest approach is to write messages using a MethodWriter/MethodReaderhttps://github.com/OpenHFT/Chronicle-Queue#high-level-interface最简单的方法是使用 MethodWriter/MethodReaderhttps://github.com/OpenHFT/Chronicle-Queue#high-level-interface编写消息

You start by defining an asynchronous interface, where all methods have:您首先定义一个异步接口,其中所有方法都具有:

  • arguments which are only inputs arguments 仅作为输入
  • no return value or exceptions expected.没有返回值或预期异常。

A simple asynchronous interface一个简单的异步接口

import net.openhft.chronicle.wire.SelfDescribingMarshallable;
interface MessageListener {
    void method1(Message1 message);

    void method2(Message2 message);
}

static class Message1 extends SelfDescribingMarshallable {
    String text;

    public Message1(String text) {
        this.text = text;
    }
}

static class Message2 extends SelfDescribingMarshallable {
    long number;

    public Message2(long number) {
        this.number = number;
    }
}

To write to the queue you can call a proxy that implements this interface.要写入队列,您可以调用实现此接口的代理。

SingleChronicleQueue queue1 = ChronicleQueue.singleBuilder(path).build();

MessageListener writer1 = queue1.acquireAppender().methodWriter(MessageListener.class);

// call method on the interface to send messages
writer1.method1(new Message1("hello"));
writer1.method2(new Message2(234));

These calls produce messages which can be dumped as follows.这些调用产生可以如下转储的消息。

# position: 262568, header: 0
--- !!data #binary
method1: {
  text: hello
}
# position: 262597, header: 1
--- !!data #binary
method2: {
  number: !int 234
}

To read the messages, you can provide a reader which calls your implementation with the same calls that you made.要阅读消息,您可以提供一个阅读器,该阅读器使用您所做的相同调用来调用您的实现。

// a proxy which print each method called on it
MessageListener processor = ObjectUtils.printAll(MessageListener.class)
// a queue reader which turns messages into method calls.
MethodReader reader1 = queue1.createTailer().methodReader(processor);

assertTrue(reader1.readOne());
assertTrue(reader1.readOne());
assertFalse(reader1.readOne());

Running this example prints:运行此示例打印:

method1 [!Message1 {
  text: hello
}
]
method2 [!Message2 {
  number: 234
}
]

Nice @PeterLawrey has a different way to build the processor. Nice @PeterLawrey 有一种不同的方式来构建处理器。 I mean in your example you print the objects I want populate the two different types of objects.我的意思是在您的示例中,您打印我想要填充两种不同类型的对象的对象。 I don't find a way until now using the methods in the same listener to do it.直到现在我才找到使用同一个侦听器中的方法来做到这一点的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM