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.camel.util.ObjectHelper;
024 import org.quartz.CronTrigger;
025 import org.quartz.Trigger;
026
027 public class CronScheduledRoutePolicy extends ScheduledRoutePolicy implements ScheduledRoutePolicyConstants {
028 private String routeStartTime;
029 private String routeStopTime;
030 private String routeSuspendTime;
031 private String routeResumeTime;
032
033 public void onInit(Route route) {
034 try {
035 doOnInit(route);
036 } catch (Exception e) {
037 throw ObjectHelper.wrapRuntimeCamelException(e);
038 }
039 }
040
041 protected void doOnInit(Route route) throws Exception {
042 QuartzComponent quartz = route.getRouteContext().getCamelContext().getComponent("quartz", QuartzComponent.class);
043 setScheduler(quartz.getScheduler());
044
045 // Important: do not start scheduler as QuartzComponent does that automatic
046 // when CamelContext has been fully initialized and started
047
048 if (getRouteStopGracePeriod() == 0) {
049 setRouteStopGracePeriod(10000);
050 }
051
052 if (getTimeUnit() == null) {
053 setTimeUnit(TimeUnit.MILLISECONDS);
054 }
055
056 // validate time options has been configured
057 if ((getRouteStartTime() == null) && (getRouteStopTime() == null) && (getRouteSuspendTime() == null) && (getRouteResumeTime() == null)) {
058 throw new IllegalArgumentException("Scheduled Route Policy for route {} has no stop/stop/suspend/resume times specified");
059 }
060
061 if (scheduledRouteDetails == null) {
062 scheduledRouteDetails = new ScheduledRouteDetails();
063 scheduledRouteDetails.setRoute(route);
064
065 if (getRouteStartTime() != null) {
066 scheduleRoute(Action.START);
067 }
068 if (getRouteStopTime() != null) {
069 scheduleRoute(Action.STOP);
070 }
071
072 if (getRouteSuspendTime() != null) {
073 scheduleRoute(Action.SUSPEND);
074 }
075 if (getRouteResumeTime() != null) {
076 scheduleRoute(Action.RESUME);
077 }
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 @Override
098 protected Trigger createTrigger(Action action, Route route) throws Exception {
099 CronTrigger trigger = null;
100
101 if (action == Action.START) {
102 trigger = new CronTrigger(TRIGGER_START + route.getId(), TRIGGER_GROUP + route.getId(), getRouteStartTime());
103 } else if (action == Action.STOP) {
104 trigger = new CronTrigger(TRIGGER_STOP + route.getId(), TRIGGER_GROUP + route.getId(), getRouteStopTime());
105 } else if (action == Action.SUSPEND) {
106 trigger = new CronTrigger(TRIGGER_SUSPEND + route.getId(), TRIGGER_GROUP + route.getId(), getRouteSuspendTime());
107 } else if (action == Action.RESUME) {
108 trigger = new CronTrigger(TRIGGER_RESUME + route.getId(), TRIGGER_GROUP + route.getId(), getRouteResumeTime());
109 }
110
111 return trigger;
112 }
113
114 public void setRouteStartTime(String routeStartTime) {
115 this.routeStartTime = routeStartTime;
116 }
117
118 public String getRouteStartTime() {
119 return routeStartTime;
120 }
121
122 public void setRouteStopTime(String routeStopTime) {
123 this.routeStopTime = routeStopTime;
124 }
125
126 public String getRouteStopTime() {
127 return routeStopTime;
128 }
129
130 public void setRouteSuspendTime(String routeSuspendTime) {
131 this.routeSuspendTime = routeSuspendTime;
132 }
133
134 public String getRouteSuspendTime() {
135 return routeSuspendTime;
136 }
137
138 public void setRouteResumeTime(String routeResumeTime) {
139 this.routeResumeTime = routeResumeTime;
140 }
141
142 public String getRouteResumeTime() {
143 return routeResumeTime;
144 }
145
146 }