FileWriterPattern
FileWriterPattern 模式用于将文本数据写入文件。
支持类
TextFileWriter 类提供用于打开字符串数据并将其写入给定文件的逻辑。 根据指定的属性,以附加方式或改写方式打开该文件。作业重新启动期间,始终以附加方式打开该文件。
可以通过两种方式从 JobStepContext 动态设置 TextFileWriter
文件名:
- 使用计算网格定义的单个已知作业级别属性。在以下示例中:
如果在 TextFileWriter 初始化之前设置 "/my/fileName" 值,那么 TextFileWriter 会将此值用作文件名。JobStepContext ctx = JobStepContextMgr.getContext(); Properties jobProps = ctx.getJobLevelProperties(); jobProps.set(TextFileWriter. FILENAME_JOBSTEPCONTEXT_DEFAULT_PROPERTY_NAME, "/my/fileName");
- 根据 BDS 写程序级别 xJCL 属性定义,使用自己的定制属性。如果具有两个写程序,且每个写程序需要不同文件名,那么使用此方法。在此方法中,使用名为 com.ibm.websphere.batch.devframework.datastreams.patterns.TextFileWriter.FILENAME.xjcl.custom.property.name 的 BDS 级别 xJCL 属性以标识 JobStepContext 属性名称。例如:
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>
使用第一种方法时,在 TextFileWriter 初始化之前设置作业级别属性。例如:JobStepContext ctx = JobStepContextMgr.getContext(); Properties jobProps = ctx.getJobLevelProperties(); jobProps.set("my.bds.specific.property.name", "/my/fileName");
必需属性
以下属性对于该模式是必需的。
属性名称 | 值 |
---|---|
PATTERN_IMPL_CLASS | 实现 FileWriterPattern 接口的类 |
FILENAME | 输入文件的完整路径 |
可选属性
以下属性对于该模式是可选的。
属性名称 | 值 | 描述 |
---|---|---|
debug | true 或 false(缺省值为 false) | 在此批处理数据流上启用详细跟踪。 |
EnablePerformanceMeasurement | true 或 false(缺省值为 false) | 如果正使用 GenericXDBatchStep,那么计算批处理数据流和 processRecord 方法中所花的总时间。 |
EnableDetailedPerformanceMeasurement | true 或 false(缺省值为 false) | 提供在批处理数据流的每个方法中花费的更为详细的时间细目。 |
file.encoding | 文件的编码 | 例如,8859_1 |
AppendJobldToFileName | true 或 false(缺省值为 false) | 加载文件之前,将作业标识附加到文件名。 |
append | true 或 false(缺省值为 true) | 确定是否以附加方式打开文件。 要点: 重新启动期间,始终以附加方式打开文件。
|
接口定义
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 样本
<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>