Skip to content

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

@nicolasduminil

Description

@nicolasduminil

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions