Skip to content

Conversation

ArtyomGabeev
Copy link
Contributor

Please refer for more details: https://jira.spring.io/browse/BATCH-2747

@pivotal-issuemaster
Copy link

@ArtyomGabeev Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@ArtyomGabeev Thank you for signing the Contributor License Agreement!

@mminella
Copy link
Member

I made a comment on this issue. Let's discuss there before merging this PR.

@ArtyomGabeev
Copy link
Contributor Author

In general step can be annotated with @JobScope in order to have access to jobParameters.

@mminella , you right, it was my mistake when I mentioned StepScope in this issue, because it doesn't make sense.
But this fix is still reasonable if you want to create a JobScoped step, and pass some parameters (like chunk size for example)

BTW: I and original author have an issue with JobScoped step.

@alex2chen
Copy link

alex2chen commented Dec 12, 2018

   @Bean(EXPORT_CSV_NAME)
    public Job exportCsvJob(@Qualifier(STEP_NAME_EXPORTCSV) Step exportCsv) {
        return jobBuilders
                .get(EXPORT_CSV_NAME)
                .incrementer(new RunIdIncrementer())
                .repository(jobRepository)
                .start(initParamsStep())
                .next(takeCountStep())
                .next(exportCsv)
                .build();
    }

me too!
3.0.7.RELEASE!

@dongdongqin
Copy link

I have a way to use jobscope or stepscope for FlowBuilder.

please see the configuration below:

/**
* setlStatJob
*
* @param jobBuilderFactory
* @param splitFlow
* @param ichgStatTaskletMergedStep
* @param ichgSetlStatClearStep
* @return Job
*/
@bean(name = "singleInterchangeSettlementStatisticJob")
public Job setlStatJob(
JobBuilderFactory jobBuilderFactory,
Flow splitFlow,
Step ichgStatTaskletMergedStep,
Step ichgSetlStatClearStep) {

    return jobBuilderFactory.get(JobEnum.SNGL_ICHG_SETL_STAT.getJobName())
            .incrementer(new RunIdIncrementer())
            .start(splitFlow)
            .next(ichgStatTaskletMergedStep)
            .next(ichgSetlStatClearStep)

// .next(snglSetlStatFeedStep)
.build()
.build();

}

/**
 * parallel step
 *
 * @param financeStatFlow
 * @param nonFinanceStatFlow
 * @return Flow
 */
@Bean
public Flow splitFlow(Flow financeStatFlow,
                      Flow nonFinanceStatFlow) {

    return new FlowBuilder<SimpleFlow>("ichgStatFlow")
            .split(taskExecutor())
            .add(financeStatFlow, nonFinanceStatFlow)
            .build();
}

/**
 * finance Stat Flow
 *
 * @param snglFinclSetlStatMasterStep
 * @return Flow
 */
@JobScope
@Bean
public Flow financeStatFlow(Step snglFinclSetlStatMasterStep) {

    return new FlowBuilder<SimpleFlow>("financeStatFlow")
            .start(snglFinclSetlStatMasterStep)
            .build();
}

/**
 * non Finance Stat Flow
 *
 * @param snglNonFinclSetlStatMasterStep
 * @return Flow
 */
@JobScope
@Bean
public Flow nonFinanceStatFlow(Step snglNonFinclSetlStatMasterStep) {
    return new FlowBuilder<SimpleFlow>("nonFinanceStatFlow")
            .start(snglNonFinclSetlStatMasterStep)
            .build();
}

@Bean
public SimpleAsyncTaskExecutor taskExecutor() {
    return new SimpleAsyncTaskExecutor() {
        @Override
        protected void doExecute(Runnable task) {
            //gets the jobExecution of the configuration thread
            JobExecution jobExecution = JobSynchronizationManager.getContext().getJobExecution();

            super.doExecute(task );

            super.doExecute(new Runnable() {
                @Override
                public void run() {
                    JobSynchronizationManager.register(jobExecution);
                    try {
                        task.run();
                    } finally {
                        JobSynchronizationManager.release();
                    }
                }
            });
        }
    };
}

the first:
we need to add the public SimpleAsyncTaskExecutor taskExecutor() bean, because jobcontext is used under thread local.

the second:

do not use @JobScope under method splitFlow, use @JobScope under financeStatFlow and nonFinanceStatFlow. because delay initialise bean.

@fmbenhassine
Copy link
Contributor

LGTM. Rebased and merged as 0c50079. Thank you for your contribution!

@fmbenhassine fmbenhassine added the has: backports Legacy label from JIRA. Superseded by "for: backport-to-x.x.x" label Oct 30, 2020
@fmbenhassine fmbenhassine added this to the 4.3.0 milestone Oct 30, 2020
@fmbenhassine fmbenhassine changed the title BATCH-2747: Job scoped steps can not be used within FlowBuilder Job scoped steps can not be used within FlowBuilder Oct 30, 2020
@fmbenhassine fmbenhassine added for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line and removed has: backports Legacy label from JIRA. Superseded by "for: backport-to-x.x.x" labels Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: backport-to-4.2.x Issues that will be back-ported to the 4.2.x line in: core pr-for: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conditional (Java configured) steps cannot be scoped [BATCH-2747]
6 participants