Java 使用低级Kafka流API执行context.forward()时执行NPE
我已经使用低级卡夫卡API构建了一个普通的卡夫卡流API。拓扑是线性的 p1->p2->p3 在执行context.forward()时,我得到了NPE,这里的代码片段:Java 使用低级Kafka流API执行context.forward()时执行NPE,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我已经使用低级卡夫卡API构建了一个普通的卡夫卡流API。拓扑是线性的 p1->p2->p3 在执行context.forward()时,我得到了NPE,这里的代码片段: NAjava.lang.NullPointerException: null at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:178) at org.ap
NAjava.lang.NullPointerException: null
at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:178)
at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:133)
...
我正在使用卡夫卡流2.3.0
我在这里看到了一个类似的SO问题[1],这个问题基于非常旧的版本。所以,不确定这是否是相同的错误
编辑
我把一些更多的信息,保持我所做的要点
public class SP1Processor implements StreamProcessor {
private StreamProcessingContext ctxt;
// In init(), create a single thread pool
// which does some processing and sends the
// data to next processor
@Override
void init(StreamProcessingContext ctxt) {
this.ctxt = ctxt;
// Create a thread pool, do some work
// and then do this.ctxt.forward(K,V)
// Not showing code of Thread pool
// Strangely, inside this thread pool,
// this.ctxt isn't same what I see in process()
// shouldn't it be same? ctxt is member variable
// and shouldn't it be same
// this.ctxt.forward(K,V) here in this thread pool is causing NPE
// why does it happen?
this.ctxt.forward(K,V);
}
@Override
void process(K,V) {
// Here do some processing and go to the next processor chain
// This works fine
this.ctxt.forward(K,V);
}
}
[1]: https://stackoverflow.com/questions/39067846/periodic-npe-in-kafka-streams-processor-context
看起来它可能与相关问题是同一个问题,尽管我们讨论的是一个更现代的版本。
确保
ProcessorSupplier.get()
每次调用时都返回一个新实例。您不应该在处理器或DSL调用中创建任何线程池。
KafkaStreams中的并行性是通过num.stream.threads
、分区数和实例数来管理的
ctxt
是相同的,但其字段/成员可能不同(例如currentNode
)-可能会被不同的线程更改。ProcessorSupplier.get()
这是仅在拓扑构建期间调用还是在执行context.forward()时调用的?我添加了代码段,pl看一看,让我知道是否有什么不寻常的事情?你能添加代码吗?@BartoszWardziński:我添加了代码片段。在process()中,ctxt.forward()工作正常;然而,在init()中,我生成了一个线程池,在那里,当我执行ctxt.forward()时,它会导致NPE。ctxt本身在process()或线程池中都不是null,但奇怪的是它们不一样