Spring Integration enrichHeader with randomUUID

半腔热情 提交于 2020-01-03 20:04:47

问题


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>

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();

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?

Thanks for your help.


回答1:


Yes, that's correct.

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

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

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

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

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

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

or even better like:

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


来源:https://stackoverflow.com/questions/33292265/spring-integration-enrichheader-with-randomuuid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!