Hi, 

Claus Ibsen-2 wrote:
> 
> On Thu, Mar 25, 2010 at 10:39 AM, Tarjei Huse <[email protected]> wrote:
>>
>> Hi,
>>
>> Yesterday C. Ibsen tough me that you cannot start and stop routes when
>> you
>> use JMS connectors, but you can suspend them using a route policy.
>>
> 
> Your problem was that you wanted to stop a JMS listener in the same
> thread it was processing the message.
> So by suspending it you can do it in a more gentle way that the JMS
> listener can comply with.
> 
> You can still stop it from another thread if you like. However using
> suspend is more gentle, and works better,
> especially if you do more start/stop kinda of things.
> 
>> Now, I'm wondering what is the best way to get this done. I usually write
>> my
>> routes so that they start and stop at an JMS endpoint for persistence.
>>
>> Is there a way to find out if a route has been suspended and resume it or
>> should I just add a default routepolicy to each route and use it to
>> suspend
>> and resume routes as well?
>>
> 
> Yeah you should be able to get some details of the state of routes.
> There should be API for that somewhere :)
> 

I ended up writing this helper class:

import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.Consumer;
import org.apache.camel.Route;
import org.apache.camel.Service;
import org.apache.camel.ServiceStatus;
import org.apache.camel.SuspendableService;
import org.apache.camel.util.ServiceHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * If possible, you want to stop a Camel Route by suspending it instead of
halting 
 * it, if the route Consumer supports this.
 * 
 * This simple wrapper around CamelContext provides support for preferring
to 
 * suspend instead of stopping a route.
 * 
 *
 */
public class ServiceSuspender {
        
        protected final static transient Log log =
LogFactory.getLog("RouteStopper");
        private final CamelContext camelContext;
        
        public ServiceSuspender(CamelContext ctx) {
                this.camelContext = ctx;
        }
        /**
         * This helper returns status Stopped even if the route has status 
started,
but 
         * where the consumer is suspended.
         * @param routeId
         * @return ServiceStatus.stopped if the route is stopped or the 
Consumer is
suspended
         */
        public ServiceStatus getStatusForRoute(String routeId) {
                log.info("Getting status for route " + routeId);                
                ServiceStatus currentStatus = 
camelContext.getRouteStatus(routeId);
                if (currentStatus == ServiceStatus.Stopped) {
                        return currentStatus;
                }
                Consumer service = getRoute(routeId).getConsumer();
                if (isSuspendableService(service)) {
            SuspendableService ss = (SuspendableService) service;
            if (ss.isSuspended()) {
                return ServiceStatus.Stopped;
            } else {
                return currentStatus;
            }
                } else {
                        return currentStatus;
                }
        }

        private Route getRoute(String id) {
                List<Route> routes = camelContext.getRoutes();
                for (Route route: routes) {
                        if (route.getId().equals(id)) return route;
                }
                throw new Error("No route registered with name " + id);
        }

        /**
         * Stop or suspends a route, prefering suspend.
         * 
         * 
         * @param id
         * @throws Exception
         */
        public void suspendOrStopRoute(String id ) throws Exception {
                Route route = getRoute(id);
                Consumer consumer = route.getConsumer();
                if (isSuspendableService(consumer)) {
                        boolean suspended = 
ServiceHelper.suspendService(consumer);
                if (suspended ) {
                    log.info("Suspended consumer " + consumer );
                }
                } else {
                        camelContext.stopRoute(id);
                }
                
        }

        /**
         * Starts or resumes a route depending on it's status.
         * 
         * Expects the route to have been stopped by suspendOrStopRoute
         * @param id
         * @throws Exception
         */
        public void resumeRoute(String id) throws Exception {
                
                ServiceStatus currentStatus = camelContext.getRouteStatus(id);
                if (currentStatus.isStopped()) {
                        camelContext.startRoute(id);
                        log.info("Route was completely stopped. Starting 
route.");
                        return;
                }
                /* route may be suspended. */
                Consumer consumer = getRoute(id).getConsumer();
                if (isSuspendableService(consumer) ) { 
                        boolean resumed = ServiceHelper.resumeService(consumer);
                        if (resumed) { 
                                log.info("Route " + id + " resumed");
                        }
                }
                        
        }
        
        private boolean isSuspendableService(Consumer consumer) {
                return consumer instanceof SuspendableService;
        }       
}

Tarjei
-- 
View this message in context: 
http://old.nabble.com/Starting-and-Suspending-routes-tp28026500p28071674.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to