编年史如何增加处理器?

时间:2018-07-16 11:22:13

标签: chronicle chronicle-queue

我正在使用编年史队列5.16.8

我收到此警告

net.openhft.chronicle.threads.Pauser     : Using Pauser.sleepy() as not enough processors, have 1, needs 8+

是否可以增加此处理器?

源代码

我这里的方法是使用编年史地图来存储索引阅读器。我想,我可以在recordHistory中启用相同的行为...

我不得不使用杰克逊·杰森(Jackson Json)转换的...我没有得到如何使用writeDocument方法的方法。

虽然为true循环,但我在这里遇到的另一件不好的事情……我不知道如何找到队列中的最后一个索引。

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.queue.ChronicleQueue;
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.ExcerptTailer;
import net.openhft.chronicle.queue.RollCycles;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.LinkedList;
import java.util.List;



@Service
public class QueueService {

    public static final String INDEX = "index";

    private ObjectMapper mapper = new ObjectMapper(); // json converter

    @PostConstruct
    public void init() {
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

    public void write(List dtos, String path) throws Exception {

        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.DAILY).build()) {
            final ExcerptAppender appender = queue.acquireAppender();

            for (int i=0; i<dtos.size(); i++) {
                appender.writeText(mapper.writeValueAsString(dtos.get(i)));
            }
        }
    }

    public void write(Object dto, String path) throws Exception {

        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).rollCycle(RollCycles.DAILY).build()) {
            final ExcerptAppender appender = queue.acquireAppender();
            appender.writeText(mapper.writeValueAsString(dto));
        }
    }


    public List readList(String path, Class aClass) throws Exception {

        List dtoList = new LinkedList<>();

        try (ChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).build()) {

            final ExcerptTailer tailer = queue.createTailer();

            ChronicleMap<String, Long> indexMap = getReaderIndexMap(queue.fileAbsolutePath());

            if (indexMap.containsKey(INDEX)) {
                tailer.moveToIndex(indexMap.get(INDEX));
            }

            while (true) { // something smart ?

                String json = tailer.readText();
                if (json == null) {
                    break;
                } else {
                    dtoList.add(mapper.readValue(json, aClass));
                }
            }

            indexMap.put(INDEX, tailer.index());
            indexMap.close();
        }
        return dtoList;
    }

    public ChronicleMap<String, Long> getReaderIndexMap(String queueName) throws Exception {

        ChronicleMapBuilder<String, Long> indexReaderMap = ChronicleMapBuilder.of(String.class, Long.class)
                .name("index-reader-map")
                .averageKey(INDEX)
                .entries(1);

        ChronicleMap<String, Long> map = indexReaderMap.createPersistedTo(new File(queueName+"/reader.idx"));
        return map;
    }
}

1 个答案:

答案 0 :(得分:1)

这基于Java认为您拥有的可用处理器数量。

如果您有虚拟机,则可以将主机配置为具有更多CPU。

如果您有一台物理计算机,则可以将处理器更改为具有更多内核的处理器。

或者您可以忽略该警告。

仅暂停使用一个CPU可能不是一个好主意,因为它将占用您所有的CPU。

注意:即使是为了开发,我们通常建议至少拥有4个内核。