2012-03-26 19 views
1

我在Spring Batch的一個新手。我有繼承了一個使用Spring Batch實現的批處理。 這個效果很好,除了我會嘗試描述的一件事。Spring Batch的:一直在尋找的文件與MultiResourceItemReader

我啓動parseJob和,當它讀取XML在豆parsingStepReader處理, read()方法時被調用始終。

目錄* path_to_xml *只包含一個XML,調用read()並返回解析的XML,處理OK。然後,再次調用read()方法,返回一個空對象,然後再次調用,返回null ...等等。

調試時,MultiResourceItemReader 讀取方法嘗試讀取,不讀取任何東西(所有資源已經被讀取),遞增currentResources並返回null。

我已經readed你對這項工作停止時,讀寫器返回一個空的對象,但閱讀方法返回null,再而三讀... 我改變了重新啓動的假,但不起作用。

的工作在Linux中,批處理模式推出,與由於這一問題org.springframework.batch.core.launch.support.CommandLineJobRunner ,即啓動作業沒有完成.SH和資源都很忙。

我怎樣才能避免這種情況,或者當資源(XML)輸入目錄已經處理停止作業? 任何幫助將不勝感激。最好的祝福。

豆類文件和Java類件連接

<batch:job id="parseJob" restartable="true" incrementer="jobParametersIncrementer"> 
    <batch:flow parent="parseFlow"/> 
    <batch:flow .../> 
    <batch:flow .../> 
</batch:job> 

<batch:flow id="parseFlow"> 
    <batch:step id="parsingStep"> 
     <batch:tasklet start-limit="100" allow-start-if-complete="true" transaction-manager="..." task-executor="taskExecutor" throttle-limit="$..."> 
      <batch:chunk reader="parsingStepReader" writer="..." processor="..." commit-interval="..." skip-limit="10000000"> 
       <batch:skippable-exception-classes> 
        <batch:include class="java.lang.Exception" /> 
       </batch:skippable-exception-classes> 
      </batch:chunk> 
      <batch:listeners> 
       <batch:listener ref="iwListener" /> 
       <batch:listener ref="mySkipListener" /> 
       <batch:listener ref="myStep1Listener" /> 
      </batch:listeners> 
      <batch:no-rollback-exception-classes> 
       <batch:include class="java.lang.Exception" /> 
      </batch:no-rollback-exception-classes> 
     </batch:tasklet> 
    </batch:step> 
</batch:flow> 

<!-- --> 

<bean id="bpfReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="prototype"> 
    <property name="fragmentRootElementName" value="..." /> 
    <property name="unmarshaller" ref="..." /> 
    <property name="strict" value="false" /> 
</bean> 

<bean id="multiresourceItemReader" class="...SyncMultiResourceItemReader" abstract="true"> 
    <property name="strict" value="false" /> 
    <property name="delegate" ref="bpfReader" /> 
</bean> 

<bean id="parsingStepReader" parent="multiresourceItemReader" scope="step"> 
    <property name="resources" value="<path_to_xml>" /> 
</bean> 

和閱讀器類:

public class SyncMultiResourceItemReader<T> extends MultiResourceItemReader<T> { 
    . . . 

    @Override 
    public T read() throws Exception, UnexpectedInputException, ParseException { 
     synchronized (this) { 
      return super.read(); 
     } 
    } 

    . . . 
} 

UPDATE:解決方案通過@vsingh提出完美的作品。一旦選擇輸入元素,它必須從輸入中刪除。我不知道爲什麼,但類org.springframework.batch.item.file.MultiResourceItemReader不能正常工作,特別是在輸入錯誤。

我希望這會有所幫助。此致敬意

+0

你能更準確的問題:'MultiResourceItemReader#read()'返回null,因爲輸入資源被鎖定?或者Spring Batch繼續調用'read()'方法,儘管它剛剛返回'null'? – 2012-04-07 22:31:25

+0

@dma_k,當你闡明問題時,錯誤的問題是你的第二種方式,所以'read()'返回null,並且被一次又一次地調用。我認爲解決方案就像[this](http://stackoverflow.com/questions/7781012/spring-batch-stax-xml-reading-job-is-not-ending-when-out-of-input/10009614 #10009614),但目前還不清楚。謝謝@dma_k – 2012-04-09 05:09:29

回答

2

read方法將讀取數據,存儲在類級別並將其傳遞給寫入方法。 我會給你我們是怎麼做的

爲如

@Override 
public Long read() throws Exception, UnexpectedInputException, 
     ParseException, NonTransientResourceException { 
    synchronized (this.myIds) { 
     if (!this.myIds.isEmpty()) { 
      return this.myIds.remove(0); 
     } 
     return null; 
    } 
} 

myIds的一個例子是在類級別列表

這個列表是在之前的步驟方法

@Override 
public void beforeStep(final StepExecution stepExec) { 
    this.stepExecution = stepExec; 
      // read the ids from service and set at class level 

} 
+0

我會嘗試你的解決方案,在此先感謝 – 2012-03-30 05:08:55

+0

這有效,@vsingh謝謝。最好的問候 – 2012-04-16 05:41:26

+0

如果解決方案的作品,你可以選擇我的答案作爲正確的答案:) – vsingh 2013-08-08 13:31:50

0
填充

@vsingh建議的解決方案非常完美。一旦選擇輸入元素,它必須從輸入中刪除。我不知道爲什麼,但是類org.springframework.batch.item.file。MultiResourceItemReader無法正常工作,特別是在輸入錯誤時。

我希望這會有所幫助。最好的問候