SpringバッチSpring TaskSchedulerの例

Spring Batch + Spring TaskSchedulerの例

このチュートリアルでは、Spring TaskSchedulerを使用して、5秒ごとに実行するバッチジョブをスケジュールする方法を示します。

使用するツールとライブラリ

  1. メーベン3

  2. Eclipse 4.2

  3. JDK 1.6

  4. Spring Core 3.2.2.RELEASE

  5. Spring Batch 2.2.0.RELEASE

1. プロジェクトのディレクトリ構造

標準のMavenプロジェクト。

spring-batch-taskscheduler

2. Spring TaskScheduler

Spring 3.0では、タスクをスケジュールするためのTaskSchedulerが導入されています。 Spring-Coreの一部であり、追加の依存関係を宣言する必要はありません。

  
    
  

  
    
  

TaskSchedulerは、Beanの下で実行されるようにスケジュールされます。

RunScheduler.java

package com.example;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RunScheduler {

  @Autowired
  private JobLauncher jobLauncher;

  @Autowired
  private Job job;

  public void run() {

    try {

    String dateParam = new Date().toString();
    JobParameters param =
      new JobParametersBuilder().addString("date", dateParam).toJobParameters();

    System.out.println(dateParam);

    JobExecution execution = jobLauncher.run(job, param);
    System.out.println("Exit Status : " + execution.getStatus());

    } catch (Exception e) {
    e.printStackTrace();
    }

  }
}

P.S JobParamater need to be unique each time a batch job to run, for testing purpose, we just pass in a new Date() everything running the job.

3. 春のバッチジョブ

このジョブは、csvファイルを読み取り、カスタムライターを介して値を表示するだけです。 ファイルの終わりを参照してください。task:scheduled-tasksを使用して、このバッチジョブを5秒ごとに実行します。

resources/spring/batch/jobs/job-report.xml



  

  
  
    
  

  

  
    
  
  

  

  

  
    
      
        
        
      
    
   

  

    
    

    
      
        
          
            
          
        
        
          
            
          
        
      
    

  

  

  
  
    
    
   

report.csv

1,"139,237"
2,"500,657"
3,"342,100"

CustomWriter.java

package com.example.writers;

import java.util.List;
import org.springframework.batch.item.ItemWriter;
import com.example.model.Report;

public class CustomWriter implements ItemWriter {

  @Override
  public void write(List items) throws Exception {

    System.out.println("writer..." + items.size());
    for(Report item : items){
        System.out.println(item);
    }

  }

}

4. それを実行します

Springアプリケーションコンテキストをロードすると、スケジューラが自動的に実行されます。

App.java

package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

  public static void main(String[] args) {

    String springConfig = "spring/batch/jobs/job-report.xml";

    ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

  }
}

出力、csvコンテンツを5秒ごとに印刷します。

......
Sun Jul 28 11:20:30 MYT 2013
Jul 28, 2013 11:20:30 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=reportJob]] launched with the following parameters: [{date=Sun Jul 28 11:20:30 MYT 2013}]
Jul 28, 2013 11:20:30 AM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
writer...3
Report [id=1, Impressions=139,237]
Report [id=2, Impressions=500,657]
Report [id=3, Impressions=342,100]
Jul 28, 2013 11:20:30 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=reportJob]] completed with the following parameters: [{date=Sun Jul 28 11:20:30 MYT 2013}] and the following status: [COMPLETED]
Exit Status : COMPLETED

Sun Jul 28 11:20:35 MYT 2013
Jul 28, 2013 11:20:35 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=reportJob]] launched with the following parameters: [{date=Sun Jul 28 11:20:35 MYT 2013}]
Jul 28, 2013 11:20:35 AM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
writer...3
Report [id=1, Impressions=139,237]
Report [id=2, Impressions=500,657]
Report [id=3, Impressions=342,100]
Exit Status : COMPLETED
Jul 28, 2013 11:20:35 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=reportJob]] completed with the following parameters: [{date=Sun Jul 28 11:20:35 MYT 2013}] and the following status: [COMPLETED]
......

ソースコードをダウンロード

ダウンロード–SpringBatch-TaskScheduler-Example.zip(18 kb)