Search code examples
spring-bootapache-camelspring-camel

How to trigger camel route from client request?


I have this route:

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");

When it is in @override configure() it is working on app. startup. But what I want to do is to call (trigger) that route from Spring Boot RestController.

I have GET endpoint and I want when I call it, the route to do it stuff.


Solution

  • You can replace the 'from' clause with something like:

    from("direct:runGetAllUsers")
    

    then in your Spring Rest controller you can invoke the Camel route. There is an example in the Camel In Action 2 book with source here.