xmlParser
xmlParser

Reputation: 2031

How to get headers from one route to another route - Camel JavaDsl

I have camel rest endpoint with two params and when I send request it activates first route ("direct:amq"), where I get message from activeMq.

The headers here are okay, but this route activates another route ("direct:post)" and the headers there are missing.

I want to get the urlToPost Header from the first route in to the second.

 rest("/getFromActiveMq").produces("application/json")
    .get()
    .param()
    .name("urlToPost")
    .type(RestParamType.query)
    .dataType("String")
    .endParam()
    .param()
    .name("getactivemq")
    .type(RestParamType.query)
    .dataType("String")
    .endParam()
    .to("direct:amq");

from("direct:amq").streamCaching()
    .startupOrder(2)
    .log("My activemq is " + "${in.header.getactivemq}")
    .log("My urlToPost is " + "${in.header.urlToPost}")
    .setHeader("myHeader")
    .header("${in.header.urlToPost}")
    .log("My urlToPost Changed header is " + "${header.myHeader}")
    .process(exchange -> {
      String header = exchange.getIn().getHeader("urlToPost", String.class);
      System.out.println(header);
      exchange.getIn().setHeader("myShittyHeader", header);

      Map<String, Object> hdr = exchange.getIn()
          .getHeaders();
      for (Map.Entry<String, Object> entry : hdr.entrySet()) {
        System.out.println(entry.getKey() + "/" + entry.getValue());
      }
    })
    .pollEnrich()
    .simple("activemq://${in.header.getactivemq}")
    .onCompletion()
    .log("My body is : " + "${body}")
    .to("direct:post");

from("direct:post").tracing()
    .process(exchange -> exchange.getIn()
        .setBody(exchange.getIn()
            .getBody()))
    .convertBodyTo(String.class)
    .process(exchange -> {
      Map<String, Object> hdr = exchange.getIn()
          .getHeaders();
      for (Map.Entry<String, Object> entry : hdr.entrySet()) {
        System.out.println(entry.getKey() + "/" + entry.getValue());
      }
    })
    .log("My urlToPost BEFORE SETTING HEADERS is " + "${in.header.urlToPost}")
    .setHeader("Content-Type", constant("application/json"))
    .setHeader("Accept", constant("application/json"))
    .setHeader(Exchange.HTTP_METHOD, constant("POST"))
    .log("My urlToPost AFTER SETTING HEADERS is " + "${in.header.urlToPost}")
    // .log("My HTTP_URI is: " + "${in.header.urlToPost}")
    // .to("http4://urlToPost")
    // .to("direct:nothing");
    .enrich()
    .simple("http4://urlToPost");

I found that after:

.pollEnrich() .simple("activemq://${in.header.getactivemq}")

Headers are gone

Upvotes: 2

Views: 4160

Answers (1)

burki
burki

Reputation: 7005

The pollEnrich merges your current Exchange with another message. That means it is in effect an Aggregator.

If you don't provide an aggregation strategy, Camel uses by default a simple body aggregation. This is the reason why you lose your headers.

You have to configure a pre-existing or implement your own aggregation strategy that respects the headers of one or both messages during aggregation.

Upvotes: 4

Related Questions