2

有一个非常简单的 poc,比如这个:

    IndexedChronicle chronicle = getChronicle("basic");
    ExcerptAppender appender = chronicle.createAppender();
    appender.startExcerpt();
    appender.writeObject(new MessageKey("type", 123L));
    appender.finish();

    ExcerptTailer tailer = chronicle.createTailer();
    while(tailer.nextIndex()) {
        MessageKey key = (MessageKey) tailer.readObject();
        System.out.println("key " + key);
    }

    VanillaChronicle vcron = getVainllaChronicle("vanilla");
    VanillaAppender app = vcron.createAppender();
    app.startExcerpt();
    app.writeObject(new MessageKey("type", 123L));
    app.finish();

    ExcerptTailer vtail = vcron.createTailer();
    while(vtail.nextIndex()) {
        MessageKey key = (MessageKey) vtail.readObject();
        System.out.println("key " + key);
    }

给我一个IndexOutOfBoundsExceptionwriteObject方法上的VanillaAppender

但是,几乎没有区别,文档中也没有什么特别的不同

谁能建议它应该如何使用?

更新:

我重新安排了代码,使其与 peters 相同(实际上是复制了它),但我仍然得到这个异常:

Exception in thread "main" java.lang.IndexOutOfBoundsException: position is beyond the end of the buffer 372 > -190495716
  at net.openhft.lang.io.NativeBytes.checkEndOfBuffer(NativeBytes.java:518)
  at net.openhft.lang.io.AbstractBytes.writeObject(AbstractBytes.java:1897)
  at main.ChronicleTest.main(ChronicleTest.java:31)

导入的版本是3.2.1

<dependency>
  <groupId>net.openhft</groupId>
  <artifactId>chronicle</artifactId>
  <version>3.2.1</version>
</dependency>
4

1 回答 1

1

When I try this with Chronicle 3.2.1

public class SO25623856Main {
    public static void main(String[] args) throws IOException {
        Chronicle vcron = new VanillaChronicle("vanilla");
        ExcerptAppender app = vcron.createAppender();
        app.startExcerpt();
        app.writeObject(new MessageKey("type", 123L));
        app.finish();

        ExcerptTailer vtail = vcron.createTailer();
        while (vtail.nextIndex()) {
            MessageKey key = (MessageKey) vtail.readObject();
            System.out.println("key " + key);
        }
        vcron.close();
    }
}

class MessageKey implements Serializable {

    private String type;
    private long l;

    public MessageKey(String type, long l) {

        this.type = type;
        this.l = l;
    }

    @Override
    public String toString() {
        return "MessageKey{" +
                "type='" + type + '\'' +
                ", l=" + l +
                '}';
    }
}

it prints

key MessageKey{type='type', l=123}

BTW I suggest you use Externalizable or ByteMarshallable for improved performance and smaller messages.

于 2014-09-04T17:37:58.933 回答