Skip to content

clone ProducerFactory PostProcessors for customFactories in KafkaTemplate #1983

@thst71

Description

@thst71

**Affects Version(s):**current main

🎁 Enhancement

instrumentations attached to ProducerFactory instances are destroyed if the user adds the configOverrides parameter. This behavior is unexpected for the developer, for example it will undo sleuth instrumentation that is otherwise kept if the new KafkaTemplate(Map) constructor would be used.

/* produces an instrumented ProducerFactory, PostProcessors are added, eg for sleuth */
@Bean
ProducerFactory<K,T> producerFactory(Map<String,Object>config) {
     return new DefaultKafkaProducerFactory<K,T>(config);
}

@Bean
KafkaTemplate<K,T>kafkaTemplate(ProducerFactory<K,T>producerFactory) {
    KafkaTemplate<K,T>instrumented = new KafkaTemplate(producerFactory);

    // this constructor removes all PostProcessors because of the added config properties
    KafkaTemplate<K,T>notInstrumented = new KafkaTemplate(producerFactory, Collections.singletonMap(ProducerConfig.CLIENT_ID, "new-client"));
  
    return instrumented;
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions