首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Kafka处理Spring Cloud Stream中的NetworkException

用Kafka处理Spring Cloud Stream中的NetworkException
EN

Stack Overflow用户
提问于 2020-03-02 20:37:02
回答 1查看 399关注 0票数 0

我在Spring Cloud Stream中有以下Listener - Producer:

代码语言:javascript
复制
@StreamListener(target = MultipleProcessor.DOTCONN_INPUT, condition= "headers['kafka_receivedTopic']=='dotconnectorissues'")
public void inputDot(Message<DotConnectorIssue> messageIn) {
    DotConnectorIssue data = messageIn.getPayload();
    ObjectMapper mapper = new ObjectMapper();
    DotConnectorUpdateDto dataMapped = new DotConnectorUpdateDto(data);
    if (dataMapped.getPlantCode().equals(plantCode)) {
        log.info("incoming dotConnectorIssue " + data);
        try {
            Message<String> messageOut = MessageBuilder
                    .withPayload(mapper.writeValueAsString(dataMapped))
                    .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                    .setHeader("type", "dotconnectorissue")
                    .build();

            boolean send = ehProcessor.outputAndon().send(messageOut, 15000L);
            log.info("sent message: "+ send);
            if (!send) messagePool.getPool().add(messageOut);
        } catch (JsonProcessingException e) {
            log.error("error during creating json", e);
        }
    }

}

这段代码可以工作,但有时消息会因为以下错误而无法发送:

代码语言:javascript
复制
[kafka-producer-network-thread | producer-2] ERROR o.s.k.s.LoggingProducerListener.onError - 
Exception thrown when sending a message with key='null' and payload='{123, 34, 116, 121, 112, 101, 34, 58, 34, 85, 80, 68, 65, 84, 69, 95, 68, 79, 84, 67, 79, 78, 78, 69...' to topic andon: 

org.apache.kafka.common.errors.NetworkException: The server disconnected before a response was received.

另外,如果send变量为true。

如何处理Spring Cloud Stream中的NetworkException错误?

EN

回答 1

Stack Overflow用户

发布于 2020-03-02 23:03:42

设置sync property to true并捕获异常,或者,要异步获取错误,请设置生产者属性errorChannelEnabled

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60489017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档