简体   繁体   English

使用 Kafka 处理 Spring Cloud Stream 中的 NetworkException

[英]Handle NetworkException in Spring Cloud Stream with Kafka

I've the following Listener - Producer in Spring Cloud Stream:我有以下监听器 - Spring Cloud Stream 中的生产者:

@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);
        }
    }

}

the piece of code works but, sometimes the message fail to be sent because of the following error:这段代码有效,但有时由于以下错误而无法发送消息:

[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.

also if the send variable is true.如果发送变量为真。

How can I handle the NetworkException error in Spring Cloud Stream?如何处理 Spring Cloud Stream 中的 NetworkException 错误?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM