0
0

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 1 year has passed since last update.

SpringBatchシンプルな動きだけするサンプル

Last updated at Posted at 2023-07-20

データの流れをコンソールに表示
バージョン 5.0.2

SimpleReader
@Component
public class SimpleReader implements ItemReader<String> {

    // 流すデータ
	private List<String> dataList = new ArrayList<>(Arrays.asList("Data01", "Data02", "Data03"));

	@Override
	public String read() {
        // nullが返ったら読み込みが終了する
		if (!dataList.isEmpty()) {
			String s = dataList.remove(0);
			System.out.println("Read : " + s);
			return s;
		}
		return null;
	}
}
SimpleProcessor
@Component
public class SimpleProcessor implements ItemProcessor<String, String> {

    // readしたデータが流れてくる
	@Override
	public String process(String item) {
		item = "processed - " + item;
		return item;
	}

}
SimpleWriter
@Component
public class SimpleWriter implements ItemWriter<String> {

	@Override
	public void write(Chunk<? extends String> chunk) throws Exception {
        // processorで加工したデータを表示
		System.out.println(chunk.getItems());
	}

}

Task
@Component
public class Task implements Tasklet {
    // Taskletの呼び出しサンプル
	@Override
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
		System.out.println("call tasklet");
		return RepeatStatus.FINISHED;
	}

}
SimpleLitener
// インターセプトアノテーションの使用メモ
@Component
public class SimpleLitener {

	@BeforeStep
	public void beforeStep(StepExecution stepExecution) {
		System.out.println("BeforeStep : " + stepExecution.getStepName());
	}

	@AfterStep
	public void afterStep(StepExecution stepExecution) {
		System.out.println("AfterStep : " + stepExecution.getStepName());
	}

}
実行結果(※コンソール出力箇所抜粋)
call tasklet

BeforeStep : step1
Read : Data01
[processed - Data01]
Read : Data02
[processed - Data02]
Read : Data03
[processed - Data03]
AfterStep : step1

BeforeStep : step2
Read : Data04
[processed - Data04]
Read : Data05
[processed - Data05]
Read : Data06
[processed - Data06]
AfterStep : step2

その他関連ファイル

BatchConfiguration.java

@AllArgsConstructor
@Configuration
public class BatchConfiguration {

	private JobRepository jobRepository;
	private PlatformTransactionManager transactionManager;
	private SimpleReader reader;
	private SimpleReader2 reader2;
	private SimpleProcessor processor;
	private SimpleWriter writer;
	private SimpleLitener listener;  

	@Bean
	Job sampleJob(Step step1, Step step2, Step taskStep) {
		return new JobBuilder("sampleJob", jobRepository)
				.incrementer(new RunIdIncrementer())
				.flow(taskStep)
				.next(step1)
				.next(step2)
				.end()
				.build();
	}

	@Bean
	Step taskStep(Task tasklet) {
		return new StepBuilder("taskStep", jobRepository)
				.tasklet(tasklet, transactionManager)
				.build();
	}

	@Bean
	Step step1() {
		return new StepBuilder("step1", jobRepository)
				.<String, String> chunk(1, transactionManager)
				.listener(listener)
				.reader(reader)
				.processor(processor)
				.writer(writer)
				.build();
	}

	@Bean
	Step step2() {
		return new StepBuilder("step2", jobRepository)
				.<String, String> chunk(1, transactionManager)
				.listener(listener)
				.reader(reader2)
				.processor(processor)
				.writer(writer)
				.build();
	}

}

SimpleReader2(Step2用のコピペ)
@Component
public class SimpleReader2 implements ItemReader<String> {

	private List<String> dataList = new ArrayList<>(Arrays.asList("Data04", "Data05", "Data06"));

	@Override
	public String read() {
		if (!dataList.isEmpty()) {
			String s = dataList.remove(0);
			System.out.println("Read : " + s);
			return s;
		}
		return null;
	}

}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?