2013年10月22日 星期二

Spring Batch (一) 基本介紹+Module

(一)Spring Batch 流程介紹


看圖說故事
一個JobLauncher觸發相對應的Job
一個Job 有數個 Step、而Step有三種應對
分別是 資料讀取(ItemReader)、資料處理(ItemProcessor)、資料寫入(ItemWriter)
而JobRepository 負責提供Launcher、Job、Step的持久化機制,負責運行中的CRUD

Step的執行流程
這部份小弟待解
因為我不了解到底是我們可以設計流程該怎麼跑
還是 Reader、Processor、Writer 三者本身就會這樣跑
不過 看眾多文章 似乎是..Spring Batch 本身就會這樣在這三者之前動作

(二)基本範例

採用Maven專案
配合Tomcat與Spring MVC架構
完成一組屬於 Website 又擁有Batch功能的Project

Batch的基礎建設
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- 針對Spring Batch 所需要的三大元素 -->
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
在這邊利用了 scheduled 來做Launcher的排程
當排程時間到 會執行 myLauncher
而myLauncher對應一組 simpleJob
Job 之中有兩個 step Task 分別為 simple 和 time
jobConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:batch="http://www.springframework.org/schema/batch"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<!-- <import resource="applicationContext.xml" /> -->
<!-- This xml page is job context -->
<bean id="mytasklet" class="batch.SimpleTasklet" />
<bean id="timetasklet" class="batch.TimeTasklet" />
 
    <!-- 一個Job要是有多個Step 記得要下next參數 -->
<batch:job id="simpleJob">
<batch:step id="simpleTaskletStep" next="timeTaskletStep">
<batch:tasklet ref="mytasklet" />
</batch:step>
<batch:step id="timeTaskletStep">
<batch:tasklet ref="timetasklet" />
</batch:step>
</batch:job>
<bean id="myLauncher" class="batch.MyLauncher">
<property name="jobLauncher" ref="jobLauncher"></property>
<property name="job" ref="simpleJob"></property>
</bean>
<!-- Launcher 的 scheduled 排程 採用cron表達式 -->
<task:scheduled-tasks>
<task:scheduled ref="myLauncher" method="launch"
cron="0/5 * * * * ?" />
</task:scheduled-tasks>
</beans>
MyLauncher
package batch;
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;
class MyLauncher {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public void launch() {
   JobParameters jobParameters = new JobParametersBuilder().addLong("time",
       System.currentTimeMillis()).toJobParameters();
   try {
     JobExecution execution = jobLauncher.run(job, jobParameters);
     System.out.println("Exit Status : " + execution.getStatus());
   } catch (Exception e) {
     System.out.println("Unable to execute job");
     e.printStackTrace();
   }
 }
 public JobLauncher getJobLauncher() {
   return jobLauncher;
 }
 public void setJobLauncher(JobLauncher jobLauncher) {
   this.jobLauncher = jobLauncher;
 }
 public Job getJob() {
   return job;
 }
 public void setJob(Job job) {
   this.job = job;
 }
}

SimpleTasklet
package batch;
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;
public class SimpleTasklet implements Tasklet {
  @Override
  public RepeatStatus execute(StepContribution paramStepContribution,
      ChunkContext paramChunkContext) throws Exception {
    System.out.println("Hello World!");
    return RepeatStatus.FINISHED;
  }
}
TimeTasklet
package batch;
import java.util.Date;
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;
public class TimeTasklet implements Tasklet {
  @Override
  public RepeatStatus execute(StepContribution paramStepContribution,
      ChunkContext paramChunkContext) throws Exception {
 Date now = new Date();
 System.out.println(now);
 return RepeatStatus.FINISHED;
  }
}

至於其他設定檔,可從範例中去看

範例請點我


沒有留言:

張貼留言