欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring batch 入門學(xué)習(xí)教程(附源碼)

 更新時間:2017年11月06日 11:24:11   作者:achuo  
本篇文章主要介紹了Spring batch 入門學(xué)習(xí)教程(附源碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Spring batch 是一個開源的批處理框架.執(zhí)行一系列的任務(wù). 在 spring batch 中 一個job 是由許多 step 組成的。而每一個 step  又是由 READ-PROCESS-WRITE task或者 單個 task 組成。

1. "READ-PROCESS-WRITE" 處理,根據(jù)字面意思理解就可以:

  1. READ 就是從資源文件里面讀取數(shù)據(jù),比如從xml文件,csv文件,數(shù)據(jù)庫中讀取數(shù)據(jù).
  2. PROCESS 就是處理讀取的數(shù)據(jù)
  3. WRITE 就是將處理過的數(shù)據(jù)寫入到其他資源文件中去,可以是XML,CSV,或者數(shù)據(jù)庫.

比如:從CSV文件中 讀取數(shù)據(jù),經(jīng)過處理之后,保存到數(shù)據(jù)庫. spring batch 提供了很多類去處理這方面的東西。

2.單個task, 也就是處理單個任務(wù)。比如在一個step 開始之前或者完成之后清除資源文件等.

3.許多個step 組成在一起,就組成了一個job. 所以他們之間的關(guān)系,就如同下面的描述:

一個 job = 很多steps
一個step = 一個READ-PROCESS-WRITE 或者 一個task.
同樣一個job = step1 -->step2--step3 這樣鏈表形式的組成.

Spring batch 例子

考慮如下一個批處理的例子,看起來有點(diǎn)啰嗦,只是為了說明用途:

1. step1 : 從 A 文件夾中讀取csv 文件,處理之后,寫入到B文件夾中(READ-PROCESS-WRITE)
2. step2 : 從 B 文件夾中讀取CSV文件 ,處理之后, 存儲到數(shù)據(jù)庫中(READ-PROCESS-WRITE).
3. step3 : 刪除B文件夾下的CSV文件。(用到單個task)
4. step4 : 從數(shù)據(jù)庫讀取數(shù)據(jù),處理之后,生成XML報表文件(READ-PROCESS-WRITE).
5. step5 : 讀取XML報表,并發(fā)送EMAIL給管理員(用到單個task)

用spring batch 我們可以如下定義這個job:

<job id="abcJob" xmlns="http://www.springframework.org/schema/batch">
 <step id="step1" next="step2">
  <tasklet>
  <chunk reader="cvsItemReader" writer="cvsItemWriter" 
     processor="itemProcesser" commit-interval="1" />
  </tasklet>
 </step>
 <step id="step2" next="step3">
  <tasklet>
  <chunk reader="cvsItemReader" writer="databaseItemWriter" 
     processor="itemProcesser" commit-interval="1" />
  </tasklet>
 </step>
 <step id="step3" next="step4">
  <tasklet ref="fileDeletingTasklet" />
 </step>
 <step id="step4" next="step5">
  <tasklet>
  <chunk reader="databaseItemReader" writer="xmlItemWriter" 
     processor="itemProcesser" commit-interval="1" />
  </tasklet>
 </step>
 <step id="step5">
  <tasklet ref="sendingEmailTasklet" />
 </step>
 </job>

整個 job 的執(zhí)行是存儲在數(shù)據(jù)庫中的,所以即使是某一個step出錯失敗,也不需要全部從頭開始執(zhí)行這個job.下面是一個真正的入門教程例子.

采用 jar包如下:

spring-batch-2.2.3 以上版本,但是我在2.2.3版本中發(fā)現(xiàn) org/springframework/batch/core/schema-mysql.sql 里面的的mysql 創(chuàng)建表的語句是有問題的,也就是少了“," 號導(dǎo)致的問題( NOT NULL, 后面幾個創(chuàng)建表的語句NOT NULL 后面少了逗號),當(dāng)然你可以自己修改后再執(zhí)行,執(zhí)行完畢后有如下幾個表:

xstream-1.3.jar 必須的。

jettison-1.3.3.jar也是必須的, 否則會出現(xiàn)

java.lang.NoClassDefFoundError: org/codehaus/jettison/mapped/MappedXMLOutputFactory錯誤。

另外我用的spring 是 3.1 版本的,可以下載相關(guān)jar包,還有apache common 相關(guān)jar包就可以了。

mysql-connect-java-5.1.jar 連接mysql  數(shù)據(jù)庫用的。

假設(shè)要將如下 csv 文件讀取出來處理之后,寫入到一個xml文件之中.

,"213,100",980,"mkyong", 29/7/2013
,"320,200",1080,"staff 1", 30/7/2013
,"342,197",1200,"staff 2", 31/7/2013

用 FlatFileItemReader 去讀取CSV 文件, 用 itemProcessor 去處理數(shù)據(jù),用 StaxEventItemWriter 去寫數(shù)據(jù)

job 的定義如下(job-hello-world.xml):

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/batch
  http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
  http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 ">
 <import resource="../config/context.xml" />
 <import resource="../config/database.xml" />
 <bean id="report" class="yihaomen.model.Report" scope="prototype" />
 <bean id="itemProcessor" class="yihaomen.CustomItemProcessor" />
 <batch:job id="helloWorldJob">
  <batch:step id="step1">
   <batch:tasklet>
    <batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter" processor="itemProcessor"
     commit-interval="10">
    </batch:chunk>
   </batch:tasklet>
  </batch:step>
 </batch:job>
 <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
  <property name="resource" value="classpath:cvs/input/report.csv" />
  <property name="lineMapper">
   <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
    <property name="lineTokenizer">
     <bean
      class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
      <property name="names" value="id,sales,qty,staffName,date" />
     </bean>
    </property>
    <property name="fieldSetMapper">
     <bean class="yihaomen.ReportFieldSetMapper" />
     
     <!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name
     <bean
      class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
      <property name="prototypeBeanName" value="report" />
     </bean>
      -->
    </property>
   </bean>
  </property>
 </bean>
 <bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
  <property name="resource" value="file:xml/outputs/report.xml" />
  <property name="marshaller" ref="reportMarshaller" />
  <property name="rootTagName" value="report" />
 </bean>
 <bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  <property name="classesToBeBound">
   <list>
    <value>yihaomen.model.Report</value>
   </list>
  </property>
 </bean>
</beans>

映射csv文件到 Report 對象并寫XML文件 (通過 jaxb annotations).

package yihaomen.model;
import java.math.BigDecimal;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "record")
public class Report {
 private int id;
 private BigDecimal sales;
 private int qty;
 private String staffName;
 private Date date;
 @XmlAttribute(name = "id")
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 @XmlElement(name = "sales")
 public BigDecimal getSales() {
  return sales;
 }
 public void setSales(BigDecimal sales) {
  this.sales = sales;
 }
 @XmlElement(name = "qty")
 public int getQty() {
  return qty;
 }
 public void setQty(int qty) {
  this.qty = qty;
 }
 @XmlElement(name = "staffName")
 public String getStaffName() {
  return staffName;
 }
 public void setStaffName(String staffName) {
  this.staffName = staffName;
 }
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
 @Override
 public String toString() {
  return "Report [id=" + id + ", sales=" + sales + ", qty=" + qty + ", staffName=" + staffName + "]";
 }
}

