spring-cloud / spring-cloud/spring-cloud-task

partitioned-batch-job raises org.springframework.cloud.task.listener.TaskExecutionException when Partitioner is @StepScope

Open
#792 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

for: team-attention
Dominant language
Java
Stars
446
Forks
310
Avg merge
18h 18m
Merged PRs (30d)
6

Description

Hello,

I modified the partitioned-batch-job sample such that the PartitonHandler class reads as follows:

  @Bean
  @StepScope
  public DeployerPartitionHandler partitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer, TaskRepository taskRepository, String stepName)
  {
    Resource resource = this.resourceLoader.getResource("...");
    DeployerPartitionHandler partitionHandler = new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, stepName);
    List<String> commandLineArgs = new ArrayList<>(3);
    commandLineArgs.add("--spring.profiles.active=worker");
    commandLineArgs.add("--spring.cloud.task.initialize-enabled=false");
    commandLineArgs.add("--spring.batch.initializer.enabled=false");
    partitionHandler.setCommandLineArgsProvider(new PassThroughCommandLineArgsProvider(commandLineArgs));
    partitionHandler.setEnvironmentVariablesProvider(new SimpleEnvironmentVariablesProvider(this.environment));
    partitionHandler.setMaxWorkers(2);
    partitionHandler.setApplicationName("PartitionedBatchJobTask");
    return partitionHandler;
  }

And I also modified the method partitonJob as follows:

@Bean
@Profile("!worker")
public Job partitionedJob(/*PartitionHandler partitionHandler*/)
{
  Random random = new Random();
  return this.jobBuilderFactory.get("partitionedJob" + random.nextInt())
    .start(step1(partitionHandler(taskLauncher, jobExplorer, taskRepository, "workerStep")))
    .build();
}

This is in order to use multiple steps and, hence, to be able to pass the step name as an input argument of the partitinHandler() method.

Running the job raises the following exception:

org.springframework.context.ApplicationContextException: Failed to start bean 'taskLifecycleListener'; nested exception is 
org.springframework.cloud.task.listener.TaskExecutionException: Failed to process @BeforeTask or @AfterTask annotation 
because: Error creating bean with name 'scopedTarget.partitionHandler': Scope 'step' is not active for the current thread; 
consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is 
java.lang.IllegalStateException: No context holder available for step scope
      at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring- context-5.3.8.jar:5.3.8]
...

I have found many similar cases on stackoverflow but no working solutions. Please advise.

Many thanks in advance.

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 the partitioned-batch-job sample and the @StepScope partitionHandler and partitionedJob methods shown in the issue. Reproduce the taskLifecycleListener failure and trace how the scoped partitionHandler is referenced when the job is created. Done means the sample supports passing the step name for multiple steps without the step-scope exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.