国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Spring Batch 系列] 第一節 初識 Spring Batch

2019-11-14 22:01:19
字體:
來源:轉載
供稿:網友
[SPRing Batch 系列] 第一節 初識 Spring Batch

距離開始使用 Spring Batch 有一段時間了,一直沒有時間整理,現在項目即將完結,整理下這段時間學習和使用經歷。

官網地址:http://projects.spring.io/spring-batch/

一、定義與特點

A lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily Operations of enterprise systems.

Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. It also provides more advanced technical services and features that will enable extremely high-volume and high performance batch jobs through optimization and partitioning techniques. Simple as well as complex, high-volume batch jobs can leverage the framework in a highly scalable manner to process significant volumes of information.

Features
  • Transaction management
  • Chunk based processing
  • Declarative I/O
  • Start/Stop/Restart
  • Rety/Skip
  • Web based administration interface (Spring Batch Admin)
二、簡介

Spring Batch 是一個依托 Spring,面向批處理的框架,可以應用于企業級數據處理系統。通過閱讀官網文檔,可以知道 Spring Batch 的核心組件包括 Job、Step 等。Spring Batch 不僅提供了統一的讀寫接口、豐富的任務處理方式、靈活的事務管理及并發處理,同時還支持日志、監控、任務重啟與跳過等特性,大大簡化了批處理應用開發,將開發人員從復雜的任務配置管理過程中解放出來,使他們可以更多地去關注核心的業務處理過程。

使用場景

  • Commit batch process periodically
  • Concurrent batch processing: parallel processing of a job
  • Staged, enterprise message-driven processing
  • Massively parallel batch processing
  • Manual or scheduled restart after failure
  • Sequential processing of dependent steps (with extensions to workflow-driven batches)
  • Partial processing: skip records (e.g. on rollback)
  • Whole-batch transaction: for cases with a small batch size or existing stored procedures/scripts

三、HelloWorld

程序簡介:從指定路徑的文本文件中逐行讀取,獲取用戶的姓和名,并在處理器中拼接用戶的姓名,最后輸出用戶的姓名。

操作系統Win7 x64 旗艦版

開發環境:Eclipse 4.3 、JDK1.6

步驟:

1. 搭建開發工程

打開Eclipse, 新建 java Project ,本例使用 SpringBatchTest 為項目名。

新建 lib 文件夾,導入 SpringBatch 的 Jar 包和其他依賴包。建立相關 package 和 class ,得到結構如下圖:

SpringBatchTest001

其中 包 和 類 定義:

acc 存放訪問控制類(本例準備存放作業測試類)

batch.listener 存放批處理監聽器

batch.processor 存放 ItemProcessor實現類

batch.reader 存放 ItemReader 實現類

batch.writer 存放 ItemWriter 實現類

batch.mapper 存放邏輯對象映射處理類(本例準備存放文本行于文本行對象映射處理類)

batch.data 存放批處理過程中使用的邏輯對象

BatchServer.java 定義批處理任務方法接口

配置文件 定義:

spring-application-batch.xml 定義spring batch 核心組件和自定義作業

spring-application-resource.xml 定義spring 組件

spring-application-context.xml 根配置文件,引入使用的配置文件,并控制配置文件引入順序

2. 編寫配置文件和對應的程序代碼

由于開發過程中,配置文件和程序是并行書寫的,所以以下內容無特定順序

(1) Spring Batch 配置文件及其中定義的組件實例

