KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > job > DependsJob


1 package org.oddjob.jobs.job;
2
3 import org.oddjob.Stateful;
4 import org.oddjob.Stoppable;
5 import org.oddjob.framework.SimpleJob;
6 import org.oddjob.state.JobState;
7 import org.oddjob.state.JobStateEvent;
8 import org.oddjob.state.JobStateListener;
9 import org.oddjob.state.StateMemory;
10 import org.oddjob.util.OddjobConfigException;
11
12 /**
13  * @oddjob.description A job which depends on another job. If the other job is
14  * ready this job will run the job. If the job is executing
15  * this job will wait. If the job has finished this job will
16  * reflect the completion state.
17  *
18  * @author Rob Gordon
19  */

20
21 public class DependsJob extends SimpleJob implements Stoppable,
22         JobStateListener {
23     private static final long serialVersionUID = 20050806;
24
25     /**
26      * @oddjob.property
27      * @oddjob.description Job to depend on.
28      * @oddjob.required Yes.
29      */

30     private transient Stateful job;
31
32     private transient volatile JobStateEvent event;
33
34     /**
35      * Set the stop node directly.
36      *
37      * @param node
38      * The node to stop.
39      */

40     synchronized public void setJob(Stateful node) {
41         this.job = node;
42     }
43
44     /**
45      * Get the node to stop.
46      *
47      * @return The node.
48      */

49     synchronized public Stateful getJob() {
50         return this.job;
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.oddjob.jobs.AbstractJob#execute()
57      */

58     protected int execute() throws Throwable JavaDoc {
59         if (job == null) {
60             throw new OddjobConfigException("Job must be set.");
61         }
62         try {
63             job.addJobStateListener(this);
64             while (!stop) {
65                 JobState state = event.getJobState();
66                 logger().debug("State is [" + state + "]");
67                 long sleep = 10;
68                 if (state == JobState.READY) {
69                     if (job instanceof Runnable JavaDoc) {
70                         StateMemory remember = new StateMemory();
71                         remember.run((Runnable JavaDoc) job);
72                         if (remember.getJobState() == JobState.COMPLETE) {
73                             return 0;
74                         } else if (remember.getJobState() == JobState.NOT_COMPLETE) {
75                             return 1;
76                         } else if (remember.getJobState() == JobState.EXCEPTION) {
77                             throw remember.getThrowable();
78                         }
79                         // this shouldn't happen but something else
80
// could change the state before we ran it.
81
// go round again...
82
}
83                     else {
84                         // if job's not runnable we could be waiting a long time.
85
sleep = 0;
86                     }
87                 } else if (state == JobState.COMPLETE) {
88                     return 0;
89                 } else if (state == JobState.NOT_COMPLETE) {
90                     return 1;
91                 } else if (state == JobState.EXCEPTION) {
92                     throw event.getException();
93                 } else {
94                     logger().debug("[" + job + "] still executing...");
95                     sleep = 1000;
96                 }
97                 sleep(sleep);
98             }
99
100             return 0;
101         } finally {
102             job.removeJobStateListener(this);
103         }
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see org.oddjob.state.JobStateListener#jobStateChange(org.oddjob.state.JobStateEvent)
110      */

111     public void jobStateChange(JobStateEvent event) {
112         this.event = event;
113         synchronized (this) {
114             notifyAll();
115         }
116     }
117 }
118
Popular Tags