1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

spring-batchのTaskletで@BeforeStepは動かない

Posted at

spring-batchで以下のように、Tasklet@BeforeStepを付与するメソッドを追加しても意図通りの動作をしない。

@Component
public class MyTasklet implements Tasklet {
    
    @BeforeStep
    void beforeStep(StepExecution stepExecution) {
        System.out.println("asdfasdf");
    }

原因

恐らくだが、Taskletにはリスナーの自動登録が無いため、と思われる。

ItemReaderとかの実装クラスにStepExecutionListenerとかのリスナーを実装しとくと、(少なくともspring-bootと一緒にspring-batch動かす分には)そのリスナーも自動登録する
spring-batchはreaderとかでリスナー実装すると自動登録 を参照。

上記の通り、ItemReaderとかはリスナーの自動登録が有るがTaskletには無い、のが原因と思われる。ソースとかドキュメントをちゃんと追ってないので推測でしかないが。

解決策

executeメソッド引数のchunkContextを使うか、StepExecutionListenerを実装。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?