001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.model.RouteDefinition;
021import org.apache.camel.spi.RoutePolicy;
022import org.apache.camel.spi.RoutePolicyFactory;
023import org.apache.camel.util.EndpointHelper;
024
025/**
026 * {@link org.apache.camel.spi.RoutePolicyFactory} which executes for a duration and then triggers an action.
027 * <p/>
028 * This can be used to stop a set of routes (or CamelContext) after it has processed a number of messages, or has been running for N seconds.
029 */
030public class DurationRoutePolicyFactory implements RoutePolicyFactory {
031
032    private String fromRouteId;
033    private int maxMessages;
034    private int maxSeconds;
035    private DurationRoutePolicy.Action action = DurationRoutePolicy.Action.STOP_ROUTE;
036
037    @Override
038    public RoutePolicy createRoutePolicy(CamelContext camelContext, String routeId, RouteDefinition route) {
039        DurationRoutePolicy policy = null;
040
041        if (fromRouteId == null || EndpointHelper.matchPattern(routeId, fromRouteId)) {
042            policy = new DurationRoutePolicy(camelContext, routeId);
043            policy.setMaxMessages(maxMessages);
044            policy.setMaxSeconds(maxSeconds);
045            policy.setAction(action);
046        }
047
048        return policy;
049    }
050
051    public String getFromRouteId() {
052        return fromRouteId;
053    }
054
055    /**
056     * Limit the route policy to the route which matches this pattern
057     *
058     * @see EndpointHelper#matchPattern(String, String)
059     */
060    public void setFromRouteId(String fromRouteId) {
061        this.fromRouteId = fromRouteId;
062    }
063
064    /**
065     * Maximum number of messages to process before the action is triggered
066     */
067    public void setMaxMessages(int maxMessages) {
068        this.maxMessages = maxMessages;
069    }
070
071    public int getMaxSeconds() {
072        return maxSeconds;
073    }
074
075    /**
076     * Maximum seconds Camel is running before the action is triggered
077     */
078    public void setMaxSeconds(int maxSeconds) {
079        this.maxSeconds = maxSeconds;
080    }
081
082    public DurationRoutePolicy.Action getAction() {
083        return action;
084    }
085
086    /**
087     * What action to perform when maximum is triggered.
088     */
089    public void setAction(DurationRoutePolicy.Action action) {
090        this.action = action;
091    }
092
093}