Spring Batch学习之路- HelloWord(二)

1. 基础部分

通过实例探讨”Hello World!”了解基本配置和实现

说明:


  1. 本实例使用的是spring-batch-2.2.7
  2. 本实例使用spring版本:3.2.9.RELEASE,包含:

  1. spring-core
  2. spring-beans
  3. spring-context
  4. spring-aop
  5. spring-tx

配置ItemReader、ItemProcessor和ItemWriter,完成”Hello World!”的输出

工程结构如下图:

+说明:

  • JobLaunch.java:启动Bath
  • writeTasklet.java:完成输出工作
  • pom.xml:maven 配置
  • applicationContext.xml:spring batch 基本配置
  • batch_hello.xml:hello world step 任务配置
详细信息:
+pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd“>
<modelVersion>4.0.0</modelVersion>
<groupId>com.springbatch.hello</groupId>
<artifactId>SpringBatchHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
</dependencies>
</project>


+JobLaunch.java

package org.june.springbatch.sample.helloworld;

/**
* Created by EX-XUJUNHONG010 on 2015-09-25.
*/

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("batch_hello.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="+result.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}


+writeTasklet.java

package org.june.springbatch.sample.helloworld;

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;

/**
* Created by EX-XUJUNHONG010 on 2015-09-25.
*/
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;
}

}


+applicationContext.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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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>


+batch_hello.xml

<?xml version=”1.0” encoding=”UTF-8”?>
<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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd“>

<import resource="applicationContext.xml"/>

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

<bean id="hello" class="org.june.springbatch.sample.helloworld.writeTasklet">
    <property name="message" value="Hello " />
</bean>

<bean id="world" class="org.june.springbatch.sample.helloworld.writeTasklet">
    <property name="message" value=" World!" />
</bean>

</beans>


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值