DB TO File(Fixed)

  • 배치 작업 Xml Sample

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:batch="http://www.springframework.org/schema/batch"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch-int="http://www.springframework.org/schema/batch-integration"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
                           http://www.springframework.org/schema/batch-integration http://www.springframework.org/schema/batch-integration/spring-batch-integration.xsd">


    <!--
        * 배치작업 : DB TO FIXED 파일 샘플
        * 배치스텝
            - sample-db-to-fixed-001 : DB TO FIXED 파일 처리
     -->
    <job id="sample-db-to-fixed" xmlns="http://www.springframework.org/schema/batch">
        <step id="sample-db-to-fixed-001">
            <tasklet>
                <chunk reader="SmpDbToFixedChunk" processor="SmpDbToFixedChunk" writer="w-sample-db-to-fixed-001" />
            </tasklet>
        </step>
    </job>

    <!--
        * 샘플용직원정보 Fixed File을 Read 하기위한 bean 설정
    -->
    <bean id="w-sample-db-to-fixed-001" parent="WFix" scope="step">
        <property name="name" value="w-sample-db-to-fixed-001" />
        <property name="resource" value="file:///${prefixFilePath}/test-fixed-file-#{jobParameters['deptNo']}.txt" />
        <property name="targetType" value="sample.batch.service.dto.SmpDbToFixedChunkDto" />
    </bean>

</beans>
  • 배치 소스코드 Sample

package sample.batch.service;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import bxm.common.annotaion.BxmCategory;
import bxmc.batch.ext.context.DefaultBatchApplicationContext;
import bxmc.config.dbio.util.DasUtils;
import bxmc.core.ext.context.DefaultApplicationContext;
import sample.batch.dbio.DSmpEmpBat001;
import sample.batch.dbio.dto.DSmpEmpBat000Dto;
import sample.batch.service.dto.SmpDbToFixedChunkDto;

/**
 * 샘플 DB TO FILE(Fixed) 배치
 *
 * @author sysadmin
*/
@Scope("step")
@Service("SmpDbToFixedChunk")
@BxmCategory(logicalName="샘플 DB TO FILE(Fixed) 배치", description="샘플 DB TO FILE(Fixed) 배치")
public class SmpDbToFixedChunk implements ItemStream, ItemReader<SmpDbToFixedChunkDto>, ItemProcessor<SmpDbToFixedChunkDto, SmpDbToFixedChunkDto>{
    private Logger logger= LoggerFactory.getLogger(getClass());

    private DSmpEmpBat001 dSmpEmpBat001;

    private Iterator<DSmpEmpBat000Dto> iterator;

    private int count = 0;
    private int skipCount = 0;

    /**
     * open
     * - 초기화를 위해 구현해야 하는 Method 로서 스텝이 시작되기 전에 FW에서 최초 1번 호출된다.
     */
    @Override
    @BxmCategory(logicalName = "open : 샘플용 직원정보 Iterator 처리", description = "open : 샘플용 직원정보 Iterator 처리")
    public void open(ExecutionContext executionContext) throws ItemStreamException {

        // getBean
        dSmpEmpBat001 = DefaultApplicationContext.getBean(dSmpEmpBat001, DSmpEmpBat001.class);

        // 부서번호 파라미터 확인
        String deptNoStr = DefaultBatchApplicationContext.getJobParameter("deptNo");
        if(deptNoStr == null) {
            throw new ItemStreamException("deptNo is null or empty.");
        }

        int deptNo = Integer.valueOf(deptNoStr);

        // 부서번호에 해당하는 사원 조회
        iterator = dSmpEmpBat001.selectList01(deptNo).iterator();

    }

    /**
     * read
     * - 처리 대상 데이터를 건 별로 DB 또는 파일에서 읽어오는 역할을 수행한다.
     */
    @Override
    @BxmCategory(logicalName = "read : 샘플용 직원정보 Read", description = "read : 샘플용 직원정보를 1건씩 Read한다.")
    public SmpDbToFixedChunkDto read()
            throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        SmpDbToFixedChunkDto out = null;
        if(iterator.hasNext()) {
            out = new SmpDbToFixedChunkDto();
            DSmpEmpBat000Dto input = iterator.next();

            out.setFeduEmpNo(input.getFeduEmpNo()); // FW샘플 임직원번호
            out.setFeduEmpNm(input.getFeduEmpNm()); // FW샘플 임직원명
            out.setFeduOccpNm(input.getFeduOccpNm()); // FW샘플 직업명
            out.setFeduMngrEmpNo(input.getFeduMngrEmpNo()); // FW샘플 관리자임직원번호
            out.setFeduHireDt(input.getFeduHireDt()); // FW샘플 입사일자
            out.setFeduPayAmt(input.getFeduPayAmt()); // FW샘플 급여금액
            out.setFeduDeptNo(input.getFeduDeptNo()); // FW샘플 부서번호

            count++;
        }

        return out;
    }

    /**
     * process
     * - 처리 대상 데이터를 건 별로 업무요건에 따라 처리하는 역할을 수행한다.
     */
    @Override
    @BxmCategory(logicalName = "process : 샘플용 직원정보 처리", description = "process : 샘플용 직원정보를 1건씩 처리한다.")
    public SmpDbToFixedChunkDto process(SmpDbToFixedChunkDto in) throws Exception {
        SmpDbToFixedChunkDto out;

        /**
         *  관리자임직원번호가 없는 경우에는 null 를 return 하여 해당 Item에 대하여 Skip 처리한다.
         *  - 참고 : process에서 null을 return 하면 write로 item이 넘어가지 않는다.
         */
        if(in.getFeduMngrEmpNo() == 0)
        {
            logger.warn("임직원번호[{}]에 대한 관리자 번호가 0 입니다. Skip 처리합니다.", in.getFeduEmpNo());
            skipCount++;
            return null;
        }


        /**
         * 개별 업무로직 수행
         * ...
         * ...
         */
        out = in;

        return out;
    }

    /**
     * update
     * - 진행 상태를 기록하기 위해 구현해야 하는 Method로서 구간 별로 Commit 시에 FW에서 호출된다.
     */
    @Override
    @BxmCategory(logicalName = "update", description = "update")
    public void update(ExecutionContext executionContext) throws ItemStreamException {
        logger.info("= Count : {}, Skip Count : {}", new Object[] {count, skipCount});
    }

    /**
     * close
     * - 리소스 정리 작업을 위해 구현해야 하는 Method로서 Step 완료 시에 FW에서 호출된다.
     */
    @Override
    @BxmCategory(logicalName = "close", description = "close")
    public void close() throws ItemStreamException {

        // Close 처리
        if(iterator != null) DasUtils.closeCursor(iterator);

    }
}
SWLab Bankware Global
  • 전체
  • BXM
  • BXCM
  • BXCP
  • BXI
제품 선택 시 더 정확한 매뉴얼 가이드를 제공해드립니다.

Copyright© Bankwareglobal All Rights Reserved.