MULTI FILE READ
-
Batch Job Xml Sample
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch-int="http://www.springframework.org/schema/batch-integration"
xsi:schemaLocation="http://www.bankwareglobal.com/schema/batchex http://www.bankwareglobal.com/schema/batchex/spring-batch-ex.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch-integration http://www.springframework.org/schema/batch-integration/spring-batch-integration.xsd">
<import resource="classpath:JobConfig.xml"/>
<job-component xmlns="http://www.bankwareglobal.com/schema/batchex" id="jobcomp" with-dependon="true">
<base-package name="bxm.dft.smp.batch.bean">
<include name="MSmpMultiFileRead*Btch"/>
</base-package>
</job-component>
<!--
* Batch job : MULTI FILE READ sample
* Batch step
- JSmpMultiFileRead100 : MULTI FILE READ sample initialization process
- JSmpMultiFileRead200 : Directly opens two files and performs Insert into DB
-->
<job id="JSmpMultiFileRead" xmlns="http://www.bankwareglobal.com/schema/batchex">
<step id="JSmpMultiFileRead100" next="JSmpMultiFileRead200" parent="parentStep">
<tasklet ref="MSmpMultiFileReadInitBtch"/>
</step>
<step id="JSmpMultiFileRead200" parent="parentStep">
<tasklet>
<chunk reader="MSmpMultiFileReadBtch" processor="MSmpMultiFileReadBtch" writer="MSmpMultiFileReadBtch"/>
</tasklet>
</step>
</job>
</beans>
-
Batch source code Sample
@BxmBean("MSmpMultiFileReadBtch")
@Scope("step")
@BxmCategory(logicalName = "MULTI FILE READ sample")
public class MSmpMultiFileReadBtch implements ItemStream, ItemReader<MSmpMultiFileReadBtch01Dto>,
ItemProcessor<MSmpMultiFileReadBtch01Dto, MSmpMultiFileReadBtch03Dto>, ItemWriter<MSmpMultiFileReadBtch03Dto> {
final Logger logger = LoggerFactory.getLogger(this.getClass());
private DSmpEmpTmp001 dSmpEmpTmp100;
private FixedFileReader<MSmpMultiFileReadBtch01Dto> empInfoFixedReader = null;
private DelimitedFileReader<MSmpMultiFileReadBtch02Dto> empInfoDelimitedReader = null;
private int currentEmpNo;
/
* open
* - A method that must be implemented for initialization, and is called exactly once by the framework
* before the step starts.
*/
@Override
@BxmCategory(logicalName = "open : sample employee information File Open process")
public void open(ExecutionContext executionContext) throws ItemStreamException {
/
* Gets the input Order Date from the batch input arguments.
*/
String oDate = DefaultBatchApplicationContext.getJobParameter("oDate");
/
* Opens the sample employee1 information Fixed File.
*/
String empInfoFixedFileName = "/data1/prod/bxm400/dat/fixed_file_" + oDate + ".txt";
empInfoFixedReader = DefaultFileUtils.getFixedFileReader(empInfoFixedFileName, MSmpMultiFileReadBtch01Dto.class,
"UTF-8");
empInfoFixedReader.open(executionContext); // File Open
/
* Opens the sample employee2 information Delimited File.
*/
String empInfoDelimitedFileName = "/data1/prod/bxm400/dat/delimited_file_" + oDate + ".txt";
empInfoDelimitedReader = DefaultFileUtils.getDelimitedFileReader(empInfoDelimitedFileName,
MSmpMultiFileReadBtch02Dto.class, ",", "UTF-8");
empInfoDelimitedReader.open(executionContext); // File Open
}
/
* read
* - Performs the role of reading target data per record from DB or file.
*/
@Override
@BxmCategory(logicalName = "read : sample employee information Read")
public MSmpMultiFileReadBtch01Dto read()
throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
/
* Reads and returns one record of sample employee information at a time.
* - If empInfoReadr returns null, the batch ends.
*/
MSmpMultiFileReadBtch01Dto out = empInfoFixedReader.read();
return out;
}
/
* process
* - Performs the role of processing target data per record according to business requirements.
*/
@Override
@BxmCategory(logicalName = "process : sample employee information processing")
public MSmpMultiFileReadBtch03Dto process(MSmpMultiFileReadBtch01Dto in) throws Exception {
MSmpMultiFileReadBtch03Dto out = null;
if (dSmpEmpTmp100 == null) {
dSmpEmpTmp100 = DefaultApplicationContext.getBean(DSmpEmpTmp001.class);
}
/
* For the sample employee1 information file, compares it with the sample employee2 information file
* to check whether employee information exists in the sample employee2 information file.
* - If employee information exists in the employee2 information file, passes data to writer.
* - If employee information does not exist in the employee2 information file, returns null.
*/
if (in.getFeduEmpNo() != currentEmpNo) {
MSmpMultiFileReadBtch02Dto mSmpMultiFileReadBtch02Dto = null;
while mSmpMultiFileReadBtch02Dto = empInfoDelimitedReader.read( != null) {
if (in.getFeduEmpNo() <= mSmpMultiFileReadBtch02Dto.getFeduEmpNo()) {
currentEmpNo = mSmpMultiFileReadBtch02Dto.getFeduEmpNo();
break;
}
}
}
if (in.getFeduEmpNo() != currentEmpNo) {
return null;
}
/
* Sets the Item to be passed to write.
*/
out = new MSmpMultiFileReadBtch03Dto();
DSmpEmpTmp001Dto dSmpEmpTmp100insert02InDto = new DSmpEmpTmp001Dto();
dSmpEmpTmp100insert02InDto.setFeduEmpNo(in.getFeduEmpNo()); // set [FW sample employee number]
dSmpEmpTmp100insert02InDto.setFeduEmpNm(in.getFeduEmpNm()); // set [FW sample employee name]
dSmpEmpTmp100insert02InDto.setFeduOccpNm(in.getFeduOccpNm()); // set [FW sample occupation name]
dSmpEmpTmp100insert02InDto.setFeduMngrEmpNo(in.getFeduMngrEmpNo()); // set [FW sample manager employee number]
dSmpEmpTmp100insert02InDto.setFeduHireDt(in.getFeduHireDt()); // set [FW sample hire date]
dSmpEmpTmp100insert02InDto.setFeduPayAmt(in.getFeduPayAmt()); // set [FW sample salary amount]
dSmpEmpTmp100insert02InDto.setFeduDeptNo(in.getFeduDeptNo()); // set [FW sample department number]
out.setGrid01(dSmpEmpTmp100insert02InDto);
return out;
}
/
* write
* - Performs the role of writing processed result data to DB or file in commit units.
*/
@Override
@BxmCategory(logicalName = "write : sample employee information processing")
public void write(List<? extends MSmpMultiFileReadBtch03Dto> in) throws Exception {
/
* Loops through the Items passed as a List and sets the DTOs to be Inserted into DB as a List.
*/
List<DSmpEmpTmp001Dto> dSmpEmpTmp100insert02InDtos = new ArrayList<DSmpEmpTmp001Dto>();
for (MSmpMultiFileReadBtch03Dto mSmpMultiFileReadBtch03Dto : in) {
dSmpEmpTmp100insert02InDtos.add(mSmpMultiFileReadBtch03Dto.getGrid01());
}
/
* insert processing
* - Note 1 : For C/U/D processing with CONNECTED_BATCH, the query is executed in bulk at the Commit point.
* - Note 2 : For C/U/D processing with CONNECTED_BATCH, it is executed at the Commit point, so the return value
* is an arbitrary negative value (meaningless).
*/
dSmpEmpTmp100.insert02(dSmpEmpTmp100insert02InDtos);
}
/
* update
* - A method that must be implemented to record progress status, and is called by the framework at each
* commit section.
*/
@Override
@BxmCategory(logicalName = "update")
public void update(ExecutionContext executionContext) throws ItemStreamException {
if (empInfoFixedReader != null)
empInfoFixedReader.update(executionContext);
if (empInfoDelimitedReader != null)
empInfoDelimitedReader.update(executionContext);
}
/
* close
* - A method that must be implemented for resource cleanup, and is called by the framework when the Step
* is completed.
*/
@Override
@BxmCategory(logicalName = "close")
public void close() throws ItemStreamException {
if (empInfoFixedReader != null)
empInfoFixedReader.close();
if (empInfoDelimitedReader != null)
empInfoDelimitedReader.close();
}
}