将值注入自定义的DeadLetterPublishingRecoverer

huangapple go评论54阅读模式
英文:

Inject values into custom DeadLetterPublishingRecoverer

问题

抱歉,以下是翻译的内容:

我有一个自定义的死信恢复器,我已经实现了它,以便我可以重写createProducer方法

protected ProducerRecord<Object, Object> createProducerRecord(ConsumerRecord<?, ?> record,
        TopicPartition topicPartition, Headers headers, @Nullable byte[] key, @Nullable byte[] value)

这是因为我的DLT需要与源不同的模式。我需要注入来自我的应用程序环境特定(开发、审查、测试、生产)的 spring @Values 来获取值,以创建这个新模式并将其传输到DLT。

import org.springframework.beans.factory.annotation.Value

public class MyDeadLetterPublishingRecoverer extends DeadLetterPublishingRecoverer {
    @Value("${spring.kafka.custom.myValue}")
    private String myValue;

    public MyDeadLetterPublishingRecoverer (
    KafkaOperations<?, ?> template,
    BiFunction<ConsumerRecord<?, ?>, Exception, TopicPartition> destinationResolver) {
        super(template, destinationResolver);
    }
}

然而,myValue 总是 null,因为 DeadLetterPublishingRecoverer 不是组件或类似的构造类型,因此没有 spring 上下文来启用变量的解析。如果将 MyDeadLetterPublishingRecoverer 添加为上下文中的一个构造类型,那么构造函数将抱怨需要一个 bean,以便可以自动装配目的地解析器(无论如何,我并不希望这样做)

"无法自动装配。未找到 'BiFunction<ConsumerRecord, Exception, TopicPartition>' 类型的 bean"

因此,我正在寻找一种方法,要么让 @Value 注解在自定义的 DeadLetterPublishingRecoverer 中工作,要么找到另一种方法从 application.yml 中提取值以在自定义的 DeadLetterPublishingRecoverer 中使用。

英文:

I have a custom dead letter recoverer which I've implemented so that I could override the createProducer method

protected ProducerRecord&lt;Object, Object&gt; createProducerRecord(ConsumerRecord&lt;?, ?&gt; record,
			TopicPartition topicPartition, Headers headers, @Nullable byte[] key, @Nullable byte[] value) 

This is needed because my DLT needs a different schema from the source. I need to inject spring @Values from my application environment specific ( dev, review, test, prod) yamls to get values to create this new schema and produce to the DLT.

 import org.springframework.beans.factory.annotation.Value

public class MyDeadLetterPublishingRecoverer extends DeadLetterPublishingRecoverer
{
   @Value(&quot;${spring.kafka.custom.myValue}&quot;)
    private String myValue;

   public MyDeadLetterPublishingRecoverer (
    KafkaOperations&lt;?, ?&gt; template,
    BiFunction&lt;ConsumerRecord&lt;?, ?&gt;, Exception, TopicPartition&gt; destinationResolver) {
  super(template, destinationResolver);
}

}

However, myValue is always null because the DeadLetterPublishingRecoverer is not a component or similar stereotype so there is no spring context to enable the resolution of the variable. If one of the stereotypes is added to MyDeadLetterPublishingRecoverer then the constructor complains about needing a bean so that the desitination resolver can be autowired ( which I don't want anyway)
"Could not autowire. No beans of 'BiFunction<ConsumerRecord<?, ?>, Exception, TopicPartition>' type found"

So, I'm looking for either a way to get the @value annotation to work in the custom DeadLetterPublishingRecoverer or an alternate way to pull the values from application.yml to use in the custom DeadLetterPublishingRecoverer

答案1

得分: 0

只需将recoverer定义为@Bean,或者将其注释为@Component,Spring 将负责所有的连线。

<strike>如果您使用的是Spring Boot,只要bean存在就足够了,Boot 将其连接到容器工厂(如果您使用的是Boot的自动配置工厂)。</strike>

如果您没有使用Boot,则必须自己在容器工厂中设置它。

更正:Boot 将自动配置一个包含recoverer的CommonErrorHandler,而不是recoverer本身。

如果您不需要目标解析器,请实现更简单的构造函数(只接受KafkaOperations - 模板)。

英文:

Just define the recoverer as a @Bean, or annotate it as a @Component, and Spring will take care of all the wiring.

<strike>If you are using Spring Boot, just the presence of the bean is enough, Boot will wire it into the container factory (if you are using Boot's auto configured factory).</strike>

If you are not using boot, you will have to set in on the container factory yourself.

Correction: Boot will auto configure a CommonErrorHandler that contains the recoverer, not the recoverer itself.

If you don't need a destination resolver, implement the simpler constructor instead (the one that just takes a KafkaOperations - template).

huangapple
  • 本文由 发表于 2023年2月16日 05:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定