lafual
lafual

Reputation: 775

Spring Integration DSL EnrichedHeaders not being replaced

Using Boot 2.2.6 and SI 5.2.5 I am attempting to enrich the header with an Object. However on the second pass of the flow the new header does contain a new message UUID (so I know it is a new Header), but the enriched header is not being replaced with a new Object, but contains the previous Object.

For example;

@Configuration
public class MyFlow {

  public static class Original {
    String original;
    UUID   uuid;

    public Original() { uuid = UUID.randomUUID(); }
    public void setOriginal(String s) { original = s; }
    public String getOriginal() { return original; }
  }

  @Bean IntegrationFlow doIt() {
    return IntegrationFlows
      .from("somewhere")
      .enrichHeaders(h -> h.header("ORIGINAL", new Original()))
      .handle((p, h) -> {
        System.err.println(h); // --1
        Original original = (Original) h.get("ORIGINAL");
        original.setOriginal(p.toString());
        System.err.println(h); // --2
      })
      .channel("next")
      .get();
  }
}

On the first pass;

-- 1 original.uuid = new UUID, original.original = null

-- 2 original.uuid = same as before, original.original = new payload

On the second pass;

-- 1 original.uuid = same as before, original.original = same as before

-- 2 original.uuid = same as before, original.original = new payload

I want Original to be a new object. I assume that definition of class Original or enricherHeaders is incorrect. Where am I going wrong?

Upvotes: 0

Views: 456

Answers (1)

Gary Russell
Gary Russell

Reputation: 174584

Headers are not overwritten by default.

h -> h.defaultOverwrite(true).header("ORIGINAL", new Original())

or

h -> h.header("ORIGINAL", new Original(), true);

You would usually use the first with multiple headers.

Upvotes: 1

Related Questions