Spring集成-放置在header中的对象返回字符串而不是对象类型



我已经用Java DSL编写了一个集成流程

我正在丰富消息头以包含AtomicInteger:

.enrichHeaders(t -> t.headerFunction(pollTime,message -> new AtomicInteger()))

如果我把一个断点在随后的句柄方法在同一流,我可以看到头,它是一个字符串,而不是一个AtomicInteger。

所以如果我试图在另一个流中检索它,就像这样我会得到一个非法参数异常:

message.getHeaders().get(pollTime,AtomicInteger.class).getAndAdd(delay);

Caused by: java.lang.IllegalArgumentException: Incorrect type specified for header 'pollTime'. Expected [class java.util.concurrent.atomic.AtomicInteger] but actual type is [class java.lang.String]

如果我在Kotlin DSL中做同样的事情,它就会正常工作:

enrichHeaders {
headerFunction<Any>(pollCount) {
AtomicInteger()
}
}

有人知道我做错了什么吗?

我创建了一个独立的项目来重现错误,并将其作为预期的AtomicInteger添加到header中。

然后我调试了我们的主应用程序,结果发现有一个OpenTracingChannelInterceptor,它将所有头重写为字符串。

这个库是罪魁祸首:

io.opentracing.contrib:opentracing-spring-messaging:0.0.5io.opentracing.contrib:opentracing-spring-cloud-starter-jaeger的传递依赖关系

看起来添加这个库只会破坏Spring集成。

修复方法是排除跟踪自动配置:

@SpringBootApplication(exclude = {OpenTracingChannelInterceptorAutoConfiguration.class})

更新:

opentracing库现在的维护时间更长了,所以长期的解决方案是迁移到一个不同的跟踪库,希望不会有相同类型的问题。

https://www.cncf.io/blog/2022/01/31/cncf-archives-the-opentracing-project/

最新更新