spring batch入門實例

spring batch入門實例

爲什麼要進行批處理,哪些場合適合進行批處理?

企業領域的許多應用需要在嚴格的任務環境下,進行大量的商業計算。這些商業計算包括在大數據集中進行自動而又複雜的計算,而且這些計算不需要人工的干預。這些任務中經典的就是基於時間驅動的事件的複雜商業規則的週期性的應用,在大數據集上的重複計算,或者從內部、外部系統接收到的信息的集成,這些信息需要格式化、校驗、像一個事務一樣處理。批處理能爲公司每天處理10億計的數據。

Spring batch官方的文檔中是這樣介紹自己的:

Spring batch是一個輕量級的,易理解的批處理框架,它被設計成用於健壯的批處理應用,特別是企業系統的日常操作。Spring batch構建於產業化的、基於POJO的開發探索,能輕鬆地使用Spring框架的擴展能力,必要的時候,開發者也能很輕鬆地進行擴展。Spring batch不是一個任務框架。有很多的商業化的或者開源的任務框架,比如:Quartz,Tivoli,Control-M,等等。Spring batch是用來和這些協同工作,而不是取代它們。
Spring Batch提供了一些處理大容量數據的必要的可重用的函數。包括日誌、事物管理,任務進度統計,任務重啓,跳過,資源管理。它也提供了一些更加高級的技術服務和特性,允許處理高容量,高性能批處理任務,來應對過程優化和分佈式計算。簡單也很複雜,高容量的批處理任務能夠以一種高度可伸縮的方式提升框架處理大量信息的能力。
當開源軟件項目和相關的軟件組織把他們的注意力放在基於web的和SOA消息驅動的結構框架上,基於java的可重用的批處理框架就顯得那樣的少,儘管企業的IT環境的需求卻一直在增長。一個標準的可重用的批處理框架的缺乏導致了許多一次性的,家庭的解決方案出現在客戶的IT函數中。
SpringSource和Accenture合作來改變這樣情況,Accenture在自己產業上的批處理經驗加上SpringSource在技術經驗上的深度,和Spring已經證明的技術模型。產生了一個自然的,強大的團隊,創建了個一高質量的,市場化的軟件,增補了企業JAVA在批處理上的缺失。兩個公司都在致力於幫一些客戶解決一些相同的問題,開發基於Spring的批處理結構解決方案。這提供了一些有用的附加細節和實際的原則,幫助我們處理現實中客戶提出的實際問題。因爲這些和其它的一些原因,SpringSource和Acceture組建了一個團隊合作開發Spring Batch。
Acceture貢獻了前面所說的批處理的結構框架,基於在前幾代系統構建批處理的經驗,包括Spring Batch中的資源列表,擴展,和藍圖。
Acceture和Spring Batch這間的合作就是爲了促進企業開發者在開發批處理應用時,能在軟件處理過程中的探索、框架、工具中進行拓展。公司和政府機構,想在他們的企業IT環境中分發標準的,已經被證明的解決方案時,就會從Spring Batch中受益。

Spring Batch的使用場景:

一個經典的批處理程序是這樣的:從數據庫、文件、或者隊列中讀取大量的記錄,以一種模式時行處理,然後以一種修改後的形式寫回。Spring Batch自動化了這個批處理疊代,提供了以一個數據集的方式處理相似事物的能力,典型的就是沒有用戶交互的離線環境。批處理任務是大多數IT項目的一部分,而且Spring Batch是唯一的一個提供開源的、健壯的、企業級可伸縮性的框架。
業務場景:
1、階段性的批處理提交
2、同時處理:並行的任務處理
3、分步的、企業級的任務處理
4、大量的並行批處理任務
5、失敗之後的手動或都計劃性重啓
6、獨立步驟的連續處理
7、部分處理:跳過錯誤項
8、完全的批處理事務

技術目標:
1、批處理開發者使用Spring技術模型,專注於業務邏輯,讓框架來處理底層的細節;
2、概念和底層的清晰分離,批處理的執行環境和批處理應用;
3、以接口的方式提供一個一般化的、核心的運行服務,所有的項目都能實現它;
4、提供核心接口的默認簡單的實現,讓開發者很容易上手;
5、很簡單的配置、個性化、擴展服務,通過在Spring的各個層進行擴展;
6、提供一個簡單的部署模型,JAR包和應用分離,使用maven進行構建。

