【问题标题】:Setting up Apache camel route with MDC logging enabled在启用 MDC 日志记录的情况下设置 Apache 骆驼路由
【发布时间】:2022-11-11 14:25:13
【问题描述】:
我有以下路线。
@Override
public void configure() throws Exception {
onException(Exception.class).process(outboxExceptionProcessor).handled(true);
from("seda:outbox-channel")
.routeId("route-outbox")
.process(outboxHeaderEnricherProcessor)
.choice()
.when(header("type").isEqualTo("DealInvitationEvent"))
.multicast()
.parallelProcessing()
.process(invitationEventProcessor)
.end()
.endChoice()
.when(header("type").isEqualTo("EnquiryAcceptedEvent"))
.multicast()
.parallelProcessing()
.process(enquiryAcceptedEventProcessor)
.end()
.endChoice()
.when(header("type").isEqualTo("EnquiryDeclinedEvent"))
.multicast()
.parallelProcessing()
.process(enquiryDeclinedEventProcessor)
.end()
.endChoice()
.otherwise()
.process(outboxNotMatchingProcessor)
.end()
.process(outboxCompletionProcessor);
}
以下是向该路由器发送消息的代码。
producerTemplate.sendBodyAndHeaders(
"seda:outbox-channel", outboxEvent.getId(), headers);
我在 application.yml 中启用了 MDC 日志记录
camel:
springboot:
use-mdc-logging: true
mdc-logging-keys-pattern: "*"
即使每当我记录消息时,我都没有将值设置为 MDC。我已经相应地更新了 logback 配置,并在其他地方更新了它的工作文件,但是在路由丢失之后
【问题讨论】:
标签:
spring-boot
apache-camel
spring-camel
【解决方案1】:
为了他人的利益,我设法按如下方式实现了这一点。
@Override
public void configure() throws Exception {
onException(Exception.class).process(outboxExceptionProcessor).handled(true);
getContext()
.getExtension(ExtendedCamelContext.class)
.setUnitOfWorkFactory(new CustomMDCUnitOfWorkFactory());
from("seda:outbox-channel")
.routeId("route-outbox")
.process(outboxHeaderEnricherProcessor)
.choice()
.when(header("type").isEqualTo("DealInvitationEvent"))
.multicast()
.parallelProcessing()
.process(invitationEventProcessor)
.end()
.endChoice()
.when(header("type").isEqualTo("EnquiryAcceptedEvent"))
.multicast()
.parallelProcessing()
.process(enquiryAcceptedEventProcessor)
.end()
.endChoice()
.when(header("type").isEqualTo("EnquiryDeclinedEvent"))
.multicast()
.parallelProcessing()
.process(enquiryDeclinedEventProcessor)
.end()
.endChoice()
.otherwise()
.process(outboxNotMatchingProcessor)
.end()
.process(outboxCompletionProcessor);
}
以下是 CustomMDCUnitOfWorkFactory 实现
public class CustomMDCUnitOfWorkFactory extends DefaultUnitOfWorkFactory {
private InflightRepository inflightRepository;
private boolean usedMDCLogging;
private String mdcLoggingKeysPattern;
private boolean allowUseOriginalMessage;
private boolean useBreadcrumb;
@Override
public UnitOfWork createUnitOfWork(Exchange exchange) {
return new CustomMDCUnitOfWork(
exchange,
this.inflightRepository,
this.mdcLoggingKeysPattern,
this.allowUseOriginalMessage,
this.useBreadcrumb);
}
public void afterPropertiesConfigured(CamelContext camelContext) {
this.inflightRepository = camelContext.getInflightRepository();
this.usedMDCLogging =
camelContext.isUseMDCLogging() != null && camelContext.isUseMDCLogging();
this.mdcLoggingKeysPattern = camelContext.getMDCLoggingKeysPattern();
this.allowUseOriginalMessage =
camelContext.isAllowUseOriginalMessage() != null
? camelContext.isAllowUseOriginalMessage()
: false;
this.useBreadcrumb =
camelContext.isUseBreadcrumb() != null ? camelContext.isUseBreadcrumb() : false;
}
}