这里我们通过一个简单的实例来对springbatch做一个入门的体会。刚刚好,看见你幸福的样子,于是幸福着你的幸福。
springbatch的入门案例
简单项目的结构如下:
一、引入maven的springbatch的依赖
<dependencies> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>LATEST</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> </dependencies>
二、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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <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.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" 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-3.0.xsd"> <bean:import resource="application.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.linux.huhx.batch.WriteTasklet"> <bean:property name="message" value="Hello "></bean:property> </bean:bean> <bean:bean id="world" class="com.linux.huhx.batch.WriteTasklet"> <bean:property name="message" value=" World!"></bean:property> </bean:bean> </bean:beans>
四、WriteTasklet.java的内容如下
package com.linux.huhx.batch; 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; /** * @Author: huhx * @Date: 2017-09-14 上午 9:56 * @Desc: */ public class WriteTasklet implements Tasklet { private String message; public void setMessage(String message) { this.message = message; } @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { System.out.print(message); return RepeatStatus.FINISHED; } }
五、JobLaunch.java的内容如下
package com.linux.huhx.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; /** * @Author: huhx * @Date: 2017-09-14 上午 9:58 * @Desc: */ public class JobLaunch { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("config/batch/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(); } } }
运行的结果是先打印Hello 后打印 World!