본문 바로가기
개발/Spring

[Spring Batch] 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;

by 상용최 2020. 11. 16.
반응형

Spring Batch에서 @JobScope사용시 아래와 같은 에러가 발생할 수 있다.

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;

 

기존코드는 아래와 같았다.

@JobScope를 빼면 문제없이 동작하는 코드가 @JobScope만 달면 빈 생성에서 에러가 발생했다.

    @Bean(name = "testJob")
    public Job testJob(JobCompletionNotificationListener jobCompletionNotificationListener){
        return jobBuilderFactory.get("job1")
                .incrementer(new CurrentTimeIncrementer())
                .listener(jobCompletionNotificationListener)
                .flow(step1(null,null,null))
                .end()
                .build();
    }

    @Bean
    @JobScope
    public Step step1(MyBatisPagingItemReader<Test> reader
                        , CustomItemWriter customItemWriter
                        , ChunkListenerImpl chunkListener){
        return stepBuilderFactory.get("step1")
                .<Test, Test>chunk(10)
                .reader(reader)
                .faultTolerant()
                    .retryLimit(10)
                    .retry(Exception.class)
                .processor(mySqlBatchProcessor())
                .writer(customItemWriter)
                .listener(chunkListener)
                .build();
    }

 

코드를 아래와같이 변경하면 정상적으로 동작한다.

    @Bean(name = "testJob")
    public Job testJob(JobCompletionNotificationListener jobCompletionNotificationListener){
        return jobBuilderFactory.get("job1")
                .incrementer(new CurrentTimeIncrementer())
                .listener(jobCompletionNotificationListener)
                .start(step1(null, null, null))
                .build();
    }

    @Bean
    @JobScope
    public Step step1(MyBatisPagingItemReader<Test> reader
                        , CustomItemWriter customItemWriter
                        , ChunkListenerImpl chunkListener){
        return stepBuilderFactory.get("step1")
                .<Test, Test>chunk(10)
                .reader(reader)
                .faultTolerant()
                    .retryLimit(10)
                    .retry(Exception.class)
                .processor(mySqlBatchProcessor())
                .writer(customItemWriter)
                .listener(chunkListener)
                .build();
    }

자세한 내용은 아래 클래스들을 분석해보시면 알 수 있을거라고 생각됩니다.

 

참고사항

docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/job/builder/JobBuilder.html#flow-org.springframework.batch.core.Step-

 

JobBuilder (Spring Batch 4.3.0 API)

JobBuilder(java.lang.String name) Create a new builder for a job with the given name.

docs.spring.io

docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/job/builder/FlowJobBuilder.html

 

FlowJobBuilder (Spring Batch 4.3.0 API)

FlowJobBuilder public FlowJobBuilder(JobBuilderHelper  parent) Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. Parameters: parent - a parent helper containing common job properties

docs.spring.io

docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/job/builder/SimpleJobBuilder.html

 

SimpleJobBuilder (Spring Batch 4.3.0 API)

SimpleJobBuilder public SimpleJobBuilder(JobBuilderHelper  parent) Create a new builder initialized with any properties in the parent. The parent is copied, so it can be re-used. Parameters: parent - the parent to use

docs.spring.io

 

 

 

반응형

댓글