spring-application-batch.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:batch="http://www.springframework.org/schema/batch" 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://www.springframework.org/schema/beans   6             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   7             http://www.springframework.org/schema/batch    8             http://www.springframework.org/schema/batch/spring-batch-2.2.xsd" 9     default-autowire="byName">10 11     <!-- Spring Batch  內存模型 -->12     <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />13     <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">14         <property name="jobRepository" ref="jobRepository" />15     </bean>16     <bean id="taskExecutor" class="org.springframework.core.task.SyncTaskExecutor" />17 18 19     <!-- 文本行與邏輯對象映射處理 -->20     <bean id="customerLineMapper" class="cn.spads.batch.mapper.CustomLineMapper"/>21     <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" >22         <property name="delimiter" value=" "/>23     </bean>24 25     <!-- Scope = step 變量后綁定固定寫法,即可以在對象調用時綁定變量 -->26     <bean id="customReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">27         <property name="lineMapper">28             <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">29                 <property name="lineTokenizer" ref="lineTokenizer"/>30                 <property name="fieldSetMapper" ref="customerLineMapper"/>31             </bean>       32         </property>33         <!-- 此處使用單一文件絕對路徑 -->34         <property name="resource" value="file:#{jobParameters['customFileAbPath']}"/>35     </bean>36 37     <bean id="customProcessor" class="cn.spads.batch.processor.CustomProcessor"/>38     <bean id="customWriter" class="cn.spads.batch.writer.CustomWriter"/>39 40     <bean id="customJobListener" class="cn.spads.batch.listener.CustomJobListener"/>41     <bean id="customStepListener" class="cn.spads.batch.listener.CustomStepListener"/>42 43 44     <batch:job id="customJob">45         <batch:step id="customJob_first_step">46             <batch:tasklet>47                 <batch:chunk reader="customReader" processor="customProcessor" 48                     writer="customWriter" commit-interval="100">49                 </batch:chunk>50                 <batch:listeners>51                     <batch:listener ref="customStepListener" />52                 </batch:listeners>53             </batch:tasklet>54         </batch:step>55         <batch:listeners>56             <batch:listener ref="customJobListener"/>57         </batch:listeners>58     </batch:job>59 </beans>

由于本示例使用Spring Batch 提供 的固定長度文本加載實例(FlatFileItemReader),因此沒有自定義 Reader。

LineVo.java

package cn.spads.batch.data;/** * <b>文本行邏輯對象</b><br> * @author        Gaylen * @version        V1.1.0 * history * 1.1.0, 2014年11月24日        Gaylen            FE * @since        Java 6.0 */public class LineVo {    /** 行號 */    private int id;    /** 名 */    private String givenName;    /** 姓 */    private String familyName;    /** 全名 */    private String fullName;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getGivenName() {        return givenName;    }    public void setGivenName(String givenName) {        this.givenName = givenName;    }    public String getFamilyName() {        return familyName;    }    public void setFamilyName(String familyName) {        this.familyName = familyName;    }    public String getFullName() {        return fullName;    }    public void setFullName(String fullName) {        this.fullName = fullName;    }}
View Code

CustomLineMapper.java

package cn.spads.batch.mapper;import org.springframework.batch.item.file.mapping.FieldSetMapper;import org.springframework.batch.item.file.transform.FieldSet;import org.springframework.validation.BindException;import cn.spads.batch.data.LineVo;/** * <b>文本行-邏輯對象映射</b><br> * @author        Gaylen * @version        V1.1.0 * history * 1.1.0, 2014-11-24        Gaylen            FE * @since        Java 6.0 */public class CustomLineMapper implements FieldSetMapper<LineVo> {    /**     * <b>映射處理</b><br>     * @param fieldSet     * @return DelCommandBean     */    @Override    public LineVo mapFieldSet(FieldSet fieldSet) throws BindException {        LineVo lv = new LineVo();        lv.setId(Integer.parseInt(fieldSet.readString(0)));        lv.setGivenName(fieldSet.readString(1));        lv.setFamilyName(fieldSet.readString(2));        return lv;    }}
View Code

CustomProcessor.java

package cn.spads.batch.processor;import org.springframework.batch.item.ItemProcessor;import cn.spads.batch.data.LineVo;/** * <b>處理器</b><br> * @author        Gaylen * @version        V1.1.0 * history * 1.1.0, 2014年11月24日        Gaylen            FE * @since        Java 6.0 */public class CustomProcessor implements ItemProcessor<LineVo, LineVo> {    @Override    public LineVo process(LineVo item) throws Exception {        if (item == null) {            return null;        }        item.setFullName(new StringBuilder().append(item.getFamilyName() == null ? "*" : item.getFamilyName())                .append(" - ")                .append(item.getGivenName() == null ? "*" : item.getGivenName())                .toString());        return item;    }}
View Code

CustomWriter.java