Spring Batch的結構

Spring Batch在設計的時候考慮到了擴展性和各種最終開發的用戶的多樣性。下面的圖展示了分層框架的骨架,支持擴展和開發者的易用性。

分層結構展示了分層結構的三個主要部件:應用層,核心層,和基礎結構層。一個應用包含了所有的批處理任務和開發者用Spring Batch寫的代碼。Core層包含了一些核心的運行時類,用來運行來管理一個批處理任務。它包括了像JobLauncher和Job,還有Step這樣的實現。應用層和核心層都是構建在基礎結構層之上。基礎結構層包括了readers和writers,還有services,比如RetryTemplate,這些被應用開發者和核心框架共用。


這是一篇spring batch的入門實例,如果你對spring的一些基本概念還不是很瞭解,可能會有難度,本文也假設你已經對maven有一些瞭解。
首先新建一個maven項目,注意是jar項目,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.test.springbatch</groupId>
	<artifactId>spring-batch</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-batch</name>
	<description>spring-batch</description>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<org.springframework.version>3.0.7.RELEASE</org.springframework.version>
	</properties>
	<dependencies>
		<!-- 測試用 start -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.batch</groupId>
			<artifactId>spring-batch-core</artifactId>
			<version>2.1.9.RELEASE</version>
		</dependency>
	</dependencies>
</project>

spring-batch的配置文件(applicationContext-batch-myfirstbatch.xml)如下:
<beans:beans xmlns="http://www.springframework.org/schema/batch"
	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:bean="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.xsd
	http://www.springframework.org/schema/batch
	http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
	<bean:bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<bean:property name="transactionManager" ref="transactionManager" />
	</bean:bean>
	<bean:bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
	</bean:bean>
	<bean:bean id="jobLauncher"
		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<bean:property name="jobRepository" ref="jobRepository" />
	</bean:bean>
	<job id="ioSampleJob">
		<step id="step1">
			<tasklet ref="begin"></tasklet>
		</step>
	</job>
	<bean:bean id="begin" class="com.test.tasklet.MyFirstTasklet" scope="step">
	</bean:bean>
</beans:beans>
這個文件放在src/main/resources文件夾下。

com.test.tasklet.MyFirstTasklet
的代碼如下:
/**
 * 
 */
package com.test.tasklet;

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;

/**
 * @author hadoop
 *
 */
public class MyFirstTasklet implements Tasklet {

	/* (non-Javadoc)
	 * @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)
	 */
	public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
			throws Exception {
		
		for(int i = 0; i < 10; i++)
		{
			System.out.println(i);
		}
		
		return RepeatStatus.FINISHED;
	}

}

main函數:
/**
 * 
 */
package com.test.springbatch;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author hadoop
 *
 */
public class AppMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context;
		JobParametersBuilder jobPara = new JobParametersBuilder();  //設置文件路徑參數
		context = new FileSystemXmlApplicationContext(new String[]{"classpath:applicationContext-batch-myfirstbatch.xml"});
	    String jobName = "ioSampleJob";
		Job job = (Job)context.getBean(jobName);
	    JobLauncher launcher = (JobLauncher)context.getBean("jobLauncher");
	    JobExecution result = null;
	    try {
	      result = launcher.run(job, jobPara.toJobParameters());
	    } catch (JobExecutionAlreadyRunningException e) {
	    	e.printStackTrace();
	    } catch (JobRestartException e) {
	    	e.printStackTrace();
	    } catch (JobInstanceAlreadyCompleteException e) {
	    	e.printStackTrace();
	    } catch (JobParametersInvalidException e) {
	    	e.printStackTrace();
	    }
	    ExitStatus es = result.getExitStatus();
	    if(es.getExitCode().equals(ExitStatus.COMPLETED.getExitCode())) //任務正常完成
	    {
	    	System.out.println("任務正常完成");
	    }
	    else
	    {
	    	System.out.println("任務失敗");
	    }
	}

}
一個spring batch的入門實例的代碼就這麼多。
但用到的概念卻不多,下面將一一說明。
Job,一個Job就是一個任務;


發佈了55 篇原創文章 · 獲贊 6 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章