1

I recently update my spring boot app 2.1.9 to 2.2.0 and i'm facing a problem. When i'm calling "configprops" from actuator endpoint, an exception is throw : Scope 'job' is not active for the current thread

I reproduce the bug : https://github.com/guillaumeyan/bugspringbatch (just launch the test). Original project come from https://github.com/spring-guides/gs-batch-processing/tree/master/complete

I tried to add :

  @Bean
  public StepScope stepScope() {
    final StepScope stepScope = new StepScope();
    stepScope.setAutoProxy(true);
    return stepScope;
  }

but it does not work (with spring.main.allow-bean-definition-overriding=true)

Here is my configuration of the spring batch

  @Bean
  @JobScope
  public RepositoryItemReader<DossierEntity> dossierToDiagnosticReader(PagingAndSortingRepository<DossierEntity, Long> dossierJpaRepository, @Value("#{jobParameters[origin]}") String origin) {
    RepositoryItemReader<DossierEntity> diagnosticDossierReader = new RepositoryItemReader<>();
    diagnosticDossierReader.setRepository(dossierJpaRepository);
    diagnosticDossierReader.setMethodName("listForBatch");
    // doing some stuff with origin
    return diagnosticDossierReader;
  }
ExceptionHandlerExceptionResolver[199] - Resolved [org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'scopedTarget.dossierToDiagnosticReader': Scope 'job' 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 job scope]
Guillaume
  • 53
  • 7

2 Answers2

1

I downloaded your project and was able to reproduce the case. There are two issues with your example:

  • You are defining a job scoped bean in your app but the JobScope is not defined in your context (and you are not using @EnableBatchProcessing annotation that adds it automatically to the context). If you want to use the job scope without @EnableBatchProcessing, you need to add it manually to the context.
  • Your test fails because there is no job running during your test. Job scoped beans are lazily instantiated when a job is actually run. Since your test does not start a job, the bean is not able to be proxied correctly.

Your test does not seem to test a batch job, I would exclude the job scoped bean from the test's context.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • I updated the project to have a real case (I took it from https://github.com/spring-guides/gs-batch-processing/tree/master/complete) and is a better use case of my project. You are right, i don't want to test a batch but the actuator endpoint configprops. It does not work since 2.2.0, maybe it is more a bug from spring actuator ? – Guillaume Oct 22 '19 at 17:09
  • 1
    If it works with 2.1.9 but fails with 2.2.0, then it is probably a regression indeed. However, since you want to test only the web endpoint and not the batch job itself, have you considered using the Spring Boot test [slice](https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests) `@WebMvcTest`? – Mahmoud Ben Hassine Oct 23 '19 at 13:12
0

Bug resolve in spring boot 2.2.1 https://github.com/spring-projects/spring-boot/issues/18714

Guillaume
  • 53
  • 7