Java Reference
In-Depth Information
import java.util.List;
import javax.batch.api.AbstractItemWriter;
/**
*
* @author Juneau
*/
public class AcmeWriter extends AbstractItemWriter<WidgetOutputItem> {
@Override
public void writeItems(List<WidgetOutputItem> list) throws Exception {
for(WidgetOutputItem item:list){
System.out.println("Write to file:" + item.getLineText());
}
}
}
The
WidgetReportItem
and
WidgetOutputItem
objects are merely containers that hold a String of text. Below is
the implementation for
WidgetReportItem
, the
WidgetOutputItem
object is identical other than the name.
public class WidgetReportItem {
private String lineText;
public WidgetReportItem(String line){
this.lineText = line;
}
/**
* @return the lineText
*/
public String getLineText() {
return lineText;
}
/**
* @param lineText the lineText to set
*/
public void setLineText(String lineText) {
this.lineText = lineText;
}
}
When this batch job is executed, the text file is read, processed, and then specific lines of text are written to the system
log. The read and process tasks are performed as part of the first step, and then the write is processed as the second
step. To execute this batch job, the a
JobOperator
must be obtained by executing
BatchRuntime.geteJobOperator()
.
Once the
JobOperator
has been obtained, its
start
method can be invoked, passing the name of the batch XML file
for processing, along with the necessary Properties. In the example below, an EJB timer is used to schedule the batch
job that we have created in the previous code examples.