package cn.spads.batch.writer;import java.util.List;import org.springframework.batch.item.ItemWriter;import cn.spads.batch.data.LineVo;/** * <b>輸出</b><br> * @author        Gaylen * @version        V1.1.0 * history * 1.1.0, 2014年11月24日        Gaylen            FE * @since        Java 6.0 */public class CustomWriter implements ItemWriter<LineVo> {    @Override    public void write(List<? extends LineVo> items) throws Exception {        if (items == null || items.size() == 0) {            System.out.println("error.");        } else {            for (LineVo lv : items) {                System.out.println(lv.getFullName());            }        }    }}
View Code

CustomJobListener.javaCustomStepListener.java 本例中只給出空定義。

BatchServer.java

package cn.spads.batch;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import org.springframework.batch.core.Job;import org.springframework.batch.core.JobExecution;import org.springframework.batch.core.JobParameter;import org.springframework.batch.core.JobParameters;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;/** * <b>批處理服務接口</b><br> * @author        Gaylen * @version        V1.1.0 * history * 1.1.0, 2014年11月24日        Gaylen            FE * @since        Java 6.0 */public class BatchServer {    /** 類單例對象 */    private static final BatchServer INSTANCE = new BatchServer();    /**     * 單例     * @return     */    public static BatchServer getInstance() {        return INSTANCE;    }    /**     * 私有構造方法     */    private BatchServer() {    }    /**     * <b>測試作業</b><br>     * @param launcher     * @param job     * @param paraMap     */    public void execCustomJob(JobLauncher launcher, Job job, Map<String, Object> paraMap) {        JobExecution result = this.executeBatchJob(launcher, job, this.getJobParameters(paraMap));        System.out.println(result.toString());    }    /**     * <b>得到作業選項</b><br>     * 默認配置任務開始時間     * @param paraMap     * @return     */    private JobParameters getJobParameters(Map<String, Object> paraMap) {        HashMap<String, JobParameter> parameters = new HashMap<String, JobParameter>();        parameters.put("time", new JobParameter(Calendar.getInstance().getTimeInMillis()));        String key = null;        Object value = null;        if (paraMap == null || paraMap.size() == 0) {            return new JobParameters(parameters);        }        for (Entry<String, Object> entry : paraMap.entrySet()) {            if (entry == null) {                continue;            }            key = entry.getKey();            value = entry.getValue();            if (value instanceof Date) {                parameters.put(key, new JobParameter((Date) value));            } else if (value instanceof String || value instanceof Integer) {                parameters.put(key, new JobParameter((String) value));            } else if (value instanceof Double) {                parameters.put(key, new JobParameter((Double) value));            } else if (value instanceof Long) {                parameters.put(key, new JobParameter((Long) value));            }        }        return new JobParameters(parameters);    }    /**     * <b>批處理執行器</b><br>     * @param joblanuncher     * @param job     * @param parameters     */    public JobExecution executeBatchJob(JobLauncher launcher, Job job, JobParameters jobParameters) {        JobExecution result = null;        try {            result = launcher.run(job, jobParameters);        } catch (JobExecutionAlreadyRunningException e) {            e.printStackTrace();        } catch (JobRestartException e) {            e.printStackTrace();        } catch (JobInstanceAlreadyCompleteException e) {            e.printStackTrace();        } catch (JobParametersInvalidException e) {            e.printStackTrace();        }        return result;    }}
View Code

(2) acc 包下 新建測試類 MainTest.java 并在 I盤 新建 SpringBatchTest.txt

