diff --git a/spring-integration-kafka/src/main/java/org/springframework/integration/kafka/outbound/KafkaProducerMessageHandler.java b/spring-integration-kafka/src/main/java/org/springframework/integration/kafka/outbound/KafkaProducerMessageHandler.java index 38a8609857..075ab51703 100644 --- a/spring-integration-kafka/src/main/java/org/springframework/integration/kafka/outbound/KafkaProducerMessageHandler.java +++ b/spring-integration-kafka/src/main/java/org/springframework/integration/kafka/outbound/KafkaProducerMessageHandler.java @@ -133,6 +133,10 @@ public class KafkaProducerMessageHandler extends AbstractReplyProducingMes private Type replyPayloadType = Object.class; + private ProducerRecordCreator producerRecordCreator = + (message, topic, partition, timestamp, key, value, headers) -> + new ProducerRecord<>(topic, partition, timestamp, key, value, headers); + public KafkaProducerMessageHandler(final KafkaTemplate kafkaTemplate) { Assert.notNull(kafkaTemplate, "kafkaTemplate cannot be null"); this.kafkaTemplate = kafkaTemplate; @@ -307,6 +311,16 @@ public class KafkaProducerMessageHandler extends AbstractReplyProducingMes this.replyPayloadType = payloadType; } + /** + * Set a {@link ProducerRecordCreator} to create the {@link ProducerRecord}. + * @param producerRecordCreator the creator. + * @since 3.2.1 + */ + public void setProducerRecordCreator(ProducerRecordCreator producerRecordCreator) { + Assert.notNull(producerRecordCreator, "'producerRecordCreator' cannot be null"); + this.producerRecordCreator = producerRecordCreator; + } + @Override public String getComponentType() { return this.isGateway ? "kafka:outbound-gateway" : "kafka:outbound-channel-adapter"; @@ -391,8 +405,8 @@ public class KafkaProducerMessageHandler extends AbstractReplyProducingMes headers = new RecordHeaders(); this.headerMapper.fromHeaders(messageHeaders, headers); } - final ProducerRecord producerRecord = new ProducerRecord<>(topic, partitionId, timestamp, - (K) messageKey, payload, headers); + final ProducerRecord producerRecord = this.producerRecordCreator.create(message, topic, partitionId, + timestamp, (K) messageKey, payload, headers); ListenableFuture> sendFuture; RequestReplyFuture gatewayFuture = null; if (this.isGateway) { @@ -584,4 +598,33 @@ public class KafkaProducerMessageHandler extends AbstractReplyProducingMes } + /** + * Creates a {@link ProducerRecord} from a {@link Message} and/or properties + * derived from configuration and/or the message. + * + * @param the key type. + * @param the value type. + * + * @since 3.2.1 + * + */ + @FunctionalInterface + public interface ProducerRecordCreator { + + /** + * Create a record. + * @param message the outbound message. + * @param topic the topic. + * @param partition the partition. + * @param timestamp the timestamp. + * @param key the key. + * @param value the value. + * @param headers the headers. + * @return the record. + */ + ProducerRecord create(Message message, String topic, Integer partition, Long timestamp, K key, V value, + Headers headers); + + } + }