刷新0个未完成的偏移补偿消息

时间:2019-03-14 05:45:10

标签: multithreading java.util.concurrent apache-kafka-connect paho java-threads

我正在为Kafka编写自己的Mqtt连接器。我知道Confluent有Mqtt连接器,但它不是开源的,所以我决定编写自己的连接器,我在向Kafka发送消息方面遇到问题。我可以收到Mqtt消息,但无法发送到Kafka。基本上,当消息从Mqtt Broker messageArrived方法触发时,我将消息添加到BlockingQueue中,如下所示:

private final BlockingQueue<SourceRecord> queue= new LinkedBlockingQueue<>();

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
    log.info("Message arrived : " + message.toString());
    queue.add(new SourceRecord(null, null, kafkaTopic, null,
            Schema.STRING_SCHEMA, message.getId(),
            Schema.BYTES_SCHEMA, message.getPayload()));
    }

这是我的poll方法:

@Override
public List<SourceRecord> poll() throws InterruptedException {
    List<SourceRecord> records = new ArrayList<SourceRecord>();
    records.add(queue.take());
    return records;
}

因此,当我调试此代码时,控制台重复输出如下:

[2019-03-13 17:45:37,113] INFO WorkerSourceTask{id=mqtt-connector-0} flushing 0 outstanding messages for offset commit (org.apache.kafka.connect.runtime.WorkerSourceTask)
[2019-03-13 17:45:40,385] INFO Message arrived : {"timestamp":1552488339305,"values":[{"id":"Simulation.Simulator.Temperature","v":1,"q":true,"t":1552488336813}]} (deneme.MqttSourceTask)
[2019-03-13 17:45:45,386] INFO Message arrived : {"timestamp":1552488344305,"values":[{"id":"Simulation.Simulator.Temperature","v":2,"q":true,"t":1552488341809}]} (deneme.MqttSourceTask)
[2019-03-13 17:45:48,204] INFO WorkerSourceTask{id=mqtt-connector-0} Committing offsets (org.apache.kafka.connect.runtime.WorkerSourceTask)

似乎Mqtt消息来了,但无法发送Kafka。这两种方法可以通过多个线程异步工作。

当我同时使用queue.hashCode()poll方法将messageArrived写到控制台时,哈希码是不同的。它的作用类似于单独的队列对象。我不明白为什么会这样。是什么导致此问题?

谁能告诉我出什么问题了?

0 个答案:

没有答案