Camel Java DSL: Update the next polling request param using the value from the response

393 views Asked by At

I am new to Apache camel, this is what I am trying to figure out. In a sample code below, I am trying to use the property - "value" in the request param in next polling request.

String valueFromTheResponse= ""
m.addRouteBuilder(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("timer://foo?period=2)
            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
            .setHeader("Accept", constant("application/json"))
            .to("https4://" + <myrequestURL>?param=<valueFromTheResponse>)
            .marshal().json(JsonLibrary.Jackson)
            .setProperty("value", jsonpath("$.value"))
            .process(new Processor() {
                @Override
                public void process(final Exchange exchange) throws Exception {
                    valueFromTheResponse = (String) exchange.getProperty("value");
                }
            })
        }
    });
    m.run();

What would be the best way to achieve this? or assign the class level variable the property value?

UPDATE: SOLUTION got it working by adding the following:

.process(new Processor() {
                @Override
                public void process(final Exchange exchange) throws Exception {
                    exchange.getIn().setHeader("CamelHttpQuery", buildParamQuery());
                }
            })
1

There are 1 answers

2
Claus Ibsen On

You would need to store the value in a shared field in for example the RouteBuilder class itself, or a shared class instance. And then in the to http endpoint uri, you need to set the param query as a message header instead where you can get that value via a method call.

.setHeader(Exchange.HTTP_QUERY, method(this, "buildParamQuery"))

And then have a method

public String buildParamQuery() {
  return "param=" + sharedValue;
}

And then you set this field from the inlined processor with the last value. And mind about the initial value, eg the first poll the value is null so you need to maybe to return an empty string/null from the buildParamQuery method or something else.