聞かれたのでメモ。
Spring BatchはデフォルトでJob管理のためのメタデータテーブルをDBに作成する。
Spring Bootを使うと設定不要でそのテーブルが作成され、アクセスするためのJobRepositoryなどが作られる。
ただ、これがDataSourceが一個しか登録されていないことを前提にしているので、本ジョブで複数のDataSourceを使おうとするとエラーになる。
java.lang.IllegalStateException: To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2
この時はJobRepository用のDataSourceに@Primaryをつけて、次のようなBatchConfigurerを用意しておけば良い。
@Bean
@ConfigurationProperties(prefix = "spring.datasource1")
@Primary
DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource2")
DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
DefaultBatchConfigurer batchConfigurer(DataSource dataSource) {
return new DefaultBatchConfigurer(dataSource);
}
メタデータテーブルをそもそも作りたくないという場合は次の記事を参照。