2

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");
xmlParser
  • 1,903
  • 4
  • 19
  • 48

3 Answers3

1

Try this without context.start() ....

   CamelContext camelContext = new DefaultCamelContext();
    ProducerTemplate template = camelContext.createProducerTemplate();

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

    Message out = exchange.getOut();   
DCO
  • 1,222
  • 12
  • 24
  • Yes i tested it with curl, swagger, chrome, postman and etc... And its giving ok response. – xmlParser Mar 22 '19 at 13:04
  • Try removing context.start(); Here was someone with the same problem: https://stackoverflow.com/questions/12306211/not-getting-http-response-body-when-using-camel-producertemplate-and-http – DCO Mar 22 '19 at 13:14
  • I have changed my method as yours and now it gives Message[]... I think its same. – xmlParser Mar 22 '19 at 13:56
  • weird. maybe set exchange.getIn().setHeader(Exchange.HTTP_QUERY, ""); in process method? – DCO Mar 22 '19 at 13:59
  • Still the same... Maybe the message is empty because the response is Array from JsonObjects? – xmlParser Mar 22 '19 at 14:07
  • Now I`ve got: Response from http template is org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@7e1ffe70 – xmlParser Mar 22 '19 at 14:10
  • 1
    Found this one. Maybe you are right https://stackoverflow.com/questions/51018993/accessing-data-from-json-body-inside-apache-camel – DCO Mar 22 '19 at 14:11
  • I saw this but the answer is to create pojo class like the object in the response and in my case that is not a solution. What If I need to make 20 different routes to 20 different endpoints, then I will need to create 20 pojos... – xmlParser Mar 22 '19 at 14:13
  • Maybe use map for serialization? Found this one https://stackoverflow.com/questions/13085921/unmarshal-json-to-map-list-of-strings – DCO Mar 22 '19 at 14:21
  • I could not implement unmarshall in that route. I have made it in from->unmarshal->to (route) which is second in my example and still nothing.. – xmlParser Mar 22 '19 at 14:31
  • Its not that, I have tested it with another endpoint which gives response String and its same. Response from http template is org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@6fa2448b – xmlParser Mar 22 '19 at 14:54
1

The http components are streaming based, so you can ask Camel to give you the response as string instead.

String s = exchange.getMessage().getBody(String.class);

See more in these links

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
1

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.

Bambus
  • 1,493
  • 1
  • 15
  • 32