package cn.spads.acc;import java.util.HashMap;import java.util.Map;import org.springframework.batch.core.Job;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import cn.spads.batch.BatchServer;/** * <b>批處理測試入口</b><br> * @author        Gaylen * @version        V1.1.0 * history * 1.1.0, 2014年11月24日        Gaylen            FE * @since        Java 6.0 */public class MainTest {    static private String fileLocation = "I:/SpringBatchTest.txt";    static private void testCustomJob(ApplicationContext context) {        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");        Job job = (Job) context.getBean("customJob");        Map<String, Object> paraMap = new HashMap<String, Object>();        paraMap.put("customFileAbPath", fileLocation);        BatchServer.getInstance().execCustomJob(launcher, job, paraMap);    }    public static void main(String[] args) {        ApplicationContext context = new FileSystemXmlApplicationContext("config/spring-application-context.xml");        testCustomJob(context);    }}
View Code
文本文件(SpringBatchTest.txt)內容如下:
1 三 張2 四 李3 五 王4 六 馬
View Code

通過以上步驟, project 目錄結構應如下圖:

SpringBatchTest002

3. 至此整個 HelloWorld 項目搭建完成, 可以運行程序,得到輸出結果如下:

 1 2014-11-25 0:12:28 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh 2 信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@bf32c: startup date [Tue Nov 25 00:12:28 CST 2014]; root of context hierarchy 3 2014-11-25 0:12:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 4 信息: Loading XML bean definitions from file [D:/LocalDEV/workspace43/SpringBatchTest/config/spring-application-context.xml] 5 2014-11-25 0:12:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 6 信息: Loading XML bean definitions from file [D:/LocalDEV/workspace43/SpringBatchTest/config/spring-application-resource.xml] 7 2014-11-25 0:12:28 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 8 信息: Loading XML bean definitions from file [D:/LocalDEV/workspace43/SpringBatchTest/config/spring-application-batch.xml] 9 2014-11-25 0:12:28 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition10 信息: Overriding bean definition for bean 'customJob': replacing [Generic bean: class [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.batch.core.configuration.xml.JobParserJobFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]11 2014-11-25 0:12:28 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition12 信息: Overriding bean definition for bean 'customReader': replacing [Generic bean: class [org.springframework.batch.item.file.FlatFileItemReader]; scope=step; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:/LocalDEV/workspace43/SpringBatchTest/config/spring-application-batch.xml]] with [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in file [D:/LocalDEV/workspace43/SpringBatchTest/config/spring-application-batch.xml]]13 2014-11-25 0:12:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons14 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157fb52: defining beans [transactionManager,jobRepository,jobLauncher,taskExecutor,customerLineMapper,lineTokenizer,customReader,customProcessor,customWriter,customJobListener,customStepListener,org.springframework.batch.core.scope.internalStepScope,org.springframework.beans.factory.config.CustomEditorConfigurer,org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor,customJob_first_step,customJob,scopedTarget.customReader]; root of factory hierarchy15 2014-11-25 0:12:28 org.springframework.batch.core.launch.support.SimpleJobLauncher run16 信息: Job: [FlowJob: [name=customJob]] launched with the following parameters: [{time=1416845548796, customFileAbPath=I:/SpringBatchTest.txt}]17 2014-11-25 0:12:28 org.springframework.batch.core.job.SimpleStepHandler handleStep18 信息: Executing step: [customJob_first_step]19 張 - 三20 李 - 四21 王 - 五22 馬 - 六23 2014-11-25 0:12:28 org.springframework.batch.core.launch.support.SimpleJobLauncher run24 信息: Job: [FlowJob: [name=customJob]] completed with the following parameters: [{time=1416845548796, customFileAbPath=I:/SpringBatchTest.txt}] and the following status: [COMPLETED]25 JobExecution: id=0, version=2, startTime=Tue Nov 25 00:12:28 CST 2014, endTime=Tue Nov 25 00:12:28 CST 2014, lastUpdated=Tue Nov 25 00:12:28 CST 2014, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=0, version=0, Job=[customJob]], jobParameters=[{time=1416845548796, customFileAbPath=I:/SpringBatchTest.txt}]
View Code

總結:本篇文章簡單介紹了 Spring Batch,以及使用 Spring Batch 開發 HelloWorld 程序。

本文中使用 Project 源碼可以從此處下載:http://download.csdn.net/detail/driftingshine/8194729


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 子长县| 洮南市| 麻阳| 康定县| 梧州市| 江达县| 汽车| 舟山市| 定陶县| 仪陇县| 运城市| 珲春市| 长岛县| 乐陵市| 三明市| 绥宁县| 青龙| 从化市| 舒城县| 旬阳县| 简阳市| 大宁县| 长丰县| 泰顺县| 镇巴县| 都江堰市| 山东省| 双柏县| 南江县| 武陟县| 鸡泽县| 托克托县| 抚松县| 江华| 志丹县| 宁远县| 清涧县| 沙雅县| 临沭县| 上虞市| 吉木乃县|