我有一个 kafka 生产者定义如下
public KafkaMessageProducer(String kafkaHost, String kafkaPort, Map<String, String> map) {
this.kafkaTopic = map;
final Properties properties = new Properties();
properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
properties.put("bootstrap.servers", kafkaHost + ":" + kafkaPort);
producer = new KafkaProducer<String, String>(properties);
}
我正在使用以下代码发送消息。(也尝试使用回调)。
public void sendMessage(String topic, RestCommonResource resultToken) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.valueToTree(resultToken);
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, jsonNode.toString());
producer.send(record);
}
但是如果 kafka 服务器关闭并且生产者发布消息,程序将陷入无限循环,并出现以下异常:
WARN [2018-09-13 06:27:59,589] org.apache.kafka.common.network.Selector: Error in I/O with localhost/127.0.0.1
! java.net.ConnectException: Connection refused: no further information
! at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.7.0_80]
! at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:744) ~[na:1.7.0_80]
! at org.apache.kafka.common.network.Selector.poll(Selector.java:238) ~[kafka-clients-0.8.2.1.jar:na]
! at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:192) [kafka-clients-0.8.2.1.jar:na]
! at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:191) [kafka-clients-0.8.2.1.jar:na]
! at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:122) [kafka-clients-0.8.2.1.jar:na]
! at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]
如果有任何属性可以设置为停止重试并删除消息。
眼眸繁星
幕布斯6054654
相关分类