|
| 1 | +package uk.gov.companieshouse.orders.api.kafka; |
| 2 | + |
| 3 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 4 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 5 | +import org.apache.kafka.clients.producer.RecordMetadata; |
| 6 | +import uk.gov.companieshouse.kafka.message.Message; |
| 7 | +import uk.gov.companieshouse.kafka.producer.ProducerConfig; |
| 8 | +import uk.gov.companieshouse.kafka.producer.factory.KafkaProducerFactory; |
| 9 | + |
| 10 | +import java.util.Properties; |
| 11 | +import java.util.concurrent.ExecutionException; |
| 12 | +import java.util.concurrent.Future; |
| 13 | + |
| 14 | +public class OrdersApiKafkaProducer { |
| 15 | + |
| 16 | + private KafkaProducer<String, byte[]> kafkaProducer; |
| 17 | + |
| 18 | + /** |
| 19 | + * Instantiate the necessary configuration for producing and a producer for Kafka. |
| 20 | + * The {@link KafkaProducerFactory} will be used to create the producer. |
| 21 | + * |
| 22 | + * @param config Configuration for the producer |
| 23 | + */ |
| 24 | + public OrdersApiKafkaProducer(ProducerConfig config) { |
| 25 | + this(config, new KafkaProducerFactory()); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Instantiate the necessary configuration for producing and a producer for Kafka. |
| 30 | + * |
| 31 | + * @param config Configuration for the producer |
| 32 | + * @param producerFactory Factory to create the producer |
| 33 | + */ |
| 34 | + public OrdersApiKafkaProducer(ProducerConfig config, KafkaProducerFactory producerFactory) { |
| 35 | + Properties props = new Properties(); |
| 36 | + |
| 37 | + props.put("bootstrap.servers", String.join(",", config.getBrokerAddresses())); |
| 38 | + props.put("acks", config.getAcks().getCode()); |
| 39 | + props.put("key.serializer", config.getKeySerializer()); |
| 40 | + props.put("value.serializer", config.getValueSerializer()); |
| 41 | + props.put("retries", config.getRetries()); |
| 42 | + props.put("max.block.ms", config.getMaxBlockMilliseconds()); |
| 43 | + props.put("request.timeout.ms", config.getRequestTimeoutMilliseconds()); |
| 44 | + props.put("enable.idempotence", false); |
| 45 | + |
| 46 | + if (config.isRoundRobinPartitioner()) { |
| 47 | + props.put("partition.assignment.strategy", "roundrobin"); |
| 48 | + } |
| 49 | + |
| 50 | + kafkaProducer = producerFactory.getProducer(props); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Send a message to a topic in Kafka. |
| 55 | + * |
| 56 | + * The data in the message is sent to Kafka in a byte array |
| 57 | + * to avoid unrecognised encoding characters which can occur |
| 58 | + * when Strings are used with Kafka. |
| 59 | + * |
| 60 | + */ |
| 61 | + public void send(Message msg) throws ExecutionException, InterruptedException { |
| 62 | + |
| 63 | + ProducerRecord<String, byte[]> record = getProducerRecordFromMessage(msg); |
| 64 | + |
| 65 | + kafkaProducer.send(record).get(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Send a message to a topic in Kafka, and return a {@link Future < RecordMetadata >} for processing manually. |
| 70 | + * |
| 71 | + * The data in the message is sent to Kafka in a byte array |
| 72 | + * to avoid unrecognised encoding characters which can occur |
| 73 | + * when Strings are used with Kafka. |
| 74 | + * |
| 75 | + */ |
| 76 | + public Future<RecordMetadata> sendAndReturnFuture(Message msg) { |
| 77 | + |
| 78 | + ProducerRecord<String, byte[]> record = getProducerRecordFromMessage(msg); |
| 79 | + |
| 80 | + return kafkaProducer.send(record); |
| 81 | + } |
| 82 | + |
| 83 | + private ProducerRecord<String, byte[]> getProducerRecordFromMessage(Message msg) { |
| 84 | + |
| 85 | + return new ProducerRecord<>( |
| 86 | + msg.getTopic(), |
| 87 | + msg.getPartition(), |
| 88 | + msg.getTimestamp(), |
| 89 | + msg.getKey(), |
| 90 | + msg.getValue() |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public void close() { |
| 95 | + kafkaProducer.close(); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments