当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

Spring Batch Sample

发表于: 2014-03-30   作者:antlove   来源:转载   浏览次数:
摘要:      1. SampleDecider.java package sample; import org.apache.log4j.Logger; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters

 

   1. SampleDecider.java

package sample;

import org.apache.log4j.Logger;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;

public class SampleDecider implements JobExecutionDecider {
	private Logger logger = Logger.getLogger(SampleDecider.class);
	
	private String DECIDER_FLAG = "DECIDER_FLAG";
	
	@Override
	public FlowExecutionStatus decide(JobExecution jobExecution,
			StepExecution stepExecution) {
		
		logger.debug("execute the SampleDecider... ");
		JobParameters jobParameters  =jobExecution.getJobParameters();
		
		String flag = jobParameters.getString(DECIDER_FLAG);
		
		if(flag == null){
			flag = "";
		}
		logger.debug("the decider flag is "+flag);
		
		return new FlowExecutionStatus(flag);
	}

}

 

 

    2. SampleItemProcessor.java

package sample;

import org.apache.log4j.Logger;
import org.springframework.batch.item.ItemProcessor;

public class SampleItemProcessor implements ItemProcessor<String, String> {

	private Logger logger = Logger.getLogger(SampleItemProcessor.class);
	
	@Override
	public String process(String str) throws Exception {
		logger.debug("execute the SampleItemProcessor...");
		
		return str;
	}
}

  

    3. SampleItemReader.java

package sample;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

public class SampleItemReader implements ItemReader<String> {
	
	private Logger logger = Logger.getLogger(SampleItemReader.class);
	
	private List<String> list = new ArrayList<String>();
	
	public SampleItemReader(){
		list.add("1");
		list.add("2");
		list.add("3");
	}
	
	@Override
	public String read() throws Exception, UnexpectedInputException, ParseException,
			NonTransientResourceException {
		logger.debug("execute the SampleItemWriter... ");
		if(list.size()>0){
			return list.remove(0);
		}
		return null;
	}
}

 

    4. SampleItemWriter.java

package sample;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.item.ItemWriter;

public class SampleItemWriter implements ItemWriter<String> {
	private Logger logger = Logger.getLogger(SampleItemWriter.class);
	
	@Override
	public void write(List<? extends String> arg0) throws Exception {
		logger.debug("execute the SampleItemWriter... ");
	}
}

 

    5. DefaultConnection.java

package sample;

import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.Map;
import java.util.Properties;

public class DefaultConnection implements Connection {
          .....
}

 

    6. SampleDataSource.java

package sample;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

public class SampleDataSource implements DataSource{
       ....
}

 

    7. sample-job.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
	xmlns:beans="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.2.xsd">
	
	<!-- configure the job repository -->
	<beans:bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.SimpleJobRepository">
        <beans:constructor-arg>
            <beans:bean class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
        </beans:constructor-arg>
        <beans:constructor-arg>
            <beans:bean class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />
        </beans:constructor-arg>
        <beans:constructor-arg>
            <beans:bean class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
        </beans:constructor-arg>
        <beans:constructor-arg>
            <beans:bean class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
       </beans:constructor-arg>
	</beans:bean>
	
	<!-- configure the job launcher -->
	<beans:bean id="jobLauncher" class="launcher.DefaultSimpleJobLauncher">  
        <beans:property name="jobRepository" ref="jobRepository"/>  
    </beans:bean>
    
	<job id="sampleJob" restartable="true">
		<decision decider="sampleDecider" id="sampleDecision">
			<next on="*" to="sampleStep"/>
			<end on="end"/>
		</decision>
	
		<step id="sampleStep">
			<tasklet>
		      <chunk reader="sampleItemReader" 
		      		 writer="sampleItemWriter" 
		      		 processor="sampleItemProcessor"
		             commit-interval="100">
		      </chunk>
			</tasklet>
		</step>
	</job>
	
	<beans:bean id="sampleItemReader" class="sample.SampleItemReader"/>
	<beans:bean id="sampleItemWriter" class="sample.SampleItemWriter"/>
	<beans:bean id="sampleItemProcessor" class="sample.SampleItemProcessor"/>
	<beans:bean id="sampleDecider" class="sample.SampleDecider"/>

	<beans:bean id="dataSource" class="sample.SampleDataSource"/>
	
	<beans:bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<beans:property name="dataSource" ref="dataSource" />
	</beans:bean>
</beans:beans>

 

    8. Main.java 测试

package sample;

import org.springframework.batch.core.launch.support.CommandLineJobRunner;

public class Main {

	public static void main(String[] args) throws Exception {
		String[] params = {"sample/sample-job.xml","sampleJob","DECIDER_FLAG=end"};
		
		CommandLineJobRunner.main(params);
	}
}

 

 

Spring Batch Sample

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Spring Batch 之 Sample(XML文件操作)(五) 前篇关于Spring Batch的文章,讲述了Spring Batch 对CS
Spring Batch 之 Sample(CSV文件操作)(四) 本文将通过一个完整的实例,与大家一起讨论运用Spring
通过前面两篇关于Spring Batch文章的介绍,大家应该已经对Spring Batch有个初步的概念了。这篇文章
前篇关于Spring Batch的文章,讲述了Spring Batch 对XML文件的读写操作。 本文将通过一个完整的实例
前面关于Spring Batch的文章,讲述了SpringBatch对CSV文件的读写操作、对XML文件的操作,以及对固定
Spring Batch 之 Spring Batch 简介(一) Spring Batch是一个轻量级的,完全面向Spring的批处理框架
转http://www.coderli.com/spring-batch-intro-sample/ 官方文档地址:http://docs.spring.io/sprin
1. Intro to Spring Batch 1) Spring Batch is a framework for Batch Processing - Execution of a
Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:inf
Spring Batch Documentation: http://static.springsource.org/spring-batch/reference/index.html
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号