2015-01-09 194 views
5

我正在嘗試將作業參數注入到自定義的ItemReader中。我已經回顧了關於這個主題的所有StackOverflow筆記(例如:How to get access to job parameters from ItemReader, in Spring Batch?),並且我看到這是一個常見的痛點,大部分都是未解決的。我希望春天的大師(@Michael Minella任何人)都會看到這個並有一些洞察力。來自Spring批處理作業參數

儘管沒有代碼或配置更改,但我仍然確定作業參數可用於大約10次運行中的一次。這是一個隨機成功的案例,而不是隨機的失敗,所以很難追查到。

我用調試器挖入了Spring代碼,並確定當這個失敗時,名爲jobParameters的Bean在注入發生時在Spring中註冊。

我使用Spring 4.1.4彈簧批次3.0.2和彈簧數據的JPA 1.7.1和彈簧數據公地1.9.1,在Java運行8

的Java

@Component("sourceSelectionReader") 
@Scope("step") 
public class SourceSelectionReaderImpl 
implements ItemReader<MyThing> { 
    private Map<String,Object> jobParameters; 

// ... snip ... 


    @Autowired 
    @Lazy 
    @Qualifier(value="#{jobParameters}") 
    public void setJobParameters(Map<String, Object> jobParameters) { 
     this.jobParameters = jobParameters; 
    } 
} 

工作啓動參數:

launch-context.xml job1 jobid(long)=1 

推出-context.xml中(減去絨毛):

<context:property-placeholder location="classpath:batch.properties" /> 

<context:component-scan base-package="com.maxis.maximo.ilm" /> 

<jdbc:initialize-database data-source="myDataSource" enabled="false"> 
    <jdbc:script location="${batch.schema.script}" /> 
</jdbc:initialize-database> 

<batch:job-repository id="jobRepository" 
    data-source="myDataSource" 
    transaction-manager="transactionManager" 
    isolation-level-for-create="DEFAULT" 
    max-varchar-length="1000"/> 

<import resource="classpath:/META-INF/spring/module-context.xml" /> 

模塊-context.xml中(減去絨毛):

<description>Example job to get you started. It provides a skeleton for a typical batch application.</description> 

<import resource="classpath:/META-INF/spring/hibernate-context.xml"/> 
<import resource="classpath:/META-INF/spring/myapp-context.xml"/> 

<context:component-scan base-package="com.me" /> 
<bean class="org.springframework.batch.core.scope.StepScope" /> 

<batch:job id="job1"> 
    <batch:step id="step0002" >    
     <batch:tasklet transaction-manager="transactionManager" start-limit="100" > 
      <batch:chunk reader="sourceSelectionReader" writer="selectedDataWriter" commit-interval="1" /> 
     </batch:tasklet> 
    </batch:step> 
</batch:job> 
+0

你如何啓動這項工作? – wassgren

+0

而且,你爲什麼使用'@ Lazy'進行注射?這是你的任務需要嗎? – wassgren

+0

現在,我正在從我的eclipse IDE中啓動該作業。它將在命令行啓動時啓動。 我使用@Lazy,因爲它允許我避開這個問題,並繼續進行真正的項目,等待解決。還有其他方法可以將這些參數導入到bean中,但是從長遠來看,它們會產生支持問題。 –

回答

2

的重要步驟獲得作業參數來工作是定義StepScope bean,並確保您的閱讀器是@StepScope組件。

我會嘗試以下方法:

首先確保有限定的臺階豆。這是很好的使用Java配置設置:

@Configuration 
public class JobFrameworkConfig { 
    @Bean 
    public static StepScope scope() { 
     return new StepScope(); 
    } 
    // jobRegistry, transactionManager etc... 
} 

然後,請確保您的bean是通過使用@StepScope -annotation(幾乎在你的例子)步驟作用域。注入不是@Lazy@Value

@Component("sourceSelectionReader") 
@StepScope // required, also works with @Scope("step") 
public class SourceSelectionReaderImpl implements ItemReader<MyThing> { 
    private final long myParam; 

    // Not lazy, specified param name for the jobParameters 
    @Autowired 
    public SourceSelectionReaderImpl(@Value("#{jobParameters['myParam']}") final long myParam) { 
     this.myParam = myParam; 
    } 

    // the rest of the reader... 
} 
+0

使用@Scope(「step」) - bean已初始化,但是步驟作用域參數不可用。 使用@StepScope,豆初始化失敗: 不能繼承final類類com.sun.proxy $ Proxy42 \t在org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)..。etc –

+0

@ pojo人,奇怪的失敗。請注意,有一個名爲「StepScope」的類和一個名爲'@ StepScope'的註釋。不方便;) – wassgren

+0

在另一個線程上,討論了使用StepScope的意外代理。 Per @Mike Manella(Spring批次技術主管)StepScope是範圍(「步驟」,...)的快捷方便。解決方案是使用正確的參數理解和應用範圍註釋當我有機會時,我將應用這些並報告結果。 –

-1

儘量@Component後添加@DependsOn( 「jobParameters」)( 「sourceSelectionReader」)

+0

這只是確認沒有名爲jobParameters的bean。謝謝 –

相關問題