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     */
017    package org.apache.camel.routepolicy.quartz;
018    
019    import java.util.concurrent.TimeUnit;
020    
021    import org.apache.camel.Route;
022    import org.apache.camel.component.quartz.QuartzComponent;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.quartz.CronTrigger;
026    import org.quartz.Trigger;
027    
028    public class CronScheduledRoutePolicy extends ScheduledRoutePolicy implements ScheduledRoutePolicyConstants {
029        private static final transient Log LOG = LogFactory.getLog(CronScheduledRoutePolicy.class);
030        private String routeStartTime;
031        private String routeStopTime;
032        private String routeSuspendTime;
033        private String routeResumeTime;
034        
035        public void onInit(Route route) {   
036            try {       
037                QuartzComponent quartz = route.getRouteContext().getCamelContext().getComponent("quartz", QuartzComponent.class);
038                setScheduler(quartz.getScheduler());
039                
040                if (getRouteStopGracePeriod() == 0) {
041                    setRouteStopGracePeriod(10000);
042                }
043                
044                if (getTimeUnit() == null) {
045                    setTimeUnit(TimeUnit.MILLISECONDS);
046                }
047    
048                if ((getRouteStartTime() == null) && (getRouteStopTime() == null) && (getRouteSuspendTime() == null) && (getRouteResumeTime() == null)) {
049                    if (LOG.isWarnEnabled()) {
050                        LOG.warn("Scheduled Route Policy for route " + route.getId() + " is not set since the no start, stop and/or suspend times are specified");
051                    }
052                    return;
053                }
054            
055                if (scheduledRouteDetails == null) {
056                    scheduledRouteDetails = new ScheduledRouteDetails();
057                    scheduledRouteDetails.setRoute(route);
058                    
059                    if (getRouteStartTime() != null) {
060                        scheduleRoute(Action.START); 
061                    }
062    
063                    if (getRouteStopTime() != null) {
064                        scheduleRoute(Action.STOP);
065                    }
066                    
067                    if (getRouteSuspendTime() != null) {
068                        scheduleRoute(Action.SUSPEND);
069                    }
070                    if (getRouteResumeTime() != null) {
071                        scheduleRoute(Action.RESUME);
072                    }
073                }
074    
075                getScheduler().start();
076            } catch (Exception e) {
077                handleException(e);
078            }        
079        }   
080    
081        @Override
082        protected void doStop() throws Exception {
083            if (scheduledRouteDetails.getStartJobDetail() != null) {
084                deleteRouteJob(Action.START);
085            }
086            if (scheduledRouteDetails.getStopJobDetail() != null) {
087                deleteRouteJob(Action.STOP);
088            }
089            if (scheduledRouteDetails.getSuspendJobDetail() != null) {
090                deleteRouteJob(Action.SUSPEND);
091            }
092            if (scheduledRouteDetails.getResumeJobDetail() != null) {
093                deleteRouteJob(Action.RESUME);
094            }
095        }
096    
097        /* (non-Javadoc)
098         * @see org.apache.camel.routepolicy.quartz.ScheduledRoutePolicy#createTrigger(org.apache.camel.routepolicy.quartz.ScheduledRoutePolicyConstants.Action)
099         */
100        @Override
101        protected Trigger createTrigger(Action action, Route route) throws Exception {
102            CronTrigger trigger = null;
103            
104            if (action == Action.START) {
105                trigger = new CronTrigger(TRIGGER_START + route.getId(), TRIGGER_GROUP + route.getId(), getRouteStartTime());
106            } else if (action == Action.STOP) {
107                trigger = new CronTrigger(TRIGGER_STOP + route.getId(), TRIGGER_GROUP + route.getId(), getRouteStopTime());
108            } else if (action == Action.SUSPEND) {
109                trigger = new CronTrigger(TRIGGER_SUSPEND + route.getId(), TRIGGER_GROUP + route.getId(), getRouteSuspendTime());
110            } else if (action == Action.RESUME) {
111                trigger = new CronTrigger(TRIGGER_RESUME + route.getId(), TRIGGER_GROUP + route.getId(), getRouteResumeTime());
112            }
113            
114            return trigger;
115        }
116        
117        public void setRouteStartTime(String routeStartTime) {
118            this.routeStartTime = routeStartTime;
119        }
120    
121        public String getRouteStartTime() {
122            return routeStartTime;
123        }
124    
125        public void setRouteStopTime(String routeStopTime) {
126            this.routeStopTime = routeStopTime;
127        }
128    
129        public String getRouteStopTime() {
130            return routeStopTime;
131        }
132    
133        public void setRouteSuspendTime(String routeSuspendTime) {
134            this.routeSuspendTime = routeSuspendTime;
135        }
136    
137        public String getRouteSuspendTime() {
138            return routeSuspendTime;
139        }
140    
141        public void setRouteResumeTime(String routeResumeTime) {
142            this.routeResumeTime = routeResumeTime;
143        }
144    
145        public String getRouteResumeTime() {
146            return routeResumeTime;
147        }    
148    
149    }