spring-projects / spring-projects/spring-batch

Expose contextToJobNames from DefaultJobLoader [BATCH-2864]

Open
#751 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core status: waiting-for-triage type: enhancement
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

Yanming Zhou opened BATCH-2864 and commented

I'm using AutomaticJobRegistrar to register job per xml application context

<bean id="jobLoader" class="org.springframework.batch.core.configuration.support.DefaultJobLoader"/>
<bean id="automaticJobRegistrar" class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
     <property name="applicationContextFactories">
          <bean class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
               <property name="resources" value="resources/batch/*.xml" />
          </bean>
     </property>
     <property name="jobLoader" ref="jobLoader"/>
</bean>

Currently there is no way to get bean from job context since context instances are hold by private field contextToJobNames, such feature is required by execute step via remote invocation to locate step instance, I had to use the dangerous reflection.

@Component
public class JobStepExecutor {

	@Autowired
	private ApplicationContext rootApplicationContext;

	@Autowired
	private JobLoader jobLoader;

	@Autowired
	private JobExplorer jobExplorer;

	public void execute(Long jobExecutionId, Long stepExecutionId, String stepName)
			throws JobInterruptedException, NoSuchJobException {
		String jobName = jobExplorer.getJobExecution(jobExecutionId).getJobInstance().getJobName();
		Map<ConfigurableApplicationContext, Collection<String>> contextToJobNames = ReflectionUtils
				.getFieldValue(jobLoader, "contextToJobNames");
		Step step = contextToJobNames.entrySet().stream().filter(entry -> entry.getValue().contains(jobName))
				.map(entry -> entry.getKey()).findAny().map(ctx -> ctx.getBean(stepName, Step.class))
				.orElseGet(() -> rootApplicationContext.getBean(stepName, Step.class));
		StepExecution stepExecution = jobExplorer.getStepExecution(jobExecutionId, stepExecutionId);
		step.execute(stepExecution);
	}

}


Affects: 4.2.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with DefaultJobLoader and its private contextToJobNames field, then review the JobLoader interface and surrounding configuration support classes to understand the intended public access point. The issue is done when callers can locate job contexts and their job names without reflection for the remote step execution use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.