turcsanyip commented on code in PR #10001:
URL: https://github.com/apache/nifi/pull/10001#discussion_r2142678343
##########
nifi-extension-bundles/nifi-kafka-bundle/nifi-kafka-service-shared/src/main/java/org/apache/nifi/kafka/service/producer/Kafka3ProducerService.java:
##########
@@ -53,14 +54,32 @@ public Kafka3ProducerService(final Properties properties,
final ServiceConfiguration
serviceConfiguration,
final ProducerConfiguration
producerConfiguration) {
final ByteArraySerializer serializer = new ByteArraySerializer();
- this.producer = new KafkaProducer<>(properties, serializer,
serializer);
+
+ try {
+ this.producer = new KafkaProducer<>(properties, serializer,
serializer);
+ } catch (Exception e) {
+ throw new ProcessException("Failed to create Kafka Producer", e);
+ }
+
this.callbacks = new ArrayList<>();
this.serviceConfiguration = serviceConfiguration;
- this.wrapper = producerConfiguration.getTransactionsEnabled()
- ? new KafkaTransactionalProducerWrapper(producer)
- : new KafkaNonTransactionalProducerWrapper(producer);
+ try {
+ this.wrapper = producerConfiguration.getTransactionsEnabled()
+ ? new KafkaTransactionalProducerWrapper(producer)
+ : new KafkaNonTransactionalProducerWrapper(producer);
+ } catch (Exception e) {
+ boolean success;
+ try {
+ close();
+ success = true;
+ } catch (Exception e2) {
+ success = false;
+ }
+ throw new ProcessException(String.format("Failed to initialize
Kafka Producer (%s)",
+ success ? "closed producer cleanly after failure" : "also
failed to close producer after failure"), e);
+ }
Review Comment:
Using `addSuppressed()` makes sense. Thanks for the hint!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]