如果Kafka服务器停机,Kafka spring integeration如何hanlde

时间:2016-03-13 09:40:32

标签: spring-integration apache-kafka

我有一个Web应用程序,它通过从请求中读取一些值来发送kafka消息。如果我的kafka服务器关闭,如何发送错误状态。现在在服务器停机的情况下,产品不断尝试连接并无限地记录下面的错误。

错误:

14:56:10.181 [kafka-producer-network-thread | producer-1] WARN  o.a.kafka.common.network.Selector - Error in I/O with localhost/127.0.0.1
java.net.ConnectException: Connection refused

生产者配置是:

<int:channel id="inputToKafka">
    <int:queue/>
</int:channel>

<int-kafka:outbound-channel-adapter
        id="kafkaOutboundChannelAdapter"
        auto-startup="true"
        kafka-producer-context-ref="kafkaProducerContext"
        channel="inputToKafka"  >
    <int:poller fixed-delay="1000" time-unit="MILLISECONDS" receive-timeout="10" task-executor="taskExecutor"/>
</int-kafka:outbound-channel-adapter>

<task:executor id="taskExecutor" pool-size="5" keep-alive="120" queue-capacity="500"/>

<int-kafka:producer-context id="kafkaProducerContext">
    <int-kafka:producer-configurations>
        <int-kafka:producer-configuration broker-list="localhost:9092"
                                          key-class-type="java.lang.String"
                                          value-class-type="java.lang.String"
                                          sync="true"
                                          send-timeout="10"
                                          topic="test"
                                          key-encoder="kafkaEncoder"
                                          value-encoder="kafkaEncoder"
                                          compression-type="none"/>
    </int-kafka:producer-configurations>
</int-kafka:producer-context>
<bean id="kafkaEncoder" class="org.springframework.integration.kafka.serializer.common.StringEncoder">
</bean>
<bean id="valueEncoder" class="org.springframework.integration.kafka.serializer.avro.AvroReflectDatumBackedKafkaEncoder">
    <constructor-arg value="com.fastretailing.catalogPlatformSCMProducer.model.ProducerMessage" />
</bean>

发送代码为:

 ProducerMessage k = list.get(0);
    boolean status = false;
    try {
        status = inputToKafka.send(
                MessageBuilder.withPayload(k.getJsonString(k))
                        .setHeader(KafkaHeaders.MESSAGE_KEY, k.getFeedName())  // Note: the header was `messageKey` in earlier versions
                        .setHeader(KafkaHeaders.TOPIC, "test")       // Note: the header was `topic` in earlier versions
                        .build()
        );
    } catch (Exception e) {
        System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
        System.out.println(e);
    }
    System.out.println(status);
    if (!status){
        System.out.println("errrrrrrrrrrrrrrrrrrrrrrrr");
    }

我总是将状态视为真实。即使kafka服务器已关闭。而上述连接拒绝错误不断出现。

1 个答案:

答案 0 :(得分:0)

<queue/>删除轮询器和inputToKafka元素。

所有这些状态都说明该消息已在通道ok中加入队列。

使其成为DirectChannel(通过删除队列)意味着适配器发送将在调用线程上运行。