Passing info between steps in Spring? [duplicate]

南笙酒味 提交于 2020-08-01 06:38:06

问题


I'm trying to make a Spring Batch and I have no experience with it.

Is it possible to pass information from each batch step or must they be completely independent?

For example if I have

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

And getSQLs triggers a bean which executes a class which generates a List of type String. Is it possible to reference that list for the bean triggered by runSQLs? ("triggered" may not be the right term but I think you know what I mean)

UPDATE: So getSQLs step triggers this bean:

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

which triggers myTask class which executes this method:

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

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

    return RepeatStatus.FINISHED;
}

Do I need to somehow pass stepExecution to the execute method?


回答1:


Spring Batch supports pushing data to future job steps, and this can be done through the ExecutionContext, more precisely the JobExecutionContext. Here I'm referring to example from the official documentation, as it is the ultimate reference for me:

To make the data available to future Steps, it will have to be "promoted" to the Job ExecutionContext after the step has finished. Spring Batch provides the ExecutionContextPromotionListener for this purpose.

The listener should be configured with your step, the one sharing data with future ones:

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

The data should be populated from your execution code block as follows:

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

Then in subsequent steps, this List can be retrieved with a post computation hook annotated with @BeforeStep a as follows:

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



回答2:


java config way.

Step 1 : Configure ExecutionContextPromotionListener

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

    }

Step 2 : Configure Step with ExecutionContextPromotionListener
@Bean

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

Step 3 : Accessing data in processor

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

Step 4 : setting data in processor

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



回答3:


I recommend to think twice in case you want to use ExecutionContext to pass information between steps. Usually it means the Job is not designed perfectly. The main idea of Spring Batch is to process HUGE amount of data. ExecutionContext used for storing information about progress of a Job/Step to reduce unnecessary work in case of failure. It is by design you can't put big data into ExectionContext. After completion of a step, you should have your information in reliably readable form - File, DB, etc. This data can be used on next steps as input. For simple jobs I would recommend to use only Job Parameters as information source.

In your case "runGetSQLs" doesn't look like a good candidate for a Step, but if you want you can implement it as a Spring bean and autowire in "runRunSQLs" step (which again is arguably good candidate for a Step). Based on your naming, runGetSQLs looks like ItemReader and runRunSQLs looks like ItemWriter. So they are parts of a step, not different steps. In this case you don't need to transfer information to other steps.



来源:https://stackoverflow.com/questions/32654896/passing-info-between-steps-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!