KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > ScheduleJob


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 /*
6  * Created by IntelliJ IDEA.
7  * User: plightbo
8  * Date: May 22, 2002
9  * Time: 4:05:53 PM
10  */

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 JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * Schedules a job in the Quartz job scheduler to be executed one or more times in the future.
36  * The following arguments are required:
37  *
38  * <ul>
39  * <li> triggerId - the id of the trigger function defined in the XML workflow
40  * <li> jobName - the name to be given to the job
41  * <li> triggerName - the name to be given to the trigger
42  * <li> groupName - the group given to both the job and the trigger
43  * </ul>
44  *
45  * The following arguments are optional:
46  * <ul>
47  * <li> username - the system account's username that will execute the function in the future.
48  * If this is not specified value from WorkflowContext.getCaller() is used
49  * <li> password - the system account's password
50  * <li> local - if set to the true, a LocalWorkflowJob is used, bypassing the need for SOAP support.
51  * Will be ignored if "workflowClass" is specified.
52  * <li> jobClass - the class implementing 'Job' to run, defaults to WorkflowJob. If not specified,
53  * defaults to either a WorkflowJob or a LocalWorkflowJob if "local" is set to true.
54  * <li>schedulerName - the name of an existing scheduler to use</li>
55  * <li>schdulerStart - if "true", start the scheduler if it hasn't been started already</li>
56  * <li>txHack - set this to true if you are getting lockups while running with transactions (defaults to false)</li>
57  * </ul>
58  *
59  * If you are using a cron trigger, the following is required:
60  * <ul>
61  * <li> cronExpression - the Cron expression
62  * </ul>
63  *
64  * If you are using a simple trigger, the follow are all optional:
65  * <ul>
66  * <li> startOffset - the offset, in milliseconds, from the current time. (default is 0)
67  * <li> endOffset - the offset, in milliseconds, from the current time. (default is infinity)
68  * <li> repeat - the repeat count (default is 0). Can also be REPEAT_INDEFINITELY
69  * <li> repeatDelay - the time delay, in milliseconds, between repeats (default is 0)
70  * </ul>
71  *
72  * @author <a HREF="mike.g.slack@usahq.unitedspacealliance.com ">Michael G. Slack</a>
73  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
74  */

75 public class ScheduleJob implements FunctionProvider {
76     //~ Static fields/initializers /////////////////////////////////////////////
77

78     private static final Log log = LogFactory.getLog(ScheduleJob.class);
79
80     //~ Methods ////////////////////////////////////////////////////////////////
81

82     public void execute(Map JavaDoc transientVars, Map JavaDoc 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 JavaDoc) args.get("triggerId"));
90             String JavaDoc jobName = (String JavaDoc) args.get("jobName");
91             String JavaDoc triggerName = (String JavaDoc) args.get("triggerName");
92             String JavaDoc groupName = (String JavaDoc) args.get("groupName");
93
94             String JavaDoc username = (String JavaDoc) args.get("username");
95             String JavaDoc password = (String JavaDoc) args.get("password");
96
97             boolean txHack = TextUtils.parseBoolean((String JavaDoc) args.get("txHack"));
98
99             if (username == null) {
100                 username = context.getCaller();
101             }
102
103             String JavaDoc cronExpression = (String JavaDoc) args.get("cronExpression");
104
105             jobName = jobName + ":" + entry.getId();
106             triggerName = triggerName + ":" + entry.getId();
107             groupName = groupName + ":" + entry.getId();
108
109             String JavaDoc schedulerName = (String JavaDoc) 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 JavaDoc) args.get("schedulerStart"))) {
121                 log.info("Starting Quartz Job Scheduler");
122                 s.start();
123             }
124
125             Class JavaDoc jobClass;
126             String JavaDoc jobClassArg = (String JavaDoc) args.get("jobClass");
127
128             if (jobClassArg != null) {
129                 jobClass = Class.forName(jobClassArg);
130             } else if (TextUtils.parseBoolean((String JavaDoc) 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                 // get start date - default is now
143
Date JavaDoc startDate = null;
144
145                 try {
146                     String JavaDoc start = (String JavaDoc) args.get("startOffset");
147
148                     if (s != null) {
149                         startDate = new Date JavaDoc(now + Long.parseLong(start));
150                     }
151                 } catch (NumberFormatException JavaDoc e) {
152                 }
153
154                 if (startDate == null) {
155                     startDate = new Date JavaDoc(now);
156                 }
157
158                 // get end date - default is null
159
Date JavaDoc endDate = null;
160
161                 try {
162                     String JavaDoc end = (String JavaDoc) args.get("endOffset");
163
164                     if (s != null) {
165                         startDate = new Date JavaDoc(now + Long.parseLong(end));
166                     }
167                 } catch (NumberFormatException JavaDoc e) {
168                 }
169
170                 // get the repeat amount - default is 0
171
int repeat = 0;
172
173                 try {
174                     String JavaDoc r = (String JavaDoc) 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 JavaDoc e) {
184                 }
185
186                 // get repeat delay - default is 0
187
long delay = 0;
188
189                 try {
190                     String JavaDoc rd = (String JavaDoc) args.get("repeatDelay");
191
192                     if (rd != null) {
193                         delay = Long.parseLong(rd);
194                     }
195                 } catch (NumberFormatException JavaDoc 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 JavaDoc e) {
232             log.error("Error scheduling job", e);
233         }
234     }
235 }
236
Popular Tags