1 4 package org.oddjob.quartz; 5 6 import java.io.IOException ; 7 import java.io.ObjectInputStream ; 8 import java.io.ObjectOutputStream ; 9 import java.io.Serializable ; 10 import java.text.ParseException ; 11 import java.util.Date ; 12 13 import org.oddjob.Iconic; 14 import org.oddjob.Resetable; 15 import org.oddjob.Stateful; 16 import org.oddjob.arooa.registry.ComponentRegistry; 17 import org.oddjob.framework.BasePrimary; 18 import org.oddjob.images.IconEvent; 19 import org.oddjob.images.IconListener; 20 import org.oddjob.state.JobStateEvent; 21 import org.oddjob.state.JobStateListener; 22 import org.oddjob.util.OddjobConfigException; 23 import org.oddjob.util.OddjobConstantException; 24 import org.quartz.CronTrigger; 25 import org.quartz.JobDataMap; 26 import org.quartz.JobDetail; 27 import org.quartz.Scheduler; 28 import org.quartz.SchedulerException; 29 30 37 public class CronSchedule extends BasePrimary 38 implements Serializable , Stateful, QuartzSchedule { 39 private static final long serialVersionUID = 20051122; 40 41 42 private static final String GROUP_NAME = Scheduler.DEFAULT_GROUP; 43 44 47 private JobToken jobToken; 48 49 private transient ComponentRegistry componentRegistry; 50 51 56 private String expression; 57 58 59 private String id; 60 61 private transient JobStateListener jobListener; 62 63 private transient IconListener iconListener; 64 65 public CronSchedule() { 66 completeConstruction(); 67 } 68 69 private void completeConstruction() { 70 jobListener = new JobStateListener() { 71 public void jobStateChange(JobStateEvent event) { 72 stateHandler.fireEvent(new JobStateEvent( 73 CronSchedule.this, event.getJobState(), 74 event.getTime(), event.getException())); 75 } 76 }; 77 78 iconListener = new IconListener() { 79 public void iconEvent(IconEvent e) { 80 iconHelper.changeIcon(e.getIconId()); 81 } 82 }; 83 } 84 85 88 public void scheduleWith(Scheduler scheduler) throws SchedulerException { 89 if (jobToken == null) { 90 throw new NullPointerException ("Job to schedule must be specified."); 91 } 92 93 OddjobData ojd = new OddjobData(); 94 ojd.setJob(new Execute()); 95 96 JobDataMap jobDataMap = new JobDataMap(); 97 98 jobDataMap.put(OddjobData.ODDJOB_DATA, ojd); 99 100 JobDetail jobDetail = new JobDetail(toString(), 101 Scheduler.DEFAULT_GROUP, 102 RunnableQuartzJob.class); 103 104 jobDetail.setJobDataMap(jobDataMap); 105 106 CronTrigger trigger = null; 107 try { 108 trigger = new CronTrigger(triggerName(), 109 GROUP_NAME, 110 expression); 111 } 112 catch (ParseException e) { 113 throw new SchedulerException("Failed creating trigger for expression [" 114 + expression + "]", e); 115 } 116 scheduler.scheduleJob(jobDetail, trigger); 117 118 } 119 120 124 public void unscheduleFrom(Scheduler scheduler) throws SchedulerException { 125 scheduler.unscheduleJob(triggerName(), GROUP_NAME); 126 } 127 128 134 public void setId(String id) { 135 if (this.id != null) { 136 throw new OddjobConfigException("Id can't be changed."); 137 } 138 this.id = id; 139 } 140 141 146 private String triggerName() { 147 return id + "-Trigger"; 148 } 149 150 public void setComponentRegistry(ComponentRegistry componentRegistry) { 151 this.componentRegistry = componentRegistry; 152 } 153 154 public void setJob(Runnable job) { 155 if (job == null) { 156 throw new OddjobConfigException("No job to schedule specified!"); 157 } 158 if (jobToken != null) { 159 throw new OddjobConstantException("[" + this 160 + "] job can't be changed."); 161 } 162 163 jobToken = JobToken.create(componentRegistry, job); 164 } 165 166 167 public String toString() { 168 if (getName() == null) { 169 return "Cron [" + jobToken + "] expression [" + expression + "]"; 170 } 171 return getName(); 172 } 173 174 177 public String getExpression() { 178 return expression; 179 } 180 183 public void setExpression(String expression) { 184 this.expression = expression; 185 } 186 187 private void writeObject(ObjectOutputStream os) 188 throws IOException { 189 os.defaultWriteObject(); 190 } 191 192 private void readObject(ObjectInputStream is) 193 throws IOException , ClassNotFoundException { 194 is.defaultReadObject(); 195 completeConstruction(); 196 } 197 198 200 class Execute implements Runnable { 201 synchronized public void run() { 202 logger().debug("Executing at [" + new Date ()+ "]"); 203 Runnable job = (Runnable ) JobToken.retrieve(componentRegistry, jobToken); 204 205 if (job == null) { 206 setJobStateException( 207 new NullPointerException ("Failed to find job for [" + jobToken + "]")); 208 } 209 210 if (job instanceof Resetable) { 211 ((Resetable) job).hardReset(); 212 } 213 214 if (job instanceof Stateful) { 215 ((Stateful) job).addJobStateListener(jobListener); 216 } 217 if (job instanceof Iconic) { 218 ((Iconic) job).addIconListener(iconListener); 219 } 220 221 try { 222 job.run(); 223 } 224 catch (Throwable t) { 225 setJobStateException(t); 226 } 227 finally { 228 if (job instanceof Stateful) { 229 ((Stateful) job).removeJobStateListener(jobListener); 230 } 231 if (job instanceof Iconic) { 232 ((Iconic) job).removeIconListener(iconListener); 233 } 234 } 235 } 236 } 237 } 238 | Popular Tags |