pvillard31 commented on code in PR #10001:
URL: https://github.com/apache/nifi/pull/10001#discussion_r2142191165
##########
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:
Thoughts on the below? Makes the code a bit easier for a case that should be
quite rare - hopefully.
```suggestion
try {
this.wrapper = producerConfiguration.getTransactionsEnabled()
? new KafkaTransactionalProducerWrapper(producer)
: new KafkaNonTransactionalProducerWrapper(producer);
} catch (final Exception e) {
try {
close();
} catch (final Exception closeEx) {
e.addSuppressed(closeEx);
}
throw new ProcessException("Failed to initialize Kafka
Producer", e);
}
```
##########
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) {
Review Comment:
```suggestion
} catch (final Exception e) {
```
--
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]