Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kafka Consumer-Java实现_Java_Spring_Apache Kafka - Fatal编程技术网

Kafka Consumer-Java实现

Kafka Consumer-Java实现,java,spring,apache-kafka,Java,Spring,Apache Kafka,我在Java中实现KafkaConsumer时遇到了这个问题。下面列出的代码读取流并处理消息。此代码启动后是否会继续侦听传入消息?如果是这样的话,它如何保持运行并继续使用这些消息 public void run(int a_numThreads) { Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, new Intege

我在Java中实现KafkaConsumer时遇到了这个问题。下面列出的代码读取流并处理消息。此代码启动后是否会继续侦听传入消息?如果是这样的话,它如何保持运行并继续使用这些消息

public void run(int a_numThreads) {
    Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
    topicCountMap.put(topic, new Integer(a_numThreads));
    Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
    List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);

    // now launch all the threads
    //
    executor = Executors.newFixedThreadPool(a_numThreads);

    // now create an object to consume the messages
    //
    int threadNumber = 0;
    for (final KafkaStream stream : streams) {
        executor.submit(new ConsumerTest(stream, threadNumber));
        threadNumber++;
    }
}
public void运行(int a_numThreads){
Map topicCountMap=新HashMap();
put(主题,新整数(a_numThreads));
Map consumerMap=consumer.createMessageStreams(topicCountMap);
列表流=consumerMap.get(主题);
//现在启动所有线程
//
executor=Executors.newFixedThreadPool(a_numThreads);
//现在创建一个对象来使用消息
//
int threadNumber=0;
对于(最终卡夫卡斯特雷姆流:流){
提交(新的ConsumerTest(流、线程号));
threadNumber++;
}
}

一旦启动,代码将继续侦听传入消息,因为它使用了(线程池中的线程数=
a_numThreads
)并且线程池中的每个线程都执行一个使用者(
ConsumerTest

如果仔细观察
ConsumerTest
类,您会发现有一个无限循环。
it.hasNext()
正在阻塞(请参阅),因此消费者将始终等待下一条消息:

public void run() {
        ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
        while (it.hasNext())
            System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));
        System.out.println("Shutting down Thread: " + m_threadNumber);
}
public void run(){
ConsumerIterator it=m_stream.iterator();
while(it.hasNext())
System.out.println(“Thread”+m_threadNumber+”:“+新字符串(it.next().message());
System.out.println(“关闭线程:+m_threadNumber”);
}

也就是说,可以使用属性
consumer.timeout.ms
设置超时,如果在指定的时间间隔后没有消息可供使用,则
会向consumer抛出超时异常,但默认值为-1(无超时),因此默认情况下,消费者会一直使用消息,直到消费者和执行者关闭为止(请参见示例中的
shutdown
方法)。

我想知道他们为什么决定将其称为卡夫卡。卡夫卡起源于捷克语。卡夫卡的意思是“像一只鸟”。可能是一只“鸽子”,它将消息从源传递到目标;)真有趣!我会说一点(很少)捷克语,但我不知道,我的字典里也没有。谢谢你,简!迭代器是Kafka提供的一个实现,它会导致程序阻塞并等待下一条消息吗?确切地说,迭代器是在Kafka中实现的,并由java BlockingQueue支持。“一个迭代器,在从提供的队列中读取值之前一直阻塞。迭代器接受一个shutdownCommand对象,该对象可以添加到队列中以触发关闭”()