Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass values from JobExecution context to calling JobStep execution context #4524

Open
eryanv opened this issue Dec 26, 2023 · 1 comment
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature

Comments

@eryanv
Copy link

eryanv commented Dec 26, 2023

This request comes from the Stack Overflow question posed here:
https://stackoverflow.com/questions/77649370/promote-execution-context-from-jobstep-to-calling-step

When using a JobStep, there is no means to pass values from the execution context of the child job run in the JobStep back to the parent step, similar to how an ExecutionContextPromotionListener would be used to promote values from a child step back up to the parent job. This cannot be achieved in the current design, in part, because the JobExecution of the job being called is only scoped to the JobStep.doExecute method.

It would be nice to have an interface that specified actions to be performed between the child JobExecution and parent StepExecution. The JobStep.determineStepExitStatus method is one example of code that could implement this interface and the Job to Step Execution Context Promotion would be another example.

Background:

I'm developing a small report-running framework to add to an application that uses Spring Batch for a number of integration tasks. The design consists of three steps:

  1. Retrieve a Report Definition from a database table (contains a Job Name, static parameters to pass to job, list of users who receive the report) and put that into the Step/Job Execution Context. This step is accomplished via a tasklet.
  2. Use the Report Definition in the Execution context to create a JobStep. The job would perform whatever logic that needs to be done and creates a file. The name of this file would be provided in the child job's execution context to be passed to the next step in the parent job.
  3. Distribute that file out to the users (probably another tasklet).

Here's what the second step currently looks like, pre-workaround mentioned below:

@JobScope
@Bean(name="runReportStep")
Step buildRunReportStep(@Value("#{jobExecutionContext['reportDefn']}") ReportDefinition rptDfn) {
        try {
            Job job = jobRegistry.getJob(rptDfn.getJobName());
            return new StepBuilder("runReportStep", jobRepository)
                                               .job(job)
                                               .launcher(jobLauncher)
                                               .build();
        } catch (NoSuchJobException e) {
            return null;
        }
}

Alternatives considered:

  • Use an external data store (database table) to store the information.
  • Subclass JobScope, copying the doExecute method and rewriting the determineStepExitStatus to also perform the task of JobExectution -> StepExecution promotion. This is the approach I am currently using as a workaround
@eryanv eryanv added status: waiting-for-triage Issues that we did not analyse yet type: feature labels Dec 26, 2023
@B-urb
Copy link

B-urb commented Jan 19, 2024

I've been trying to achieve something similar and also found that there is no good way to achieve this without manually persisting the data in the JobSteps Job in an external table. It would be neat to have a way to copy the data from the JobSteps JobExecutionContext to the Parent's JobExecution context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage Issues that we did not analyse yet type: feature
Projects
None yet
Development

No branches or pull requests

2 participants