01: package org.sakaiproject.component.app.scheduler.jobs;
02:
03: import java.util.Iterator;
04: import java.util.List;
05:
06: import org.apache.commons.logging.Log;
07: import org.apache.commons.logging.LogFactory;
08: import org.quartz.Job;
09: import org.quartz.JobExecutionContext;
10: import org.quartz.JobExecutionException;
11: import org.sakaiproject.api.app.scheduler.DelayedInvocation;
12: import org.sakaiproject.api.app.scheduler.ScheduledInvocationCommand;
13: import org.sakaiproject.component.app.scheduler.DelayedInvocationReader;
14: import org.sakaiproject.component.cover.ComponentManager;
15: import org.sakaiproject.db.api.SqlService;
16: import org.sakaiproject.time.api.Time;
17: import org.sakaiproject.time.api.TimeService;
18:
19: public class ScheduledInvocationRunner implements Job {
20:
21: private static final Log LOG = LogFactory
22: .getLog(ScheduledInvocationRunner.class);
23:
24: /* (non-Javadoc)
25: * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
26: */
27: public void execute(JobExecutionContext arg0)
28: throws JobExecutionException {
29:
30: SqlService sqlService = ((SqlService) ComponentManager
31: .get("org.sakaiproject.db.api.SqlService"));
32: Time now = ((TimeService) ComponentManager
33: .get("org.sakaiproject.time.api.TimeService"))
34: .newTime();
35:
36: String sql = "SELECT INVOCATION_ID, INVOCATION_TIME, COMPONENT, CONTEXT FROM SCHEDULER_DELAYED_INVOCATION WHERE INVOCATION_TIME < ?";
37:
38: Object[] fields = new Object[1];
39:
40: fields[0] = now;
41:
42: LOG.debug("SQL: " + sql + " NOW:" + now);
43: List invocations = sqlService.dbRead(sql, fields,
44: new DelayedInvocationReader());
45:
46: for (Iterator i = invocations.iterator(); i.hasNext();) {
47:
48: DelayedInvocation invocation = (DelayedInvocation) i.next();
49:
50: if (invocation != null) {
51:
52: LOG
53: .debug("processing invocation: [" + invocation
54: + "]");
55:
56: try {
57: ScheduledInvocationCommand command = (ScheduledInvocationCommand) ComponentManager
58: .get(invocation.componentId);
59: command.execute(invocation.contextId);
60: } catch (Exception e) {
61: LOG.error("Failed to execute component: ["
62: + invocation.componentId + "]: " + e);
63: } finally {
64: sql = "DELETE FROM SCHEDULER_DELAYED_INVOCATION WHERE INVOCATION_ID = ?";
65:
66: fields[0] = invocation.uuid;
67:
68: LOG.debug("SQL: " + sql);
69: sqlService.dbWrite(sql, fields);
70: }
71: }
72: }
73: }
74:
75: }
|