![](/img/trans.png)
[英]How to resolve the dependency org.apache.kafka.clients.producer.Producer in maven project as a java producer for apache kafka
[英]Apache Kafka - Producer break on initTransaction
我正在測試一個基本的交易案例,但它停留在producer.initTransaction();上。 任何配置都可以出錯嗎?
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();
}
嘗試:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.