简体   繁体   中英

Apache Kafka - Producer break on initTransaction

I'm testing an elementary transaction case, but it stays on producer.initTransaction(); Can any configuration be wrong?

public static void main(String[] argv) throws Exception {
    String topicName = "helloworldpartitioned";
    // Configure the Producer
    Properties configProperties = new Properties();
    configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProperties.put(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG, "1000");
    configProperties.put(ProducerConfig.CLIENT_ID_CONFIG, "test");
//      configProperties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, Boolean.TRUE);
    configProperties.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, PRODUCER_TRANSACTIONAL_ID);

    KafkaProducer<String, String> producer = new KafkaProducer<String, String>(configProperties);
    producer.initTransactions();
    producer.beginTransaction();
        String line = "TestMessage";
        System.out.println("Inizializzo la transazione");
        ProducerRecord<String, String> rec = new ProducerRecord<String, String>(topicName, line);
        producer.send(rec);
    // Thread.sleep(5000);
    producer.commitTransaction();
    producer.flush();
    producer.close();
}

Try to:

producer.initTransactions();
producer.beginTransaction();
    String line = "TestMessage";
    System.out.println("Inizializzo la transazione");
    ProducerRecord<String, String> rec = new ProducerRecord<String, String>(topicName, line);
    producer.send(rec);


// Thread.sleep(5000);
producer.commitTransaction();
producer.flush();
producer.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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