0

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());
                }
            })
fenrigne123
  • 593
  • 1
  • 4
  • 9

1 Answers1

0

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.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • method(this, "buildParamQuery") gave me a NPE, BUT I was able to use your suggested way by doing this .setHeader(Exchange.HTTP_QUERY, simple(buildParamQuery())). Looks like the problem is still the same, buildParamQuery() is only called once and not in the next polling request. Do you have any suggestions? – fenrigne123 Feb 24 '19 at 19:01
  • I somehow need to update the CamelHttpQuery for the next poll. – fenrigne123 Feb 24 '19 at 19:28