2015-01-23 2 views
1

CSV 파일을 사용하고 FAILED 작업을 다시 시작하면 StepExecutionListner 및 관련 beforeStep (....) 메서드를 사용하여 을 파일 내에 배치 할 수 있습니다. 그래서 코드는 같은 것을 볼 수 있었다 :StaxEventItemReader - 이전 작업 실행에서 처리 된 XML 조각 건너 뛰기

public void beforeStep(StepExecution stepExecution) { 

    ExecutionContext executionContext = stepExecution.getExecutionContext(); 

    if (executionContext.containsKey(getKey(LINES_READ_COUNT))) { 

     long lineCount = executionContext.getLong(getKey(LINES_READ_COUNT)); 

     LineReader reader = getReader(); 
     Object record = ""; 
     while (reader.getPosition() < lineCount && record != null) { 
      record = readLine(); 
     } 
    } 
} // Or something similar 

StaxEventItemReader 작업을 할 때 내 질문은 어떻게이 같은 일을 달성 할 것입니다?

내 batch_step_execution_context는 {"string":"StaxEventItemReader.read.count","int":6}과 비슷합니다. 그래서 제 경우에는 첫 번째 5 개의 XML 조각이 성공적으로 처리되었으며 작업을 다시 시작할 때 XML 조각 번호 6부터 처리하기 시작했습니다.

아래 설정을 사용하면 XML 파일 내에 어떻게 리더를 배치 할 수 있습니까?

<batch:job id="reportJob" restartable="true"> 
    <batch:step id="step1"> 
     <batch:tasklet> 
      <batch:chunk reader="xmlItemReader" writer="cvsFileItemWriter" processor="filterReportProcessor" 
       commit-interval="1"> 
      </batch:chunk> 
      <batch:listeners> 
       <batch:listener ref="step1Listener" /> 
      </batch:listeners> 
     </batch:tasklet> 
    </batch:step> 
</batch:job> 

<bean id="step1Listener" class="com.mkyong.listeners.Step1Listener" /> 

<bean id="filterReportProcessor" class="com.mkyong.processor.FilterReportProcessor" /> 

<bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader"> 
    <property name="fragmentRootElementName" value="record" /> 
    <property name="resource" value="classpath:xml/report.xml" /> 
    <property name="unmarshaller" ref="reportUnmarshaller" /> 
</bean> 

<!-- Read and map values to object, via jaxb2 --> 
<bean id="reportUnmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
    <property name="classesToBeBound"> 
     <list> 
      <value>com.mkyong.model.Report</value> 
     </list> 
    </property> 
</bean> 

환경 - 스프링 배치 코어-2.2.0; 스프링 코어 3.2.2

테스트 입력 파일

는 CSV 파일로 XML 파일을 변환합니다.

<company> 
    <record refId="1001"> 
     <name>mkyong</name> 
     <age>31</age> 
     <dob>31/8/1982</dob> 
     <income>200,000</income> 
    </record> 
    <record refId="1002"> 
     <name>kkwong</name> 
     <age>30</age> 
     <dob>26/7/1983</dob> 
     <income>100,999</income> 
    </record> 
    <record refId="1003"> 
     <name>joel</name> 
     <age>29</age> 
     <dob>21/8/1984</dob> 
     <income>1,000,000</income> 
    </record> 
    <record refId="1004"> 
     <name>leeyy</name> 
     <age>29</age> 
     <dob>21/3/1984</dob> 
     <income>80,000.89</income> 
    </record> 
    <record refId="1005"> 
     <name>Grant</name> 
     <age>29</age> 
     <dob>21/3/1984</dob> 
     <income>80,000.89</income> 
    </record> 
</company> 

시운전 1

입력 파일에 두 개의 레코드를 처리 한 후, I는 RuntimeException을 강제.

batch_job_execution --->> "FAILED";"FAILED";"java.lang.RuntimeException: Get me out of here! 

batch_step_execution_context --->> {"string":"StaxEventItemReader.read.count","int":2} 

Output CSV file --->> 1001,mkyong,31,31/08/1982,200000 
         1002,kkwong,30,26/07/1983,100999 

시운전 2

프로세스 모두 "잔존 데이터"그래서 기대 .... REFID = "1003"REFID = "1004"REFID = "1005"

batch_job_execution --->> "COMPLETED";"COMPLETED";"''";"2015-01-25 16:18:08.587" 

batch_step_execution_context --->> {"string":"StaxEventItemReader.read.count","int":6} 


Output CSV file --->> 1001,mkyong,31,31/08/1982,200000 
         1002,kkwong,30,26/07/1983,100999 
         1003,joel,29,21/08/1984,1000000 
         1004,leeyy,29,21/03/1984,80000.89 
         1005,Grant,29,21/03/1984,80000.89 

StaxEventItemReader는 FIL의 시작부터 읽고처럼

불행하게도 보이는 테스트 결과 e, StaxEventItemReader.read.count의 값에 기초하여 자신을 재배치하는 것이 아니라 첫 번째 테스트 후에 2로 설정됩니다.

답변

1

아무 것도 구성 할 필요가 없습니다. 이것은 이미 StaxEventItemReader의 기본 동작입니다. 그것이 열리면 단계 실행 컨텍스트의 읽기 카운트에서 스스로 재배치됩니다.

+0

일부 테스트 실행 데이터로 내 게시물을 업데이트했습니다. 불행히도 StaxEventItemReader의 기본값으로 설명하는 동작이 표시되지 않습니다. –

+0

문제가 보이지 않습니다. 중복 된 출력 레코드가 보이지 않으므로 문제가 무엇입니까? 독자가 입력 파일에 자신을 재배치하는 것처럼 출력기 자체가 출력 파일에 위치합니다. 따라서 나머지 세 개의 레코드가 출력 파일에 추가됩니다. –

+0

필자 필자는''를 사용했다. 작성자의 설정에''를 추가하면 참조하는 중복 레코드가 표시됩니다. –