001: package com.xoetrope.task;
002:
003: import java.util.Date;
004:
005: /**
006: * Create a new job. A job is associated with an executable task and is run at
007: * a defined interval or after a defined time.
008: *
009: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
010: * the GNU Public License (GPL), please see license.txt for more details. If
011: * you make commercial use of this software you must purchase a commercial
012: * license from Xoetrope.</p>
013: * <p> $Revision: 1.4 $</p>
014: */
015: public class XJob extends Thread implements XArguments {
016: int id;
017: Object className;
018: int type;
019: long interval;
020: long sleepInterval;
021: long startDate;
022: Object obj;
023: Object[] arguments;
024: boolean keepRunning;
025:
026: /**
027: * Create a new job
028: * @param jobId the ID by which the job will be identified
029: * @param cls the XExecutable class
030: * @param t the type
031: * @param intrvl the interval
032: * @param start the start time
033: * @param args the arguments
034: */
035: public XJob(int jobId, Object cls, int t, long intrvl, long start,
036: Object[] args) {
037: id = jobId;
038: className = cls;
039: type = t;
040: interval = intrvl;
041: sleepInterval = interval / 5l;
042: if (sleepInterval < 200)
043: sleepInterval = 200;
044: startDate = start;
045:
046: if (args != null) {
047: arguments = new Object[args.length];
048: for (int i = 0; i < args.length; i++)
049: arguments[i] = args[i];
050: }
051:
052: keepRunning = true;
053: }
054:
055: /**
056: * Run the job and execute the task when ready
057: */
058: public void run() {
059: boolean jobExecuted = false;
060: do {
061: if (new Date().getTime() > startDate) {
062: execute();
063: jobExecuted = true;
064: startDate += interval;
065: }
066:
067: try {
068: sleep(sleepInterval);
069: } catch (Exception e) {
070: }
071: } while ((keepRunning)
072: && ((type > XScheduler.ONCE_OFF_JOB) || !jobExecuted));
073: }
074:
075: /**
076: * Execute the task, the actual work of the callback or executable object
077: */
078: public void execute() {
079: if (keepRunning) {
080: XExecutable exe = null;
081: try {
082: if (className instanceof String)
083: exe = (XExecutable) Class.forName(
084: ((String) className).trim()).newInstance();
085: else
086: exe = (XExecutable) className;
087: exe.execute(this );
088: } catch (Exception ex) {
089: ex.printStackTrace();
090: }
091: }
092: }
093:
094: /**
095: * End the job
096: */
097: public void kill() {
098: keepRunning = false;
099: }
100:
101: /**
102: * Get the number of arguments belonging/needed by to this job
103: * @return the number of arguments
104: */
105: public int getNumArguments() {
106: if (arguments == null)
107: return 0;
108: return arguments.length;
109: }
110:
111: /**
112: * Get an argument
113: * @param index the index of the argument
114: * @return the argument object
115: */
116: public Object getArgument(int index) {
117: if (arguments == null)
118: return null;
119: return arguments[index];
120: }
121: }
|