`
mowengaobo
  • 浏览: 166224 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Spring Batch 之 Sample(Hello World)(三)

 
阅读更多

      通过前面两篇关于Spring Batch文章的介绍,大家应该已经对Spring Batch有个初步的概念了。这篇文章,将通过一个”Hello World!”实例,和大家一起探讨关于Spring Batch的一些基本配置和实现。使大家从开发的角度对Spring Batch有一个真切的体会。

      说明:1,本实例使用的是spring-batch 2.1.8

             2,本实例没有像前面讲的那样配置ItemReader、ItemProcessor和ItemWriter,而是之间在Step中调用Tasklet,由Tasklet完成”Hello World!”的输出。

      工程结构如下图:

                    

      JobLaunch.java类用来启动Bath,writeTasklet.java用来完成输出工作。application.xml用来配置一些Spring信息,batch.xml配置Job信息。

application.xml文件配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">

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

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

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

 

      jobLauncher负责batch的启动工作,jobRepository负责job的整个运行过程中的CRUD操作,transactionManager负责事务的管理操作。

      batch.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
    xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

<bean:import resource="applicationContext.xml"/>

<job id="helloWorldJob">
<step id="step_hello" next="step_world">
<tasklet ref="hello" transaction-manager="transactionManager"></tasklet>
</step>
<step id="step_world">
<tasklet ref="world" transaction-manager="transactionManager"></tasklet>
</step>
</job>

<bean:bean id="hello" class="com.wanggc.springbatch.sample.helloworld.writeTasklet">
<bean:property name="message" value="Hello "></bean:property>
</bean:bean>

<bean:bean id="world" class="com.wanggc.springbatch.sample.helloworld.writeTasklet">
<bean:property name="message" value=" World!"></bean:property>
</bean:bean>
</bean:beans>
 

      配置了一个ID为helloWorldJob的job,此job有两个Step : step_hello和step_world,前者负责输出“Hello ”,后者负责输出“World!”,当第一个Step完成以后,执行第二个Step。 

      writeTasklet类的代码如下:

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;
    }

}
 

      此类中定义了一个message属性,通过batch.xml的“hello”和“world” Bean为其注入值。 execute方法,是由Tasklet接口继承而来的,是Tasklet实现业务逻辑的地方。此实例中只是简单的输出Message信息后,直接返回。

      启动类JobLaunch类的代码如下:

public class JobLaunch {

/**
     * @param args
*/
public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "batch.xml");
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("helloWorldJob");

try {
/* 运行Job */
            JobExecution result = launcher.run(job, new JobParameters());
/* 处理结束,控制台打印处理结果 */
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

      本例通过Spring配置的方式取得JobLauncher和Job对象,然后由JobLauncher的run方法启动job,参数JobParameters是标志job的一些参数,处理结束后,控制台输出处理结果。

      上面就是通过SpringBatch运行一个"Hello World”程序所需要的基本配置。由于其优势是处理大批量的数据,所以仅仅为了输出"Hello World"而编写这么多代码和配置文件,确实显得有些笨拙,也体现不出其优越性。

      下次,将通过读取一个CSV文件,经过简单的处理,再写入另外一个CSV文件的实例,与大家共同探讨SpringBatch的应用。

 

分享到:
评论

相关推荐

    Spring Batch HelloWorld 例子

    博文链接:https://hintcnuie.iteye.com/blog/198673

    SpringBatch-HelloWorld

    SpringBatch Tasklet sample

    SpringBatch+Spring+Mybatis+MySql (spring batch 使用jar)

    Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...

    Spring Boot整合Spring Batch,实现批处理

    4. **创建读取器(ItemReader)、处理器(ItemProcessor)和写入器(ItemWriter)**:这是Spring Batch的三个核心组件,它们分别负责从数据源读取数据、处理数据和将处理后的数据写入目标。例如,我们可以使用...

    Spring Batch in Action英文pdf版

    Spring Batch是一个开源的轻量级、全面的批处理框架,它是为了解决企业应用中的大规模数据处理需求而设计的。Spring Batch in Action是一本专注于Spring Batch框架的书籍,由Arnaud Cogoluègnes、Thierry Templier...

    The Definitive Guide to Spring Batch, 2nd Edition.epub

    The Definitive Guide to Spring Batch takes you from the “Hello, World!” of batch processing to complex scenarios demonstrating cloud native techniques for developing batch applications to be run on...

    springbatch 详解PDF附加 全书源码 压缩包

    **三、Spring Batch 源码分析** 在提供的源码中,你可以深入理解 Spring Batch 的内部工作机制,包括: 1. **Job Configuration**: 查看如何配置 Job 和 Step,了解如何定义 Item Readers、Processors 和 Writers...

    quartz整合springbatch动态集群定时实现mysql参考

    Quartz和Spring Batch是两种非常重要的Java开源框架,它们在企业级应用开发中扮演着重要角色。Quartz主要用于任务调度,而Spring Batch则专注于批量处理。在这个“quartz_springbatch_dynamic”项目中,我们将看到...

    Spring Batch批处理框架

    Spring Batch的事务管理是其核心特性之一。Spring Batch要求每个步骤都运行在一个事务上下文中。事务保证了步骤中进行的操作要么全部成功,要么全部回滚,这为批处理作业提供了数据一致性和完整性保障。 4. 错误...

    Spring Batch sample project PatternMatchingCompositeLineMapper

    10. `Spring-batch-hello-world-tasklet`: 这是Spring Batch中最基本的Hello World任务示例,通常用于演示如何定义和执行一个简单的批处理任务。 通过这些文件,我们可以深入理解Spring Batch如何通过`...

    基于Spring Batch的大数据量并行处理

    ### 基于Spring Batch的大数据量并行处理 #### 概述 Spring Batch是一款用于高效处理大量数据的开源框架,特别适用于批处理任务。它由Spring Source与Accenture合作开发,结合了双方在批处理架构和技术上的优势,...

    Spring Batch API(Spring Batch 开发文档).CHM

    Spring Batch API(Spring Batch 开发文档).CHM。 官网 Spring Batch API,Spring Batch 开发文档

    spring-batch包

    Spring Batch 是一个强大的、全面的批处理框架,用于处理大量数据的处理任务。它由 Spring 框架提供支持,因此具有高度的可配置性和可扩展性,适用于各种企业级应用。Spring Batch 4.0.0 版本是该框架的一个重要版本...

    Spring batch in action

    Spring Batch中的作业通常由三个主要组件构成:读取器(Reader)、处理器(Processor)和写入器(Writer),它们协同工作以完成整个数据处理流程。 本书的第二部分深入探讨了Spring Batch的核心概念,包括如何配置...

    SpringBatch的简单Sample

    在"SpringBatch的简单Sample"中,我们将探讨两种主要的处理模式:Tasklet和Chunk。 1. **Tasklet**: Tasklet 是SpringBatch中的基本执行单元,它可以被视为一个独立的任务。Tasklet 不是基于数据集的,而是执行一...

    springBoot+springBatch批量处理数据demo

    SpringBoot和SpringBatch是两个非常重要的Java开发框架,它们在企业级应用开发中扮演着重要角色。SpringBoot简化了Spring应用程序的配置和启动过程,而SpringBatch则专注于批处理任务,提供了一套全面且可扩展的解决...

    spring Batch实现数据库大数据量读写

    Spring Batch 是一个强大的、可扩展的Java框架,专门用于处理批量数据处理任务,包括大量数据库数据的读取、处理和写入。它被设计为在企业级应用中处理大规模、高吞吐量的工作负载。本篇文章将深入探讨如何利用...

    spring-batch分区处理示例

    Spring Batch 是一个强大的Java框架,专门用于处理批量数据处理任务。在Spring Batch中,分区处理是一种优化策略,它将大型工作负载分解成多个较小、独立的任务,这些任务可以在不同的线程或甚至不同的节点上并行...

    SpringBatch-DataMigration SpringBatch数据迁移项目

    mybatis、springBatch、mysql、quartz、spring、springMVC 部署说明: 本项目为两个数据库,由一个数据库的表向另外一个数据库的表做数据迁移,其中数据库脚本在:/src/main/resources/sql/下面(其中data_rep中的表...

Global site tag (gtag.js) - Google Analytics