いやあ、試しに色々なサイトのソースを参考に Spring Batch を試してたんだけど、一向に実行できない(^^;
ソース上のエラーは全部消しても、実行時に
***************************
APPLICATION FAILED TO START
***************************
みたいなエラーを出して止まってしまう。原因は色々だが、どこが問題なのかわからない・・・
ただ、調べていくうちに、どうも参考にしているサイトの Spring Batch が古いのが問題なのではないか?という気がしてきた。
なので、まさに俺の環境(最新の Spring Batch 6 / Spring Boot 4 環境)で説明がされている IK.AM さんの
というページを参照。
結局、下のようなソース(著作権は IK.AM さんにあると思いますが、一応、若干 import 先が違っていたりするので公開します)でついにバッチ実行ができた。
<BatchTestApplication.java>
package com.netandfield.test;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableBatchProcessing
public class BatchTestApplication {
public static void main(String[] args) {
SpringApplication.run(BatchTestApplication.class, args);
}
}
<config/JobConfig.java>
package com.netandfield.test.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.Step;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.infrastructure.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class JobConfig {
private final Logger log = LoggerFactory.getLogger(JobConfig.class);
private final JobRepository jobRepository;
public JobConfig(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
@Bean
@StepScope
public Tasklet helloTasklet() {
return (contribution, chunkContext) -> {
log.info("Hello World!");
return RepeatStatus.FINISHED;
};
}
@Bean
public Step step1(Tasklet helloTasklet) {
return new StepBuilder("step1", jobRepository).tasklet(helloTasklet)
.build();
}
@Bean
public Job job1(Step step1) {
return new JobBuilder("job1", jobRepository).start(step1).build();
}
}
この2ファイルを作成。(俺は Eclipse 上で作成しているので)プロジェクトエクスプローラーで BatchTestApplication.java を選択し、右ボタンメニューから「デバッグ」→「Javaアプリケーション」で実行される。
やっと Eclipse のコンソールに
[BatchTest] [ restartedMain] com.netandfield.test.config.JobConfig : Hello World!
と表示された。
しかし、Spring Batch 6 を使って説明している Web ページ、少ないなあ・・・
「まずは丸々コピーで参考サイトのソースを持ってきて、それを実行しながらプログラムの勉強をする」スタイルの俺からすると、「プログラムと実行結果を見比べながら記述内容の確認をして言語を理解していく」ことができないので、「動かないことには動くようにできない」というパラドックスにハマってしまったのであった・・・
エロい、いや、偉い人たち、最新環境での参考ページ制作をよろしくお願いします(笑)
最近のコメント