2013-05-15 191 views
5

我正在開發一個使用MultiResourcePartitioner並行處理多個輸入文件的Spring Batch作業。在ItemProcessor中,我需要獲取當前輸入文件中的記錄數。我從臺階背景下獲取當前文件的名稱和讀取文件中的行數:Spring批處理:獲取分步處理器中的步驟上下文

StepSynchronizationManager.register(stepExecution); 
StepContext stepContext = StepSynchronizationManager.getContext(); 
StepSynchronizationManager.close(); 
log.trace("stepContext: " + stepContext.getStepExecution().getExecutionContext().entrySet().toString()); 
... 
UrlResource currentFile = new UrlResource(stepContext.getStepExecution().getExecutionContext().getString("fileName")); 

這一切似乎工作,但在處理器線程訪問步驟背景下,當我得到異常:

2013-05-15 11:44:35,178 DEBUG [org.springframework.batch.core.step.tasklet.TaskletStep] <taskExecutor-3> - Rollback for RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.processor': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope 
2013-05-15 11:44:35,194 DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] <taskExecutor-3> - Returning JDBC Connection to DataSource 
2013-05-15 11:44:35,194 DEBUG [org.springframework.batch.repeat.support.RepeatTemplate] <taskExecutor-3> - Handling exception: org.springframework.beans.factory.BeanCreationException, caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.processor': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope 
2013-05-15 11:44:35,194 DEBUG [org.springframework.batch.repeat.support.RepeatTemplate] <taskExecutor-3> - Handling fatal exception explicitly (rethrowing first of 1): org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.processor': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope 
2013-05-15 11:44:35,210 ERROR [org.springframework.batch.core.step.AbstractStep] <taskExecutor-3> - Encountered an error executing the step 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.processor': Scope 'step' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No context holder available for step scope 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:341) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192) 
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:182) 
    at $Proxy14.process(Unknown Source) 
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:125) 
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:291) 
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:190) 
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:74) 
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:386) 
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130) 
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:264) 
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:76) 
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367) 
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:214) 
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143) 
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:250) 
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195) 
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:120) 
    at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:118) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: java.lang.IllegalStateException: No context holder available for step scope 
    at org.springframework.batch.core.scope.StepScope.getContext(StepScope.java:197) 
    at org.springframework.batch.core.scope.StepScope.get(StepScope.java:139) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327) 
    ... 24 more 

有沒有辦法從分區作業的處理器中獲取當前輸入文件名?

下面是相關配置:

<batch:job id="acct"> 
    <batch:step id="init" parent="abstractStep" next="processStep"> 
     <batch:tasklet ref="fileDeleteTasklet" /> 
    </batch:step> 
    <batch:step id="processStep"> 
     <batch:partition step="processInput" partitioner="partitioner"> 
      <batch:handler grid-size="2" task-executor="taskExecutor" /> 
     </batch:partition> 
    </batch:step> 
</batch:job> 

<batch:step id="processInput"> 
    <batch:tasklet> 
     <batch:chunk reader="reader" processor="processor" writer="writer" commit-interval="3"> 
      <batch:streams> 
       <batch:stream ref="reader"/> 
       <batch:stream ref="flatFileItemWriter"/> 
      </batch:streams> 
     </batch:chunk> 
    </batch:tasklet> 
</batch:step> 

<bean id="partitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner" scope="step"> 
    <property name="keyName" value="fileName"/> 
    <property name="resources" value="file:#{jobParameters['inputFilePattern']}"/> 
</bean> 

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> 
    <property name="corePoolSize" value="5"/> 
    <property name="maxPoolSize" value="5"/> 
</bean> 

<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"> 
    <property name="resource" value="#{stepExecutionContext[fileName]}" /> 
    <property name="lineMapper"> 
     <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> 
      <property name="lineTokenizer"> 
       <bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer"> 
        <property name="names" value="recordType,reserved,aCode,bCode,acctNumber,idType,cCode" /> 
        <property name="columns" value="1,2,3-6,7-9,10-15,16,17-19" /> 
        <property name="strict" value="false" /> 
       </bean> 
      </property> 
      <property name="fieldSetMapper"> 
       <bean class="com.example.batch.acct.AcctInputFieldSetMapper" /> 
      </property> 
     </bean> 
    </property> 
</bean> 

<bean id="processor" class="com.example.batch.acct.AcctProcessor" scope="step"> 
    <property name="soapClient" ref="soapClient" /> 
    <property name="maxNumIds" value="${batch.soap.numberOfIds}" /> 
    <aop:scoped-proxy /> 
</bean> 
+0

您使用的是什麼版本的Spring和Spring Batch? – Cygnusx1

+0

我使用Spring 3.0.6和Spring Batch 2.1.9。 – iguanodon

回答

1

回答的Spring Batch forum

ItemProcessor接口不公開的方式來訪問 StepContext,所以你需要自己把它注射:

<bean id="itemProcessor" class="com.myApp.MyItemProcessor" scope="step"> 
    <property name="stepContext" value="#{stepExecutionContext}"/> 
</bean> 

假設你的專業cessor類有一個名爲 stepContext的暴露屬性。

- Michael MinellaSpring Batch Lead

+1

您可能想要接受您的答案。閱讀http://stackoverflow.com/help/self-answer – falsarella

0
<bean id="itemProcessor" class="com.myApp.MyItemProcessor" scope="step"> 
    <property name="stepContext" value="#{stepExecutionContext}"/> 
</bean> 

在當stepContex是一種類型的org.springframework.batch.core.scope.context.StepContext這種情況下,如何表明,這種屬性的名稱,我得到異常:

組織.springframework.beans.factory.BeanCreationException:錯誤 使用名稱創建bean 'sco pedTarget.itemProcessor'定義在 類路徑資源[META-INF/jobs/Job.xml]中: bean的初始化失敗;嵌套異常是 org.springframework.beans.ConversionNotSupportedException:未能 將類型'java.util.Collections $ UnmodifiableMap' 的屬性值轉換爲所需類型 'org.springframework.batch.core.scope.context.StepContext'for property'stepContext';嵌套異常是 java.lang.IllegalStateException:無法將類型 'java.util.Collections $ UnmodifiableMap'的值轉換爲所需的類型 'org.springframework.batch.core.scope.context.StepContext'for property'stepContext' :沒有匹配的編輯或轉換戰略 發現

我需要訪問jobExecutionIdItemProcessor,所以我已經改變了這個配置:

<bean id="itemProcessor" class="com.myApp.MyItemProcessor" scope="step"> 
    <property name="stepExecution" value="#{stepExecution}"/> 
</bean> 

物業"stepExecution"是一種類型org.springframework.batch.core.StepExecution

相關問題