KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > job > support > JobTask


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.job.support;
25
26 import java.util.Date JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.riotfamily.riot.job.Job;
31 import org.riotfamily.riot.job.JobInterruptedException;
32 import org.riotfamily.riot.job.context.TaskJobContext;
33 import org.riotfamily.riot.job.persistence.JobDao;
34 import org.riotfamily.riot.job.persistence.JobDetail;
35 import org.riotfamily.riot.job.persistence.JobLogEntry;
36 import org.riotfamily.riot.job.ui.JobUIUpdater;
37 import org.springframework.dao.DataAccessException;
38
39
40 public class JobTask implements Runnable JavaDoc {
41     
42     private static Log log = LogFactory.getLog(JobTask.class);
43     
44     private Job job;
45
46     private JobDetail detail;
47     
48     private JobDao dao;
49     
50     private JobUIUpdater uiUpdater;
51
52     private TaskList taskList;
53     
54     private Log jobLog;
55     
56     private Thread JavaDoc thread;
57     
58     private boolean interrupted;
59     
60
61     public JobTask(Job job, JobDetail detail, JobDao dao,
62             JobUIUpdater uiUpdater, TaskList taskList) {
63         
64         this.job = job;
65         this.detail = detail;
66         this.dao = dao;
67         this.uiUpdater = uiUpdater;
68         this.taskList = taskList;
69         
70         jobLog = LogFactory.getLog(job.getClass());
71     }
72
73     public void interrupt() {
74         if (interrupted) {
75             return;
76         }
77         interrupted = true;
78         if (thread != null) {
79             thread.interrupt();
80             try {
81                 synchronized (thread) {
82                     thread.wait(10000);
83                 }
84             }
85             catch (InterruptedException JavaDoc e) {
86             }
87             if (detail.getState() < JobDetail.INTERRUPTED) {
88                 log.warn("Job did not interrupt its work within 10 seconds.");
89             }
90         }
91     }
92     
93     public boolean isInterrupted() {
94         Thread.yield();
95         return interrupted;
96     }
97
98     public void run() {
99         if (interrupted) {
100             return;
101         }
102         thread = Thread.currentThread();
103         try {
104             jobStarted();
105             job.execute(new TaskJobContext(this));
106         }
107         catch (JobInterruptedException e) {
108         }
109         catch (Exception JavaDoc e) {
110             interrupted = true;
111             logError(e.getMessage());
112             log.error("Job aborted due to exception", e);
113         }
114         jobStoped(!interrupted);
115         synchronized (thread) {
116             thread.notifyAll();
117         }
118     }
119     
120     public JobDetail getDetail() {
121         return this.detail;
122     }
123     
124     /**
125      * Notifies the DAO that a step has been completed.
126      * @throws JobInterruptedException if the job has been interrupted
127      */

128     public void stepCompleted() throws JobInterruptedException {
129         if (detail.getStepsTotal() > 0) {
130             detail.stepCompleted();
131             dao.updateJobDetail(detail);
132             uiUpdater.updateJob(detail);
133         }
134         if (isInterrupted()) {
135             throw new JobInterruptedException();
136         }
137     }
138             
139     public void updateExecutionTime() {
140         detail.updateExecutionTime();
141         dao.updateJobDetail(detail);
142     }
143     
144     public void updateDescription(String JavaDoc description) {
145         detail.setDescription(description);
146         dao.updateJobDetail(detail);
147         uiUpdater.updateJob(detail);
148     }
149     
150     public void updateStepsTotal(int stepsTotal) {
151         detail.setStepsTotal(stepsTotal);
152         dao.updateJobDetail(detail);
153         uiUpdater.updateJob(detail);
154     }
155     
156     /**
157      * Logs an info message.
158      */

159     public void logInfo(String JavaDoc message) {
160         jobLog.info(message);
161         log(message, JobLogEntry.INFO);
162     }
163     
164     /**
165      * Logs an error message.
166      */

167     public void logError(String JavaDoc message) {
168         jobLog.error(message);
169         log(message, JobLogEntry.ERROR);
170     }
171     
172     /**
173      * Logs a message.
174      */

175     protected void log(String JavaDoc message, int priority) {
176         JobLogEntry entry = new JobLogEntry(detail, priority, message);
177         try {
178             dao.log(entry);
179         }
180         catch (DataAccessException e) {
181             log.error("Can't save log entry: " + entry.getMessage());
182         }
183         uiUpdater.log(entry);
184     }
185     
186     private void jobStarted() {
187         taskList.addTask(this);
188         if (detail.getStartDate() == null) {
189             detail.setStartDate(new Date JavaDoc());
190             logInfo("Job started");
191         }
192         else {
193             logInfo("Job resumed");
194         }
195         detail.setState(JobDetail.STARTED);
196         dao.updateJobDetail(detail);
197         uiUpdater.updateJob(detail);
198     }
199     
200     private void jobStoped(boolean completed) {
201         taskList.removeTask(this);
202         if (completed) {
203             job.tearDown(detail.getObjectId());
204             detail.setState(JobDetail.COMPLETED);
205             detail.setEndDate(new Date JavaDoc());
206             logInfo("Job completed");
207         }
208         else {
209             detail.setState(JobDetail.INTERRUPTED);
210         }
211         dao.updateJobDetail(detail);
212         uiUpdater.updateJob(detail);
213     }
214     
215 }
Popular Tags