KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.riotfamily.riot.job.Job;
27 import org.riotfamily.riot.job.JobContext;
28 import org.riotfamily.riot.job.JobDescription;
29
30 public class TestJob implements Job {
31
32     private String JavaDoc name = "Test Job";
33     
34     private String JavaDoc description;
35     
36     private int steps = 50;
37     
38     private long delay = 1000;
39     
40     private int errorAfter = 0;
41     
42     private boolean reportProgress = true;
43     
44     private int step = 1;
45     
46     public void setName(String JavaDoc name) {
47         this.name = name;
48     }
49
50     public void setDescription(String JavaDoc description) {
51         this.description = description;
52     }
53
54     public void setSteps(int steps) {
55         this.steps = steps;
56     }
57     
58     public void setDelay(long delay) {
59         this.delay = delay;
60     }
61
62     public void setReportProgress(boolean reportProgress) {
63         this.reportProgress = reportProgress;
64     }
65
66     public void setErrorAfter(int errorAfter) {
67         this.errorAfter = errorAfter;
68     }
69
70     public JobDescription setup(String JavaDoc objectId) {
71         return new JobDescription(name, description, reportProgress ? steps : 0);
72     }
73     
74     public void execute(JobContext context) {
75         while (step <= steps) {
76             context.logInfo("Performing step " + step++);
77             if (errorAfter > 0 && step % errorAfter == 0) {
78                 context.logError("A random error occured in step " + step);
79             }
80             try {
81                 Thread.sleep(delay);
82             }
83             catch (InterruptedException JavaDoc e) {
84             }
85             context.stepCompleted();
86         }
87     }
88
89     public boolean isConcurrent() {
90         return false;
91     }
92     
93     public boolean isRepeatable() {
94         return true;
95     }
96
97     public void tearDown(String JavaDoc objectId) {
98         step = 1;
99     }
100
101 }
102
Popular Tags