jcastaneyra jcastaneyra - 2 months ago 22
Java Question

Spring Integration enrichHeader with randomUUID

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 name="translator-monitoringId" expression="T(java.util.UUID).randomUUID()"/>
<int:transformer ref="customHeaderTransformerBean" method="convertToJson"/>
routing-key-expression ="headers['translatorRoutingKey']"

So, after moving to DSL, I have this:

return IntegrationFlows
.enrichHeaders(new Consumer<HeaderEnricherSpec>() {
public void accept(HeaderEnricherSpec t) {
t.header(Constants.MONITORING_ID, UUID.randomUUID());
"headers['" + Constants.TRANSLATOR_ROUTING_KEY + "']")
.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.


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:

     new Function<Message<Object>, Object>() {
           Object apply(Message<Object> message) {
               return UUID.randomUUID();