為了轉(zhuǎn)換日期,用了自定義的 FieldSetMapper. 如果沒有數(shù)據(jù)需要轉(zhuǎn)換, BeanWrapperFieldSetMapper 通過名稱name 去自動映射值。

package yihaomen;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
import yihaomen.model.Report;
public class ReportFieldSetMapper implements FieldSetMapper<Report> {
 private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
 @Override
 public Report mapFieldSet(FieldSet fieldSet) throws BindException {
  
  Report report = new Report();
  report.setId(fieldSet.readInt(0));
  report.setSales(fieldSet.readBigDecimal(1));
  report.setQty(fieldSet.readInt(2));
  report.setStaffName(fieldSet.readString(3));
  
  //default format yyyy-MM-dd
  //fieldSet.readDate(4);
  String date = fieldSet.readString(4);
  try {
   report.setDate(dateFormat.parse(date));
  } catch (ParseException e) {
   e.printStackTrace();
  }
  
  return report;
  
 }
}

在寫入數(shù)據(jù)之前調(diào)用itemProcessor 處理數(shù)據(jù)

package yihaomen;
import org.springframework.batch.item.ItemProcessor;
import yihaomen.model.Report;
public class CustomItemProcessor implements ItemProcessor<Report, Report> {
 @Override
 public Report process(Report item) throws Exception {
  
  System.out.println("Processing..." + item);
  return item;
 }
}

spring 配置文件和數(shù)據(jù)庫配置文件

<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.2.xsd">
 
 <!-- stored job-meta in memory -->
 <!-- 
 <bean id="jobRepository"
  class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
  <property name="transactionManager" ref="transactionManager" />
 </bean>
  -->
 
  <!-- stored job-meta in database -->
 <bean id="jobRepository"
  class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="transactionManager" ref="transactionManager" />
  <property name="databaseType" value="mysql" />
 </bean>
 
 <bean id="transactionManager"
  class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
 
 <bean id="jobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <property name="jobRepository" ref="jobRepository" />
 </bean>
 
</beans>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
 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.2.xsd
  http://www.springframework.org/schema/jdbc 
  http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">
 
  <!-- connect to MySQL database -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/test" />
  <property name="username" value="root" />
  <property name="password" value="" />
 </bean>
 
 <bean id="transactionManager"
  class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
 
 <!-- create job-meta tables automatically -->
 <jdbc:initialize-database data-source="dataSource">
  <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
  <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
 </jdbc:initialize-database>
 
