0

I am using DeadLetterPublishingRecoverer to auto send the failed record to DLT. I am trying to send a custom record instead of failed record to DLT. Is it possible to do this. Please help me with the configuration. My DeadLetterPublishingRecoverer config is below.

@Bean
DeadLetterPublishingRecoverer deadLetterPublishingRecoverer(KafkaTemplate<String, byte[]> byteArrayTemplate) {
    return new DeadLetterPublishingRecoverer([
            (byte[].class)                           : byteArrayTemplate],)

}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Lee
  • 1
  • 1

1 Answers1

1

Create a subclass of DeadLetterPublishingRecoverer and override the createProducerRecord() method.

/**
 * Subclasses can override this method to customize the producer record to send to the
 * DLQ. The default implementation simply copies the key and value from the consumer
 * record and adds the headers. The timestamp is not set (the original timestamp is in
 * one of the headers). IMPORTANT: if the partition in the {@link TopicPartition} is
 * less than 0, it must be set to null in the {@link ProducerRecord}.
 * @param record the failed record
 * @param topicPartition the {@link TopicPartition} returned by the destination
 * resolver.
 * @param headers the headers - original record headers plus DLT headers.
 * @param data the value to use instead of the consumer record value.
 * @param isKey true if key deserialization failed.
 * @return the producer record to send.
 * @see KafkaHeaders
 */
protected ProducerRecord<Object, Object> createProducerRecord(ConsumerRecord<?, ?> record,

        TopicPartition topicPartition, Headers headers, @Nullable byte[] data, boolean isKey) {

In the upcoming 2.7 release, this is changed to

/**
 * Subclasses can override this method to customize the producer record to send to the
 * DLQ. The default implementation simply copies the key and value from the consumer
 * record and adds the headers. The timestamp is not set (the original timestamp is in
 * one of the headers). IMPORTANT: if the partition in the {@link TopicPartition} is
 * less than 0, it must be set to null in the {@link ProducerRecord}.
 * @param record the failed record
 * @param topicPartition the {@link TopicPartition} returned by the destination
 * resolver.
 * @param headers the headers - original record headers plus DLT headers.
 * @param key the key to use instead of the consumer record key.
 * @param value the value to use instead of the consumer record value.
 * @return the producer record to send.
 * @see KafkaHeaders
 */
protected ProducerRecord<Object, Object> createProducerRecord(ConsumerRecord<?, ?> record,
        TopicPartition topicPartition, Headers headers, @Nullable byte[] key, @Nullable byte[] value) {
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I tried overriding the method but it cant be invoked because its an internal method – Lee Feb 12 '21 at 15:26
  • I am not familiar with Kotlin `internal` semantics; it seems odd to me if Kotlin won't let you override that method; you may have to use Java for this one class. – Gary Russell Feb 12 '21 at 17:59
  • https://stackoverflow.com/questions/49284094 An answer there hints that you have to use Java. Seems like a severe limitation of Kotlin. – Gary Russell Feb 12 '21 at 18:11