FileWriterPattern
The FileWriterPattern pattern is used to write text data to a file.
Supporting classes
The TextFileWriter class provides the logic to open and write string data to the given file. The file is opened either in append or overwrite mode, depending on the properties specified. The file is always opened in append mode during a job restart.
There are two ways to set the TextFileWriter filename dynamically from JobStepContext:
- Use a single well known job-level property, defined by Compute Grid. In the following example:
JobStepContext ctx = JobStepContextMgr.getContext(); Properties jobProps = ctx.getJobLevelProperties(); jobProps.set(TextFileWriter. FILENAME_JOBSTEPCONTEXT_DEFAULT_PROPERTY_NAME, "/my/fileName");if we set the "/my/fileName" value prior to TextFileWriter initialization then this value is used as the filename by TextFileWriter.- Use our own custom property, defined by a BDS writer-level xJCL property. Use this method if we have two writers and each needs a different filename.
In this method, use the BDS-level xJCL property named com.ibm.websphere.batch.devframework.datastreams.patterns.TextFileWriter.FILENAME.xjcl.custom.property.name to identify the JobStepContext property name. For example:
xJCL <bds> <logical-name>outputStream</logical-name> <impl-class>com.ibm.websphere.batch.devframework.datastreams.patterns.TextFileWriter</impl-class> <props> <prop name="PATTERN_IMPL_CLASS" value="mypkg.MyWriter"/> <prop name="FILENAME" value="/my/staticFileName.txt"/> <!-- This will take affect if the dynamic config isn't set. --> <prop name="AppendJobIdToFileName" value="true"/> <!-- Existing options are still usable with dynamically-determined filenames --> <prop name="debug" value="true"/> <prop name="com.ibm.websphere.batch.devframework.datastreams.patterns.TextFileWriter.FILENAME.xjcl.custom.property.name" value="my.bds.specific.property.name"/> </props> </bds> </batch-data-streams> </job-step>As in the first method, set the job-level property prior to the TextFileWriter initialization. For example:
JobStepContext ctx = JobStepContextMgr.getContext(); Properties jobProps = ctx.getJobLevelProperties(); jobProps.set("my.bds.specific.property.name", "/my/fileName");
Required properties
The following properties are required for the pattern.
Property name Value PATTERN_IMPL_CLASS Class that implements the FileWriterPattern interface FILENAME Complete path to the input file
Optional properties
The following properties are optional for the pattern.
Property name Value Description debug true or false (default is false) Enable detailed tracing on this batch datastream. EnablePerformanceMeasurement true or false (default is false) Calculates the total time spent in the batch data-streams and the processRecord method, if we are using the GenericXDBatchStep. EnableDetailedPerformanceMeasurement true or false (default is false) Provides a more detailed breakdown of time spent in each method of the batch data-streams. file.encoding Encoding of the file For example, 8859_1 AppendJobldToFileName true or false (default is false) Appends the JobID to the file name before loading the file. append true or false (default is true) Determines whether to open the file in append mode. Important: During a restart, the file is always opened in append mode.
Interface definition
public interface FileWriterPattern { /** * Invoked during step setup phase * @param props */ public void initialize(Properties props); /** * This method should write the given record * object to the bufferedwriter. * @param out * @param record * @throws IOException */ public void writeRecord(BufferedWriter out, Object record) throws IOException; /** * This method is invoked only once just after the bufferedwriter * is opened. It should be used to write any header information * @param out * @throws IOException */ public void writeHeader(BufferedWriter out) throws IOException; /** * This method can be optionally called during process step to explicity * initialize and write the header. * @param header * @throws IOException */ public void writeHeader(BufferedWriter out, Object header) throws IOException; }
xJCL sample
<batch-data-streams> <bds> <logical-name>outputStream</logical-name> <props> <prop name="PATTERN_IMPL_CLASS" value="com.ibm.websphere.batch.samples.tests.bds.EchoWriter"/> <prop name="file.encoding" value="8859_1"/> <prop name="FILENAME" value="/opt/txlist.txt" /> <prop name="debug" value="true"/> </props> <impl-class>com.ibm.websphere.batch.devframework.datastreams.patterns.TextFileWriter</impl-class> </bds> </batch-data-streams>
Related:
Batch data stream framework and patterns Use the batch data stream framework JDBCReaderPattern JDBCWriterPattern ByteReaderPattern ByteWriterPattern FileReaderPattern (ZOS) RecordOrientedDatasetReaderPattern (ZOS) RecordOrientedDataSetWriterPattern JPAReaderPattern JPAWriterPattern