2015-09-18 13 views
5

私は春バッチを作ろうとしており、私はそれについての経験がありません。Springのステップ間で情報を渡しますか?

各バッチステップから情報を渡すことは可能ですか、完全に独立している必要がありますか?例えば

私は

<batch:step id="getSQLs" next="runSQLs"> 
     <batch:tasklet transaction-manager="TransactionManager" 
      ref="runGetSQLs" /> 
    </batch:step> 

    <batch:step id="runSQLs"> 
     <batch:tasklet transaction-manager="TransactionManager" 
      ref="runRunSQLs" /> 
    </batch:step> 

そしてgetSQLsを持っている場合は、String型のリストを生成するクラスを実行Beanをトリガします。 runSQLによってトリガーされたBeanのリストを参照することは可能ですか? (右の用語ではないかもしれない「トリガー」私はあなたが私が何を意味するか知っていると思う)

UPDATE:

<bean id="runGetSQLs" class="myTask" 
    scope="step"> 
    <property name="filePath" value="C:\Users\username\Desktop\sample.txt" /> 
</bean> 

この方法を実行MYTASKクラスをトリガー: だからgetSQLsステップはこのBeanをトリガー

@Override 
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { 

    ExecutionContext stepContext = this.stepExecution.getExecutionContext(); 
    stepContext.put("theListKey", sourceQueries); 

    return RepeatStatus.FINISHED; 
} 

実行方法にstepExecutionを渡す必要がありますか?

答えて

6

スプリングバッチは将来のジョブステップにデータをプッシュすることをサポートしています。これはExecutionContext、より正確にはJobExecutionContextで実行できます。それは私のための究極の基準であるとしてここで私は、example from the official documentationを参照しています:

将来のステップにデータが利用できるようにするには、それが する必要がありますステップが終了した後に仕事のExecutionContextに「昇格」 。 Spring Batchは、この 目的のExecutionContextPromotionListenerを提供します。リスナーが自分のステップで構成されるべきである

、将来のものを有するものでデータを共有する:

<batch:step id="getSQLs" next="runSQLs"> 
    <batch:tasklet transaction-manager="TransactionManager" 
     ref="runGetSQLs" /> 
    <listeners> 
     <listener> 
      <beans:bean id="promotionListener" class="org.springframework.batch.core.listener.ExecutionContextPromotionListener"> 
       <beans:property name="keys" value="theListKey"/> 
      </beans:bean> 
     </listener> 
    </listeners> 
</batch:step> 

<batch:step id="runSQLs"> 
    <batch:tasklet transaction-manager="TransactionManager" 
     ref="runRunSQLs" /> 
</batch:step> 

次のようにデータがあなたの実行コードブロックから移入されるべきである:

// ... 
ExecutionContext stepContext = this.stepExecution.getExecutionContext(); 
stepContext.put("theListKey", yourList); 

その後のステップでは、このListは、@BeforeStep aと注釈されたポスト計算フックで次のように取得できます。

@BeforeStep 
public void retrieveSharedData(StepExecution stepExecution) { 
    JobExecution jobExecution = stepExecution.getJobExecution(); 
    ExecutionContext jobContext = jobExecution.getExecutionContext(); 
    this.myList = jobContext.get("theListKey"); 
} 
+0

私のコードはstepExecutionを認識していないようです。私はorg.springframework.batch.core.StepExecutionをインポートしました。私はここで何が欠けていますか? – user2665166

+0

あなたのコードブロックでポストを更新し、これまでのことを言及する必要があります。 – tmarwen

+0

が更新されました。あなたの例に合わせてステップを変更しました。 – user2665166

0

java設定方法。

ステップ1:設定ExecutionContextPromotionListenerは

@Bean 
    public ExecutionContextPromotionListener executionContextPromotionListener() 
    { 
     ExecutionContextPromotionListener executionContextPromotionListener = new ExecutionContextPromotionListener(); 
     executionContextPromotionListener.setKeys(new String[] {"MY_KEY"}); 
     return executionContextPromotionListener; 

    } 

ステップ2:
@Bean ExecutionContextPromotionListenerでステップを構成

public Step myStep() { 
     return stepBuilderFactory.get("myStep") 
       .<POJO, POJO> chunk(1000) 
       .reader(reader()     
       .processor(Processor()) 
       .writer(Writer() 
       .listener(promotionListener()) 
       .build(); 
    } 

ステップ3:プロセッサ内のデータへのアクセス

@BeforeStep 
    public void beforeStep(StepExecution stepExecution) { 
     jobExecutionContext = stepExecution.getJobExecution().getExecutionContext(); 
     jobExecutionContext.getString("MY_KEY") 
    } 

ステップ4:プロセッサ内の設定データは

@BeforeStep 
     public void beforeStep(StepExecution stepExecution) { 
      stepExecution.getJobExecution().getExecutionContext().put("MY_KEY", My_value); 
     } 
関連する問題