2014-10-27 80 views
0

我想將一些信息從一個步驟傳遞到批處理作業中的另一個步驟。創建BStepListener,其中存儲上下文的值,但該相同的值不會到達在另一個步驟中創建的tasklet [SendMailTask​​let]。我在哪裏失蹤?無法使用ExecutionContextPromotionListener在目標步驟中接收值

作業配置

<job id="bJob" xmlns="http://www.springframework.org/schema/batch"> 
    <step id="step1"> 
     <tasklet> 
      <chunk reader="bReader" writer="bWriter" processor="bProcessor" 
     commit-interval="10" /> 
    </tasklet> 
<batch:next on="COMPLETED" to="sendEmail"/> 
<listeners> 
     <listener ref="bStepListner"/> 
     <listener ref="bPromotionListener"/> 
</listeners> 
</step> 
<step id="sendEmail"> 
    <tasklet ref="sendMailManager"/> 
</step> 
</job> 
<bean id="bStepListner" class="com.listener.BStepListener" scope="step"/> 
<bean id="bPromotionListener" class="org...ExecutionContextPromotionListener"> 
    <property name="keys" value="msg"/> 
</bean> 
<bean id="sendMailManager" class="com.mail.SendMailTasklet" scope="step"> 

BStepListener.java

public ExitStatus afterStep(StepExecution stepExecution) { 
    System.out.println("Step Execution Listener ... after Step"); 
    String message = "A Sample message from step to step"; 
    stepExecution.getExecutionContext().put("msg", message); 
    return null; 
} 

SendMailTask​​let.java

public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) 
     throws Exception { 
    logger.info("Sending Email service...."); 

    String message = (String)chunkContext.getStepContext().getJobExecutionContext().get("msg"); 
    this.sendMail(); 
    return RepeatStatus.FINISHED; 
} 

回答

3

我認爲(我不得不仔細檢查代碼),我們不」保證訂單監聽者被調用。正因爲如此,晉升監聽者可能會在你的之前被調用。嘗試使用CompositeStepExecutionListener來包裝您的聽衆列表,以便保留順序。

你可以閱讀更多關於這裏的CompositeStepExecutionListenerhttp://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/listener/CompositeStepExecutionListener.html

+1

完美。有效。我不能投票少的名聲。 – 2014-10-28 17:58:41