Search code examples
springspring-bootapache-camelspring-camel

Spring Boot Camel Route - get data from rest endpoint


I want to create camel route in Spring Boot (2.1.1) project to get the data from some (rest) endpoint (http://localhost:8080/getAllUsers) and to send that data to activeMq.

I have tried with timer data to send it on activeMq and to consume it and it is working. But I have problem with collecting data from endpoint.

I have tried several things but no success. This is what I have tried.

In this example I am not sending the data to ActiveMq, I just want to see the response...

public void createNewRoute() {
CamelContext context = new DefaultCamelContext();

try {
  ProducerTemplate template = context.createProducerTemplate();
  context.start();

  Exchange exchange = template.request("http://localhost:8080/getAllUsers",
      new Processor() {
        public void process(Exchange exchange) throws Exception {
        }
      });

  if (null != exchange) {
    Message out = exchange.getOut();
    int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
    System.out.println("Response: " + String.valueOf(responseCode));
  }

  Thread.sleep(1000 * 3);
  context.stop();
} catch (Exception ex) {
  System.out.println("Exception: " + ex);
}

System.out.println("DONE!!");
 }

Another route:

 from("servlet://localhost:8080/getAllUsers").to("activemq://all-users");

And another:

 rest("//localhost:8080/getAllUsers")
 .get().consumes("application/json")
 .to("activemq://all-users");

Solution

  • I will go with your second example:

    from("timer://test?repeatCount=1").routeId("newRoute")
        .streamCaching()
        .process(exchange -> exchange.getIn()
            .setBody(exchange.getIn()
                .getBody()))
        .marshal()
        .json(JsonLibrary.Jackson)
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
        .to("http://localhost:8080/getAllUsers")
        .log(LoggingLevel.INFO, "This is my body: ${body}")
        .to("activemq:queue://new-queue");
    

    This will trigger it once.