Apache Camel MDC 从 Body 添加字段 [英] Apache Camel MDC add field from Body

查看:24
本文介绍了Apache Camel MDC 从 Body 添加字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 apache camel,并想使用 MDC 将某些键添加到我的日志中.我浏览了官方 Camel MDC Logging 文档,这非常棒.我可以毫不费力地记录我的 routeId.我还需要从 Camel's Body 添加一个字段.

I am working with apache camel and would like to add certain keys to my logs using MDC. I went through the official Camel MDC Logging documentation which is pretty great. I am able to log my routeId's without much effort. I also need to add a field from Camel's Body.

最坏的情况我可以在所有路由中手动添加它,但我想知道是否可以以更简单的方式将字段从 body 添加到 MDC?

Worst case scenario I can add this manually in all routes, but I was wondering if its possible to add fields from body to MDC in a easier fashion?

任何想法都值得赞赏.我真的很想能够做到这一点,而不必进入每条路线并添加一个班轮.

Any ideas are appreciated. I would really like to be able to do this without having to go into every route and adding a one liner.

更新:

在我的项目中实现了自定义 MDCUnitOfWork 和 Factory.我能够看到 CustomUnitOfWorkFactory 创建我的 CustomUnitOfWork 然后设置 MDC 值.

Implemented a custom MDCUnitOfWork and Factory in my project. I am able to see the CustomUnitOfWorkFactory creating my CustomUnitOfWork which is then setting the MDC values.

但是我注意到这只发生在路线的起点.

However I noticed this only happens in the beginning of the route.

在我的用例中,我将轮询 Amazon SQS 作为我的第一条路线.我这里没有所需的信息.在第一条路线中,我构建了我的 Context 并将其设置为 Camel body,这是我需要在 MDC 中设置的信息所在的位置.

In my use case, I am Polling an Amazon SQS as my first route. I do not have the required information here. In the first route I build my Context and set that to Camel body which is where my information that I need to set in MDC resides.

是否也可以在第二条路线之前创建 UnitOfWork?

Is it possible to create UnitOfWork before second route as well?

推荐答案

您可以配置自定义 UnitOfWorkFactory 以创建自定义 UnitOfWork 扩展 MDCUnitOfWork,您可以在其中向 MDC 添加自定义信息.

You can configure a custom UnitOfWorkFactory to create a custom UnitOfWork that extends the MDCUnitOfWork, where you can add custom information to MDC.

您可以从 Java 或 XML 中在 CamelContext 上配置 UnitOfWorkFactory,只需添加一个 <bean> 和 Camel 检测并使用它

You can configure the UnitOfWorkFactory on CamelContext from Java or in XML just add a <bean> and Camel detects and uses it

这篇关于Apache Camel MDC 从 Body 添加字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