Kwebble
Kwebble

Reputation: 2075

Apache Camel example to get data from HTTP source does not get

To retrieve some open data from a remote web server to process, I'm trying out Apache Camel.

The problem is that it seems that the data is never received. I have tried the jetty, ahc and cxf components but can't get it to work. For example like this:

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class CamelHttpDemo {
  public static void main(final String... args) {
    final CamelContext context = new DefaultCamelContext();

      try {
        context.addRoutes(new RouteBuilder() {
          @Override
          public void configure() throws Exception {
            this.from("direct:start")
                .to("ahc:http://camel.apache.org/")
                .process(exchange -> {
                  System.out.println(exchange);
                });
          }
      });

      context.start();
      Thread.sleep(10000);
      context.stop();
    } catch (final Exception e) {
      e.printStackTrace();
    }
  }
}

No output is written so the line System.out.println(exchange); is never executed and I assume the data is not retrieved.

I'm using the most recent version of Apache Camel, 2.17.1.

Upvotes: 1

Views: 466

Answers (1)

Andrew Lygin
Andrew Lygin

Reputation: 6197

You need some message producer in your route to emit Exchange that would trigger the http component. Your route starts with direct:start which cannot emit new Exchanges, it just sits and waits for someone to initiate the process.

The easiest way to make your route work is to replace direct:start with some producer. For instance, replacing it with this timer .from("timer://foo?fixedRate=true&period=10000") will trigger your http-request once every 10 seconds.

If you want to initiate the request manually, you need to create a ProducerTemplate and use it to send a message to direct:start. That would be:

ProducerTemplate template = context.createProducerTemplate();
template.sendMessage("direct:start", "Message body");

Upvotes: 1

Related Questions