Vitaly Anders
Vitaly Anders

Reputation: 41

How are Camel routes launched manually?

I have a Camel route that is set to run every five minutes

@Component
public class CamelRoute extends RouteBuilder{
    private final String comment = "Cron"

    @Override
    public void setup() {
        from("quartz2://myGroup/myTimerName?cron=0+0/5+12-18+?+*+MON-FRI")
        .log("Processing from"+comment)
        .to("activemq:Totally.Rocks");
    }
}

And I want to force it to run manually, from Spring http request, and change comment field in CamelRoute

@RequestMapping(value = "/ex/foos", method = RequestMethod.GET)
@ResponseBody
public String getFoosBySimplePath() {
     //TODO: Start Camel route
     //change camel log "comment" from "Cron" to "HTTP request"
}

Upvotes: 2

Views: 1549

Answers (2)

Vitaly Anders
Vitaly Anders

Reputation: 41

Solution for my task was easy, although not straightforward if using Camel documentation:

startRoute(String routeId) Starts the given route if it has been previously stopped

I added another route

from("timer://manualRestart?repeatCount=1")
    .routeId("manualRestart")
    .noAutoStartup()
    .to("activemq:Totally.Rocks");

and use startRoute() to launch it when needed

public String getFoosBySimplePath() {
    camelContext.startRoute("manualRestart");
}

Why do I find it "not straightforward"? Because documentation says, that startRoute() can start previously stopped routes. Route was never stopped, it was configured not to start by default.

Upvotes: 2

staszko032
staszko032

Reputation: 862

To run a Camel route manually you can use FluentProducerTemplate. You can autowire an instance like a normal bean.

Examples: 1, 2

To be honest, I am not sure if it will work with quartz endpoints, but I am sure it is working pretty well with "direct:" endpoints. Anyway, it could be a good start for your findings.

Upvotes: 1

Related Questions