2015-09-18 16 views
5

Sto provando a creare un Spring Batch e non ho esperienza con esso.Passare informazioni tra i passaggi in primavera?

È possibile passare le informazioni da ogni passaggio batch o devono essere completamente indipendenti?

Per esempio se ho

<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> 

E getSQLs innesca un fagiolo che esegue una classe che genera un elenco di tipo String. È possibile fare riferimento a tale elenco per il bean attivato da runSQL? ("Innescato" non può essere il termine giusto, ma penso che tu sai cosa voglio dire)

UPDATE: Così getSQLs passo innesca questo fagiolo:

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

che innesca classe MyTask che esegue questo metodo:

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

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

    return RepeatStatus.FINISHED; 
} 

ho bisogno di passare in qualche modo stepExecution al metodo execute?

risposta

6

Spring Batch supporta i dati di spinta per le fasi di lavoro future, e ciò può essere fatto attraverso il ExecutionContext, più precisamente lo JobExecutionContext. Qui mi sto riferendo a example from the official documentation, in quanto è il riferimento ultimo per me:

Per rendere i dati disponibili per future iniziative, dovrà essere "promosso" al ExecutionContext lavoro dopo la fase è terminata . Spring Batch fornisce ExecutionContextPromotionListener per questo scopo .

L'ascoltatore deve essere configurato con il tuo passo, quello condivisione dei dati con quelli futuri:

<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> 

I dati devono essere compilati dal blocco di codice di esecuzione come segue:

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

Quindi, nei passaggi successivi, questo List può essere recuperato con un hook di calcolo post annotato con @BeforeStep a come segue:

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

Il mio codice non sembra riconoscere stepExecution. Ho importato org.springframework.batch.core.StepExecution. Cosa mi manca qui? – user2665166

+0

Dovresti piuttosto aggiornare il post con i tuoi blocchi di codice e menzionare ciò che hai fatto finora. – tmarwen

+0

Aggiornato. Ho anche cambiato il passo per abbinare il tuo esempio. – user2665166

0

java config way.

Fase 1: Configurazione ExecutionContextPromotionListener

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

    } 

Fase 2: Configurare passo con ExecutionContextPromotionListener
@Bean

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

Fase 3: Accesso ai dati in processor

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

Fase 4: i dati di impostazione in processore

@BeforeStep 
     public void beforeStep(StepExecution stepExecution) { 
      stepExecution.getJobExecution().getExecutionContext().put("MY_KEY", My_value); 
     } 
Problemi correlati