1 5 11 package com.opensymphony.workflow.util; 12 13 import com.opensymphony.module.propertyset.PropertySet; 14 15 import com.opensymphony.util.TextUtils; 16 17 import com.opensymphony.workflow.FunctionProvider; 18 import com.opensymphony.workflow.WorkflowContext; 19 import com.opensymphony.workflow.spi.WorkflowEntry; 20 import com.opensymphony.workflow.timer.LocalWorkflowJob; 21 import com.opensymphony.workflow.timer.WorkflowJob; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.quartz.*; 27 28 import org.quartz.impl.StdSchedulerFactory; 29 30 import java.util.Date ; 31 import java.util.Map ; 32 33 34 75 public class ScheduleJob implements FunctionProvider { 76 78 private static final Log log = LogFactory.getLog(ScheduleJob.class); 79 80 82 public void execute(Map transientVars, Map args, PropertySet ps) { 83 try { 84 WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry"); 85 WorkflowContext context = (WorkflowContext) transientVars.get("context"); 86 87 log.info("Starting to schdule job for WF #" + entry.getId()); 88 89 int triggerId = TextUtils.parseInt((String ) args.get("triggerId")); 90 String jobName = (String ) args.get("jobName"); 91 String triggerName = (String ) args.get("triggerName"); 92 String groupName = (String ) args.get("groupName"); 93 94 String username = (String ) args.get("username"); 95 String password = (String ) args.get("password"); 96 97 boolean txHack = TextUtils.parseBoolean((String ) args.get("txHack")); 98 99 if (username == null) { 100 username = context.getCaller(); 101 } 102 103 String cronExpression = (String ) args.get("cronExpression"); 104 105 jobName = jobName + ":" + entry.getId(); 106 triggerName = triggerName + ":" + entry.getId(); 107 groupName = groupName + ":" + entry.getId(); 108 109 String schedulerName = (String ) args.get("schedulerName"); 110 Scheduler s; 111 112 SchedulerFactory factory = new StdSchedulerFactory(); 113 114 if ((schedulerName == null) || ("".equals(schedulerName.trim()))) { 115 s = factory.getScheduler(); 116 } else { 117 s = factory.getScheduler(schedulerName); 118 } 119 120 if (TextUtils.parseBoolean((String ) args.get("schedulerStart"))) { 121 log.info("Starting Quartz Job Scheduler"); 122 s.start(); 123 } 124 125 Class jobClass; 126 String jobClassArg = (String ) args.get("jobClass"); 127 128 if (jobClassArg != null) { 129 jobClass = Class.forName(jobClassArg); 130 } else if (TextUtils.parseBoolean((String ) args.get("local"))) { 131 jobClass = LocalWorkflowJob.class; 132 } else { 133 jobClass = WorkflowJob.class; 134 } 135 136 JobDetail jobDetail = new JobDetail(jobName, groupName, jobClass); 137 Trigger trigger; 138 139 if (cronExpression == null) { 140 long now = System.currentTimeMillis(); 141 142 Date startDate = null; 144 145 try { 146 String start = (String ) args.get("startOffset"); 147 148 if (s != null) { 149 startDate = new Date (now + Long.parseLong(start)); 150 } 151 } catch (NumberFormatException e) { 152 } 153 154 if (startDate == null) { 155 startDate = new Date (now); 156 } 157 158 Date endDate = null; 160 161 try { 162 String end = (String ) args.get("endOffset"); 163 164 if (s != null) { 165 startDate = new Date (now + Long.parseLong(end)); 166 } 167 } catch (NumberFormatException e) { 168 } 169 170 int repeat = 0; 172 173 try { 174 String r = (String ) args.get("repeat"); 175 176 if (r != null) { 177 if (r.equalsIgnoreCase("REPEAT_INDEFINITELY")) { 178 repeat = SimpleTrigger.REPEAT_INDEFINITELY; 179 } else { 180 repeat = TextUtils.parseInt(r); 181 } 182 } 183 } catch (NumberFormatException e) { 184 } 185 186 long delay = 0; 188 189 try { 190 String rd = (String ) args.get("repeatDelay"); 191 192 if (rd != null) { 193 delay = Long.parseLong(rd); 194 } 195 } catch (NumberFormatException e) { 196 } 197 198 trigger = new SimpleTrigger(triggerName, groupName, jobName, groupName, startDate, endDate, repeat, delay); 199 } else { 200 trigger = new CronTrigger(triggerName, groupName, jobName, groupName, cronExpression); 201 } 202 203 JobDataMap dataMap = new JobDataMap(); 204 dataMap.put("triggerId", triggerId); 205 dataMap.put("entryId", entry.getId()); 206 dataMap.put("username", username); 207 dataMap.put("password", password); 208 jobDetail.setJobDataMap(dataMap); 209 jobDetail.setDurability(true); 210 211 trigger.setJobName(jobDetail.getName()); 212 trigger.setJobGroup(jobDetail.getGroup()); 213 214 if (txHack && !s.isPaused() && !s.isShutdown()) { 215 s.pause(); 216 217 try { 218 s.addJob(jobDetail, true); 219 s.scheduleJob(trigger); 220 } catch (SchedulerException e) { 221 throw e; 222 } finally { 223 s.start(); 224 } 225 } else { 226 s.addJob(jobDetail, true); 227 s.scheduleJob(trigger); 228 } 229 230 log.info("Job scheduled"); 231 } catch (Exception e) { 232 log.error("Error scheduling job", e); 233 } 234 } 235 } 236 | Popular Tags |