1 package org.apache.turbine.services.schedule;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.List;
23
24 import org.apache.torque.TorqueException;
25 import org.apache.torque.criteria.Criteria;
26 import org.apache.turbine.util.TurbineException;
27
28 /**
29 * Service for a cron like scheduler.
30 *
31 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
32 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33 * @version $Id: TorqueSchedulerService.java 534527 2007-05-02 16:10:59Z tv $
34 *
35 * @deprecated Use {@link QuartzSchedulerService} instead
36 */
37 @Deprecated
38 public class TorqueSchedulerService extends AbstractSchedulerService
39 {
40 /**
41 * Load all jobs from configuration storage
42 *
43 * @return the list of pre-configured jobs
44 * @throws TurbineException
45 */
46 @Override
47 protected List<? extends JobEntry> loadJobs() throws TurbineException
48 {
49 // Load all from cold storage.
50 try
51 {
52 List<JobEntryTorque> jobsTorque = JobEntryTorquePeer.doSelect(new Criteria());
53
54 for (JobEntryTorque job : jobsTorque)
55 {
56 job.calcRunTime();
57 }
58
59 return jobsTorque;
60 }
61 catch (TorqueException e)
62 {
63 throw new TurbineException("Error retrieving initial job list from persistent storage.", e);
64 }
65 }
66
67 /**
68 * @see org.apache.turbine.services.schedule.ScheduleService#newJob(int, int, int, int, int, java.lang.String)
69 */
70 @Override
71 public JobEntry newJob(int sec, int min, int hour, int wd, int day_mo, String task) throws TurbineException
72 {
73 JobEntryTorque jet = new JobEntryTorque();
74 jet.setSecond(sec);
75 jet.setMinute(min);
76 jet.setHour(hour);
77 jet.setWeekDay(wd);
78 jet.setDayOfMonth(day_mo);
79 jet.setTask(task);
80
81 return jet;
82 }
83
84 /**
85 * Get a specific Job from Storage.
86 *
87 * @param oid
88 * The int id for the job.
89 * @return A JobEntry.
90 * @throws TurbineException
91 * job could not be retrieved.
92 */
93 @Override
94 public JobEntry getJob(int oid) throws TurbineException
95 {
96 try
97 {
98 JobEntryTorque je = JobEntryTorquePeer.retrieveByPK(oid);
99 return scheduleQueue.getJob(je);
100 }
101 catch (TorqueException e)
102 {
103 throw new TurbineException("Error retrieving job from persistent storage.", e);
104 }
105 }
106
107 /**
108 * Remove a job from the queue.
109 *
110 * @param je
111 * A JobEntry with the job to remove.
112 * @throws TurbineException
113 * job could not be removed
114 */
115 @Override
116 public void removeJob(JobEntry je) throws TurbineException
117 {
118 try
119 {
120 // First remove from DB.
121 Criteria c = new Criteria().where(JobEntryTorquePeer.JOB_ID, Integer.valueOf(je.getJobId()));
122 JobEntryTorquePeer.doDelete(c);
123
124 // Remove from the queue.
125 scheduleQueue.remove(je);
126
127 // restart the scheduler
128 restart();
129 }
130 catch (TorqueException e)
131 {
132 throw new TurbineException("Problem removing Scheduled Job: " + je.getTask(), e);
133 }
134 }
135
136 /**
137 * Add or update a job.
138 *
139 * @param je
140 * A JobEntry with the job to modify
141 * @throws TurbineException
142 * job could not be updated
143 */
144 @Override
145 public void updateJob(JobEntry je) throws TurbineException
146 {
147 try
148 {
149 je.calcRunTime();
150
151 // Update the queue.
152 if (je.isNew())
153 {
154 scheduleQueue.add(je);
155 }
156 else
157 {
158 scheduleQueue.modify(je);
159 }
160
161 if (je instanceof JobEntryTorque)
162 {
163 ((JobEntryTorque)je).save();
164 }
165
166 restart();
167 }
168 catch (TorqueException e)
169 {
170 throw new TurbineException("Problem persisting Scheduled Job: " + je.getTask(), e);
171 }
172 catch (TurbineException e)
173 {
174 throw new TurbineException("Problem updating Scheduled Job: " + je.getTask(), e);
175 }
176 }
177 }