Spring Batch -----Hello world!
来源:互联网 发布:服装库存软件 编辑:程序博客网 时间:2024/06/08 09:44
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 3.0 + batch 2.1.8)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
。。。
at com.zhuhan.springbatch.JobLaunch.main(JobLaunch.java:16)
加入一个aopalliance.jar jar包就可以了
- Spring Batch -----Hello world!
- Spring Batch之Hello World
- Spring Batch Example – Hello World Project
- Spring Batch Hello World Example(一)
- Spring Batch 之 Sample(Hello World)(三)
- Spring Batch 之 Sample(Hello World)(三)
- Spring Batch 之 Sample(Hello World)(三)
- Spring Batch 之 Sample(Hello World)(三)
- Spring Batch 之 Sample(Hello World)(三)
- Spring Batch 之 Sample(Hello World)(三)
- Hello World Spring MVC Hello World
- Spring之Hello World
- spring编写hello world
- Spring之Hello World
- Spring 3.2 Hello World
- Spring Hello World
- Spring入门hello world
- spring mvc Hello World
- java 四大作用域(public protected default private)
- Swift_构造过程(指定构造器、convenience便利构造器、可失败构造器 init?(...))
- LoadRunner脚本编写之一
- JS内置对象-String对象、Date日期对象、Array数组对象、Math对象
- phantomjs robot framework [Errno 9] Bad file descriptor
- Spring Batch -----Hello world!
- 基础算法之二 直接插入排序的三种实现
- Google面试题:数字计数
- 无法打开包含文件:“C:\Program Files (x86)\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\src\mfc\occimpl.h”
- Dagger2
- raw格式数据转BMP格式(OpenCV)
- 如何使用Dockerfile构建镜像
- okhttp3去掉https证书的client
- slidingMenu (侧拉)