greyp9 commented on code in PR #8463: URL: https://github.com/apache/nifi/pull/8463#discussion_r1538306358
########## nifi-nar-bundles/nifi-kafka-bundle/nifi-kafka-3-service/src/main/java/org/apache/nifi/kafka/service/producer/Kafka3ProducerService.java: ########## @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.kafka.service.producer; + +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.Producer; +import org.apache.kafka.common.PartitionInfo; +import org.apache.kafka.common.serialization.ByteArraySerializer; +import org.apache.nifi.kafka.service.api.common.PartitionState; +import org.apache.nifi.kafka.service.api.common.ServiceConfiguration; +import org.apache.nifi.kafka.service.api.producer.FlowFileResult; +import org.apache.nifi.kafka.service.api.producer.KafkaProducerService; +import org.apache.nifi.kafka.service.api.producer.ProducerConfiguration; +import org.apache.nifi.kafka.service.api.producer.PublishContext; +import org.apache.nifi.kafka.service.api.producer.RecordSummary; +import org.apache.nifi.kafka.service.api.record.KafkaRecord; +import org.apache.nifi.kafka.service.producer.txn.KafkaNonTransactionalProducerWrapper; +import org.apache.nifi.kafka.service.producer.txn.KafkaProducerWrapper; +import org.apache.nifi.kafka.service.producer.txn.KafkaTransactionalProducerWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; +import java.util.Properties; +import java.util.stream.Collectors; + +public class Kafka3ProducerService implements KafkaProducerService { + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final Producer<byte[], byte[]> producer; + private final List<ProducerCallback> callbacks; + + private final ServiceConfiguration serviceConfiguration; + + private final KafkaProducerWrapper wrapper; + + public Kafka3ProducerService(final Properties properties, + final ServiceConfiguration serviceConfiguration, + final ProducerConfiguration producerConfiguration) { + final ByteArraySerializer serializer = new ByteArraySerializer(); + this.producer = new KafkaProducer<>(properties, serializer, serializer); + this.callbacks = new ArrayList<>(); + + this.serviceConfiguration = serviceConfiguration; + + this.wrapper = producerConfiguration.getUseTransactions() + ? new KafkaTransactionalProducerWrapper(producer) + : new KafkaNonTransactionalProducerWrapper(producer); + } + + @Override + public void close() { + producer.close(); + } + + @Override + public void init() { + wrapper.init(); + logger.trace("init()"); Review Comment: This particular one helped me work out a problem with controller service lifecycle, but now uneeded; I will remove it. -- 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]
