2

We use Apache Camel in Talend ESB Studio v6.4

In an ESB route, we consume JMS messages, process them then send them to an HTTP server. But that target server is down for maintainance every saturday from 6pm to 10pm.

How can we "pause" message consuming or message processing during that period ? I think quartz only works with file/ftp endpoints. We could use a Processor component to check in Java if we are in the down period, but what to do after that ?

Loko
  • 120
  • 9

1 Answers1

2

There are several ways to do this. One camel specific way to do it is through CamelControlBus. It takes in a routeId and performs an action (start/stop/resume etc) on it - Read more here to get an understanding Camel ControlBus

However, there is another approach that you can take. You can create a POJO bean that has 3 methods

  • shouldRouteStop() : to check the current time and decide if it should stop your route.
  • startRoute() : Starts a route if it is suspended
  • stopRoute() : Suspends a route if it is started

A simple implementation can be as follows:

public class ManagementBean {
 public boolean shouldRouteStop() {
    // Mocking the decision here
    return new Random().nextBoolean();
 }
 public void startRoute(org.apache.camel.CamelContext ctx) throws Exception {
    if (ctx.getRouteStatus("GenerateInvoices") == ServiceStatus.Suspended)
        // replace the argument with your route Id
        ctx.resumeRoute("GenerateInvoices");
 }
 public void stopRoute(org.apache.camel.CamelContext ctx) throws Exception {
    if (ctx.getRouteStatus("GenerateInvoices") == ServiceStatus.Started)
        // replace the argument with your route Id
        ctx.suspendRoute("GenerateInvoices");
 }
}

Make sure that the jms-route that you wish to control has a routeId and add this bean to your base/default CamelContext like this

main.bind("manageRouteBean", new ManagementBean());

Create another timer based route, that checks on each tick, if the route should be stopped or not and then suspends or resumes the route by routeId. This route can be implemented like below:

public class MonitoringRoute extends RouteBuilder {
 @Override
 public void configure() throws Exception {
    onException(Exception.class).log(exceptionMessage().toString());

    from("timer:time?period=10000")
            .choice()
            .when().simple("${bean:manageRouteBean?method=shouldRouteStop}")
            .log("Route Should Stop")
            .bean(ManagementBean.class, "stopRoute(*)")
            .otherwise()
            .log("Route Should Start")
            .bean(ManagementBean.class, "startRoute(*)")
            .end();
 }
}

Note that startRoute and stopRoute take the argument as *. This is camel way of automatically binding parameters based on type.

Finally, you can add this route to the main camel context like : main.addRouteBuilder(new MonitoringRoute());

For a complete implementation, have a look at this github repo

Prakhar
  • 1,065
  • 3
  • 16
  • 30