首页 > 解决方案 > Chronicle Queue 使用零分配从队列读取/写入

问题描述

我是编年史队列的新用户,我想使用零分配策略来读取和写入编年史队列中的对象。我想使用队列和像 pojo 类这样的字节的可编组实现是正确的策略吗?我没有找到任何可以帮助我的东西。我尝试在下面做一个消息类的实例来追加和读取队列中的内容。当我尝试使用多个线程阅读时,我总是会出现内存不足错误。

public class Message implements BytesMarshallable{
    private byte[] text;
    private long timeStamp;
    
    public Message(){}

    //Getters and Setters
}

当我使用多个线程出现内存不足错误时,我尝试使用 tailer.readDocument 读取

标签: javaqueuebytechroniclechronicle-queue

解决方案


使用byte[]要求您在每次大小更改时分配一个新的。您无法更改 a 的大小byte[]

相反,我建议使用Bytes可以调整大小而不会产生太多垃圾的 out 类(仅当它增长时)

package run.chronicle.queue;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.wire.BytesInBinaryMarshallable;
import net.openhft.chronicle.wire.LongConversion;
import net.openhft.chronicle.wire.MilliTimestampLongConverter;

public class Message extends BytesInBinaryMarshallable {
    private final Bytes text = Bytes.allocateElasticOnHeap();

    @LongConversion(MilliTimestampLongConverter.class)
    private long timeStamp;

    //Getters and Setters


    public Bytes getText() {
        return text;
    }

    public void setText(CharSequence text) {
        this.text.clear().append(text);
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }
}

请参阅此示例https://github.com/OpenHFT/Chronicle-Queue-Demo/tree/master/messages-with-text

当使用小堆运行时,-Xmx128m -XX:NewSize=96m -verbose:gc MessageMain您可以看到数百万条消息不会触发任何收集。

Read 10,000,000 of 10,000,000 messages in 7.990 seconds
Read 10,000,000 of 10,000,000 messages in 6.907 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-438402668456/metadata.cq4t
Read 10,000,000 of 10,000,000 messages in 6.836 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-445239126125/metadata.cq4t
Read 10,000,000 of 10,000,000 messages in 6.883 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 3 ms to add mapping for test-452122895277/metadata.cq4t
Read 10,000,000 of 10,000,000 messages in 7.013 seconds
Read 10,000,000 of 10,000,000 messages in 6.838 seconds
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 2 ms to add mapping for test-465974753213/metadata.cq4t

推荐阅读