KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > mybatchfwk > test > BatchTest


1 /*
2  * Created on 28 mars 2006
3  */

4 package net.sf.mybatchfwk.test;
5
6 import java.util.Iterator JavaDoc;
7
8 import net.sf.mybatchfwk.BatchConfiguration;
9 import net.sf.mybatchfwk.BatchException;
10 import net.sf.mybatchfwk.ExecutionReport;
11 import net.sf.mybatchfwk.IBatch;
12 import net.sf.mybatchfwk.ITask;
13 import net.sf.mybatchfwk.ITaskExecutor;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18
19 public class BatchTest implements IBatch {
20     
21     private static final Log log = LogFactory.getLog(BatchTest.class);
22
23     public void init(BatchConfiguration config) throws BatchException {
24         log.info("###> init");
25     }
26
27     public void execute(ITaskExecutor executor) throws BatchException {
28         log.info("###> begin execute");
29         int i = 0;
30         while ((i++ < 5) && (executor.isRunning())) {
31             executor.execute(new MyTask(i));
32         }
33         log.info("###> end execute");
34     }
35
36     public void stop() throws BatchException {
37         log.info("###> stop");
38         
39     }
40
41     public void pause() throws BatchException {
42         log.info("###> pause");
43         
44     }
45
46     public void resume() throws BatchException {
47         log.info("###> resume");
48         
49     }
50
51     public void end(ExecutionReport report) throws BatchException {
52         log.info("###> end");
53         log.info("###> " + "---Execution report---");
54         log.info("Begin date: " + report.getBeginDate());
55         log.info("End date: " + report.getEndDate());
56         log.info("Number of completed tasks: " + report.getNumberOfCompletedTasks());
57         Iterator JavaDoc it = report.completedTasksIdIterator();
58         if (it != null) {
59             log.info("Completed tasks: " + buildCsvString(it));
60         }
61         log.info("Number of failed tasks: " + report.getNumberOfFailedTasks());
62         it = report.failedTasksIdIterator();
63         if (it != null) {
64             log.info("Failed tasks: " + buildCsvString(it));
65         }
66         log.info("###> " + "----------------------");
67     }
68     
69     public void endOfExecution(ITask task, Throwable JavaDoc e) {
70         if (e == null) {
71             log.info("task execution success: id='" + task.getId() + "'");
72         } else {
73             log.error("task execution failure: id='" + task.getId() + "'", e);
74         }
75     }
76
77     public void manageFatalError(ExecutionReport report, Throwable JavaDoc throwable) {
78         log.fatal("fatal error, end of the batch execution", throwable);
79     }
80     
81     private String JavaDoc buildCsvString(Iterator JavaDoc it) {
82         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
83         boolean first = true;
84         while (it.hasNext()) {
85             if (!first) {
86                 buffer.append(", ");
87             }
88             buffer.append(it.next());
89             first = false;
90         }
91         return buffer.toString();
92     }
93 }
94
Popular Tags