Spring Batch
2017年12月14日大约 2 分钟
Spring Batch
Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing,
transaction management, job processing statistics, job restart, skip, and resource management.
It also provides more advanced technical services and features that will enable extremely high-volume
and high performance batch jobs through optimization and partitioning techniques.
初步感觉是为做一系列的事情提供了一个完善的管理框架。
Quick Start
本案例演示如何执行一个步骤,然后执行另一个步骤。
目录结构
├── pom.xml
├── spring-batch-hw.iml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── ryo
│ │ └── spring
│ │ └── batch
│ │ └── hw
│ │ ├── HelloTasklet.java
│ │ ├── Main.java
│ │ └── WorldTasklet.java
│ └── resources
│ ├── application.xml
│ └── spring-batch.xml
文件内容
- pom.xml
引入必须的 jar
spring-data
com.ryo
1.0-SNAPSHOT
4.0.0
spring-batch-hw
io.spring.platform
platform-bom
1.1.2.RELEASE
pom
import
org.springframework
spring-core
org.springframework.batch
spring-batch-core
commons-logging
commons-logging
- HelloTasklet.java
打印 "hello"
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 HelloTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("hello");
return RepeatStatus.FINISHED;
}
}
- WorldTasklet.java
打印 "hello"
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 WorldTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("world");
return RepeatStatus.FINISHED;
}
}
- Main.java
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws JobParametersInvalidException,
JobExecutionAlreadyRunningException,
JobRestartException,
JobInstanceAlreadyCompleteException {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-batch.xml");
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("helloWorldJob");
/* 运行Job */
JobExecution result = launcher.run(job, new JobParameters());
/* 处理结束,控制台打印处理结果 */
System.out.println(result.toString());
}
}
- spring-batch.xml
- application.xml
Run & Result
运行 Main.main()
信息: Executing step: [step_hello]
hello
Dec 14, 2017 10:50:07 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
信息: Executing step: [step_world]
world
Dec 14, 2017 10:50:07 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
JobExecution: id=0, version=2, startTime=Thu Dec 14 22:50:07 CST 2017, endTime=Thu Dec 14 22:50:07 CST 2017,
lastUpdated=Thu Dec 14 22:50:07 CST 2017, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=,
job=[JobInstance: id=0, version=0, Job=[helloWorldJob]], jobParameters=[{}]
信息: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
贡献者
binbin.hou