带有随机UUID的Spring集成丰富头 [英] Spring Integration enrichHeader with randomUUID

查看:32
本文介绍了带有随机UUID的Spring集成丰富头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Integration 的新手,使用 Java 7,在迁移到 Spring 集成 DSL 之前我有这个 xml 配置,我的应用程序正在使用从随机 UUID 生成的监视 ID 来丰富标头(这是为了关联请求和响应以便以后在日志中搜索,也许这可以用不同的方式完成,不知道):

I'm a newbie with Spring Integration, using Java 7, and I had this xml configuration before moving to Spring integration DSL, and my application was working to enrich a header with a monitoring ID generated from random UUID (This was to correlate request and response for later searching in logs, maybe this could be done in a different way, don't know):

<int:chain input-channel="requestChannel" output-channel="responseChannel">     
    <int:header-enricher>
        <int:header name="translator-monitoringId" expression="T(java.util.UUID).randomUUID()"/>
    </int:header-enricher>
    <int:transformer ref="customHeaderTransformerBean" method="convertToJson"/>     
    <int-amqp:outbound-gateway      
        exchange-name="translatorExchange"      
        amqp-template="amqpTemplate"        
        routing-key-expression ="headers['translatorRoutingKey']"       
        mapped-request-headers="translator-*"       
        mapped-reply-headers="translator-*"/>       
</int:chain>

所以,在转向 DSL 之后,我有了这个:

So, after moving to DSL, I have this:

return IntegrationFlows
    .from("requestChannel")
    .enrichHeaders(new Consumer<HeaderEnricherSpec>() {
        @Override
        public void accept(HeaderEnricherSpec t) {
            t.header(Constants.MONITORING_ID, UUID.randomUUID());
        }
    }) 
    .transform(customToJsonTransformer())
    .handle(Amqp
        .outboundGateway(rabbitTemplate())
        .exchangeName(TRANSLATOR_EXCHANGE_NAME)
        .routingKeyExpression(
            "headers['" + Constants.TRANSLATOR_ROUTING_KEY + "']")
            .mappedReplyHeaders(Constants.AMQP_CUSTOM_HEADER_FIELD_NAME_MATCH_PATTERN)
            .mappedRequestHeaders(Constants.AMQP_CUSTOM_HEADER_FIELD_NAME_MATCH_PATTERN))
        .route(new ResponseFromTranslatorRouterSI(jsonResponseMessageChannel(), exceptionResponseMessageChannel())).get();

好吧,问题是随机 UUID 包含在标头中作为 monitoringId,但在第一次执行后它保持不变,它不会像以前那样随着每个请求而改变.

Well, the thing is that random UUID is included in headers as monitoringId, but after the first execution it remains the same, it does not change with each request as was before.

你知道我是否遗漏了什么吗?

Do you know if I am missing something?

感谢您的帮助.

推荐答案

是的,没错.

让我们再看看你的代码:

Let's take a look to your code one more time:

public void accept(HeaderEnricherSpec t) {
      t.header(Constants.MONITORING_ID, UUID.randomUUID());
}

那么,您的 UUID.randomUUID() 何时会被评估?是的,就在 accept() 方法调用期间.因此只有一次.

So, when your UUID.randomUUID() will be evaluated? Right, just during accept() method invocation. Therefore only once.

使用您的 XML 变体,您可以处理 expression,它真正为每条消息求值.

With your XML variant you deal with expression which really evaluates for each message.

要使其在 Java DSL 风格中工作,您应该做类似的事情:

To make it working in Java DSL style you should do something similar:

 t.headerExpression(Constants.MONITORING_ID, "T(java.util.UUID).randomUUID()");

或者甚至更好:

t.headerFunction(Constants.MONITORING_ID, 
     new Function<Message<Object>, Object>() {
           Object apply(Message<Object> message) {
               return UUID.randomUUID();
           }
     }
);

这篇关于带有随机UUID的Spring集成丰富头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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