2
4

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.

Eclipseで簡単なバッチ処理の枠組みを作成する。

Last updated at Posted at 2018-01-02

この記事は、Eclipse でバッチ処理を作成するにあたり、まずは簡単な枠組みを作成できるかどうかを試行錯誤しながら、進めてみたことをそのまま記載したものです。

バッチ処理自体は、処理時間が長いもの(大量のデータを入力して、加工して、出力する) などを別の処理として行うというのが何となくのイメージです。なので、ジョブとかタスクとかの考え方は無く、main関数の中で長い処理をひたすら実行するようなものをイメージしていました。ただ実際に SpringFramework の機能を使うと、ジョブ、タスクの考え方が必要でした。そこで、まずはメイン関数からジョブを実行して、ジョブの中からタスクを実行して、終了コードを設定するところまでを作ってみます。

まずは、以下のバッチ処理を作ってみます。
・BatchTestApplication(メイン)
=> BatchTestJobを起動する。(SpringFrameworkを使用)
・BatchTetJob(ジョブ)
=> BatchTestTaskletを起動する。
・BatchTestTasklet(タスク)
=> "BatchTestTasklet Start OK" と出力する (終了コードは 1 を設定する)

ポイント:バッチ処理で終了コードを設定するのが少し複雑です。
(1)BatchTestTaskletクラスに ExitCodeGenerator インターフェースを実装します。
(2)getExitCodeメソッドをオーバライドして、その中で戻り値を返すようにします。
(3)SpringApplication.exit の戻り値に(2)の戻り値が設定されます。

・BatchTestApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class BatchTestAppApplication {

public static void main(String[] args) {
	ApplicationContext context = SpringApplication.run(BatchTestAppApplication.class, args);
	int iRet = SpringApplication.exit(context);
	System.out.println(iRet);
	System.exit(iRet);
}

}

・BatchTestJob.java

package com.example.demo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@EnableBatchProcessing
public class BatchTestJob {

@Autowired
private JobBuilderFactory jobFactory;

@Autowired
private StepBuilderFactory stepFactory;

@Autowired
private BatchTestTasklet batchTestTasklet;

@Bean
public Step step1() {
	return stepFactory
			.get("step1")
			.tasklet(batchTestTasklet)
			.build();
}

@Bean
public Job job(Step step1) {

	return jobFactory
			.get("job")
			.incrementer(new RunIdIncrementer())
			.start(step1)
			.build();
}

}

・BatchTestTasklet.java

package com.example.demo;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.stereotype.Component;

@Component
public class BatchTestTasklet implements Tasklet, ExitCodeGenerator {

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

	System.out.println("BatchTestTasklet Start OK");
	return RepeatStatus.FINISHED;
}

@Override
public int getExitCode() {
	return 1;
}

}

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?