</beans>

運(yùn)行程序

package yihaomen;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
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-hello-world.xml" 
   };
  
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(springConfig);
  
  JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
  Job job = (Job) context.getBean("helloWorldJob");
  try {
   JobExecution execution = jobLauncher.run(job, new JobParameters());
   System.out.println("Exit Status : " + execution.getStatus());
  } catch (Exception e) {
   e.printStackTrace();
  }
  System.out.println("Done");
 }
}



運(yùn)行結(jié)果 :

十二月 03, 2013 8:56:24 下午 org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
十二月 03, 2013 8:56:24 下午 org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
Processing...Report [id=1001, sales=213100, qty=980, staffName=yihaomen]
Processing...Report [id=1002, sales=320200, qty=1080, staffName=staff 1]
Processing...Report [id=1003, sales=342197, qty=1200, staffName=staff 2]
十二月 03, 2013 8:56:25 下午 org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status : COMPLETED
Done

結(jié)果生成了output.xml 在你工程目錄的 xml 目錄下。

整個源代碼,除去jar包之后下載:Spring batch 入門教程下載

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot項(xiàng)目啟動時如何用命令動態(tài)指定環(huán)境

    Springboot項(xiàng)目啟動時如何用命令動態(tài)指定環(huán)境

    這篇文章主要介紹了Springboot項(xiàng)目啟動時如何用命令動態(tài)指定環(huán)境的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java日期操作工具類(獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù))

    java日期操作工具類(獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù))

    這篇文章主要為大家詳細(xì)介紹了java日期操作工具類,包括獲取指定日期、日期轉(zhuǎn)換、相隔天數(shù)等操作,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Spring事件監(jiān)聽機(jī)制使用和原理示例講解

    Spring事件監(jiān)聽機(jī)制使用和原理示例講解

    Spring事件監(jiān)聽機(jī)制是一個很不錯的功能,我們在進(jìn)行業(yè)務(wù)開發(fā)的時候可以引入,在相關(guān)的開源框架中也是用它的身影,比如高性能網(wǎng)關(guān)ShenYu中就使用了Spring事件監(jiān)聽機(jī)制來發(fā)布網(wǎng)關(guān)的更新數(shù)據(jù),它可以降低系統(tǒng)的耦合性,使系統(tǒng)的擴(kuò)展性更好
    2023-06-06
  • SpringBoot自定義注解開發(fā)指南

    SpringBoot自定義注解開發(fā)指南

    在開發(fā)SpringBoot程序的過程中,有可能與其他業(yè)務(wù)系統(tǒng)進(jìn)行對接開發(fā),獲取封裝公共的API接口等等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot自定義注解的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Javaweb項(xiàng)目啟動Tomcat常見的報錯解決方案

    Javaweb項(xiàng)目啟動Tomcat常見的報錯解決方案

    Java Web項(xiàng)目啟動Tomcat時可能會遇到各種錯誤,本文就來介紹一下Javaweb項(xiàng)目啟動Tomcat常見的報錯解決方案,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • SpringIOC DI循環(huán)依賴實(shí)例詳解

    SpringIOC DI循環(huán)依賴實(shí)例詳解

    這篇文章主要介紹了SpringIOC——DI循環(huán)依賴,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • java面向?qū)ο蟮娜筇匦灾焕^承用法實(shí)例分析

    java面向?qū)ο蟮娜筇匦灾焕^承用法實(shí)例分析

    這篇文章主要介紹了java面向?qū)ο蟮娜筇匦灾焕^承用法,結(jié)合實(shí)例形式分析了java面向?qū)ο蟪绦蛟O(shè)計中繼承的基本原理與具體使用方法,需要的朋友可以參考下
    2019-11-11
  • Springboot+Hutool自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏

    Springboot+Hutool自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏

    我們在項(xiàng)目中會處理敏感數(shù)據(jù)時,通常需要對這些數(shù)據(jù)進(jìn)行脫敏,本文主要使用了Springboot整合Hutool來自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏,感興趣的可以理解下
    2023-10-10
  • Springboot整合SpringSecurity的完整案例詳解

    Springboot整合SpringSecurity的完整案例詳解

    Spring Security是基于Spring生態(tài)圈的,用于提供安全訪問控制解決方案的框架,Spring Security登錄認(rèn)證主要涉及兩個重要的接口 UserDetailService和UserDetails接口,本文對Springboot整合SpringSecurity過程給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2024-01-01
  • 深入淺出學(xué)習(xí)AQS組件

    深入淺出學(xué)習(xí)AQS組件

    AQS ( AbstractQueuedSynchronizer)是一個用來構(gòu)建鎖和同步器的框架,使用AQS能簡單且高效地構(gòu)造出應(yīng)用廣泛的大量的同步器,下面小編和大家來一起學(xué)習(xí)一下吧
    2019-05-05

最新評論