Kafka Producer请求超时设置

时间:2017-08-18 07:58:29

标签: java apache-kafka

我想设置请求超时,所以添加了request.timeout.ms参数。 当我本能地断开代理连接时,没有发生任何超时错误?

我在此配置中缺少什么?我是否还需要修改服务器设置?

 public void init() {
          LOGGER.info("initializing  KafkaProducer: Topic Name: {}", topic);
          System.out.println("initializing  KafkaProducer: Topic Name: {}");
          Properties properties = new Properties();
          properties.put("bootstrap.servers", brokerList);
          properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
          properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
          properties.put("acks", "1");
          properties.put("retries", "3");
          properties.put("linger.ms", 5);
          properties.put("block.on.buffer.full", false);
          properties.put("request.timeout.ms", "1000");
          //properties.put("metadata.fetch.timeout.ms", 1000);



          producer = new KafkaProducer<>(properties);
       }


        public void produce(String txnLogStr) {
          ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, txnLogStr);
          producer.send(record, new ProducerCallback());   

       }

       private class ProducerCallback implements Callback {
          @Override
          public void onCompletion(RecordMetadata recordMetadata, Exception e) {
              System.out.println("onCompletion recordMetadata:"+recordMetadata.offset()+", e:"+e);
             if (e != null && recordMetadata != null) {
                LOGGER.error("Kafka Queue problem. Topic: {}", topic, e);
                e.printStackTrace();
             }else {
                 System.out.println("No error");
             }
          }
       }

Kafka版本:kafka_2.11-0.10.2.0

的pom.xml

 <dependencies>
    <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.11</artifactId>
            <version>0.10.2.0</version>         

        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.10.2.0</version> 

        </dependency>


  </dependencies>

1 个答案:

答案 0 :(得分:2)

使用以下属性设置

properties.put("metadata.fetch.timeout.ms", "1000");