Spring batch 实例

10 篇文章 0 订阅

 Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。
 Spring Batch可以提供大量的,可重复的数据处理功能,包括日志记录/跟踪,事务管理,作业处理统计工作重新启动、跳过,和资源管理等重要功能。
       业务方案:
1、批处理定期提交。
2、并行批处理:并行处理工作。
3、企业消息驱动处理
4、大规模的并行处理
5、手动或是有计划的重启
6、局部处理:跳过记录(如:回滚)


 每个Batch都会包含一个Job。Job就像一个容器,这个容器里装了若干Step,Batch中实际工作的也就是这些Step
 step->ItemReader用来读取数据,ItemProcessor用来处理数据,ItemWriter用来写数据
 JobLauncher用来启动Job,JobRepository是上述处理提供的一种持久化机制,它为JobLauncher,Job,和Step实例提供CRUD操作。

下面是一个实例:

还是先说需要的jar包

pom

spring jar包:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>servlet-api-2.5</artifactId>
            <version>6.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
然后是batch的

<!-- Spring Batch dependencies -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>

web.xml

<servlet>
        <servlet-name>Spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet-mapping>
        <servlet-name>Spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
spring-batch.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">

    <!-- spring batch -jobRepository -->
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />

    <!-- spring - transactionManager -->
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <!-- batch luncher -->
    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>

    <batch:job id="helloWorldJob">
        <batch:step id="step_hello" next="step_world">
            <batch:tasklet ref="hello" transaction-manager="transactionManager"></batch:tasklet>
        </batch:step>
        <batch:step id="step_world">
            <batch:tasklet>
                <batch:chunk reader="xxcReader" processor="xxcProcessor" writer="xxcWriter" commit-interval="10" />
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="hello" class="com.batch.tasklet.WriteTasklet">
        <property name="message" value="Hello" />
    </bean>

    <bean id="xxcReader" class="com.batch.reader.XxcReader">
        <property name="lineMapper" ref="lineMapper"/>
        <property name="resource" value="/com/batch/common/user.txt"/>  
    </bean>
    <bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">  
        <property name="lineTokenizer" ref="lineTokenizer"/>  
        <property name="fieldSetMapper" ref="fieldSetMapper"/>  
    </bean>  
    <bean id="fieldSetMapper" class="com.batch.map.XxcMapper"/>  
    <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"/>  
    
    <bean id="xxcProcessor" class="com.batch.processor.XxcProcessor"/>
    <bean id="xxcWriter" class="com.batch.writer.XxcWriter"/>
    
</beans>
然后下面都是Java代码了。。。

先看一下bean文件

/*
 * Creation : 6 Jan 2016
 */
package com.batch.bean;

/**
 * The Class Message.
 */
public class Message {

    /** The code. */
    private String code;

    /** The value. */
    private String value;

    /**
     * Gets the code.
     * 
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * Sets the code.
     * 
     * @param code the new code
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * Gets the value.
     * 
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * Sets the value.
     * 
     * @param value the new value
     */
    public void setValue(String value) {
        this.value = value;
    }
}
/*
 * Creation : 6 Jan 2016
 */
package com.batch.bean;

/**
 * The Class Users.
 */
public class Users {

    /** The name. */
    private String name;

    /** The value. */
    private String value;

    /**
     * Gets the name.
     * 
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name.
     * 
     * @param name the new name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the value.
     * 
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * Sets the value.
     * 
     * @param value the new value
     */
    public void setValue(String value) {
        this.value = value;
    }
}
然后根据batch文件中的依次:

/*
 * Creation : 6 Jan 2016
 */
package com.batch.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;

public class WriteTasklet implements Tasklet {

    /** Message */
    private String message;

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
        System.out.println(message);
        return RepeatStatus.FINISHED;
    }
}
</pre><p></p><p>Map把读到的数据放到对象中</p><p></p><pre name="code" class="java">/*
 * Creation : 6 Jan 2016
 */
package com.batch.map;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import com.batch.bean.Users;

public class XxcMapper implements FieldSetMapper<Users> {

    @Override
    public Users mapFieldSet(FieldSet fieldSet) throws BindException {
        Users user = new Users();
        user.setName(fieldSet.readString(0));
        user.setValue(fieldSet.readString(1));
        return user;
    }

}


因为Read的操作都在配置文件中做完了,我这里就是个空,你也可以直接在配置文件中class的路径直接写成FlatFileItemReader,这样XXCReader文件就不要创建了

/*
 * Creation : 6 Jan 2016
 */
package com.batch.reader;

import org.springframework.batch.item.file.FlatFileItemReader;

import com.batch.bean.Users;

public class XxcReader extends FlatFileItemReader<Users> {

}
/*
 * Creation : 6 Jan 2016
 */
package com.batch.processor;

import org.springframework.batch.item.ItemProcessor;

import com.batch.bean.Message;
import com.batch.bean.Users;

public class XxcProcessor implements ItemProcessor<Users, Message> {

    @Override
    public Message process(Users item) throws Exception {
        Message message = new Message();
        message.setCode(item.getName() + "---name");
        message.setValue("000---" + item.getValue());
        return message;
    }
}
/*
 * Creation : 6 Jan 2016
 */
package com.batch.writer;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

import com.batch.bean.Message;

public class XxcWriter implements ItemWriter<Message> {

    @Override
    public void write(List<? extends Message> items) throws Exception {
        for (Message item : items) {
            System.out.println(item.getCode());
            System.out.println(item.getValue());
        }
    }

}

这个是user.txt

jim,aaa
tom,bbb
mkl,ccc

好了,现在就全部写完了

运行一下

/*
 * Creation : 6 Jan 2016
 */
package com.batch;

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 JobLaunch {
    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:spring-batch.xml" });
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("helloWorldJob");

        try {
            JobExecution result = launcher.run(job, new JobParameters());
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


直接 run Main function

看一下控制台的结果:









  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Batch权威指南》是一本非常有价值的书籍,它全面介绍了Spring Batch框架的核心概念、使用方法和最佳实践,对于想要学习和掌握Spring Batch的开发人员来说,是一本必不可少的参考书。 该书主要分为四个部分。第一部分是Spring Batch的基础知识,主要介绍了批处理的概念、Spring Batch的架构和主要组件,以及如何创建和配置Spring Batch的应用程序。第二部分是作业配置,包括如何定义和配置作业、步骤和任务等,以及作业参数和作业执行的监控与管理。第三部分是读取和写入数据,介绍了Spring Batch中各种读取和写入数据的方式,包括文件、数据库、REST接口等,并且给出了一些常见的使用示例。第四部分是高级特性,包括事务和并发处理、异常处理、转换和验证数据等。 该书的优点是内容全面详细,作者对Spring Batch框架的各个方面进行了深入的讲解,并且提供了丰富的实例和代码,帮助读者更好地理解和应用知识。此外,该书还对一些常见的问题和注意事项进行了阐述,帮助读者避免一些常见的错误。 总之,如果你想要学习和应用Spring Batch框架,我强烈推荐你阅读《Spring Batch权威指南》这本书。它将帮助你全面了解和掌握Spring Batch的开发技巧,提高批处理应用程序的开发效率和质量。无论你是初学者还是有一定经验的开发人员,这本书都能为你提供宝贵的参考和指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaxCode-1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值