SpringCloud 处理非反序列化异常
对于Kafka Streams活页夹中的常规错误处理,最终用户应用程序要处理应用程序级错误。作为为反序列化异常处理程序提供DLQ的副作用,Kafka Streams绑定器提供了一种访问直接从应用程序发送bean的DLQ的方法。一旦可以访问该bean,就可以以编程方式将任何异常记录从应用程序发送到DLQ。
使用高级DSL仍然难以进行鲁棒的错误处理。Kafka Streams本身还不支持错误处理。
但是,当您在应用程序中使用低级处理器API时,有一些选项可以控制此行为。见下文。
@Autowired
private SendToDlqAndContinue dlqHandler;
@StreamListener("input")
@SendTo("output")
public KStream<?, WordCount> process(KStream<Object, String> input) {
input.process(() -> new Processor() {
ProcessorContext context;
@Override
public void init(ProcessorContext context) {
this.context = context;
}
@Override
public void process(Object o, Object o2) {
try {
.....
.....
}
catch(Exception e) {
//explicitly provide the kafka topic corresponding to the input binding as the first argument.
//DLQ handler will correctly map to the dlq topic from the actual incoming destination.
dlqHandler.sendToDlq("topic-name", (byte[]) o1, (byte[]) o2, context.partition());
}
}
.....
.....
});
}