spring batch简单使用示例
时间: 2023-08-06 10:05:53 浏览: 130
下面是一个简单的Spring Batch使用示例:
1. 首先需要在项目中导入Spring Batch的依赖,可以在pom.xml文件中添加以下代码:
```xml
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>4.3.2</version>
</dependency>
```
2. 创建一个包含读取、处理和写入的任务类。
```java
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import java.util.List;
public class MyTask {
private ItemReader<String> reader; // 读取数据的接口
private ItemProcessor<String, String> processor; // 处理数据的接口
private ItemWriter<String> writer; // 写入数据的接口
public void setReader(ItemReader<String> reader) {
this.reader = reader;
}
public void setProcessor(ItemProcessor<String, String> processor) {
this.processor = processor;
}
public void setWriter(ItemWriter<String> writer) {
this.writer = writer;
}
public void execute() throws Exception {
String item = null;
while ((item = reader.read()) != null) { // 读取数据
String processedItem = processor.process(item); // 处理数据
writer.write(processedItem); // 写入数据
}
}
}
```
3. 创建一个Job对象
```java
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class MyJobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private ItemReader<String> reader;
@Autowired
private ItemProcessor<String, String> processor;
@Autowired
private ItemWriter<String> writer;
@Bean
public Job myJob() {
return jobBuilderFactory.get("myJob")
.start(step())
.build();
}
@Bean
public Step step() {
return stepBuilderFactory.get("step")
.<String, String>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
}
```
4. 创建一个Main类来启动Job
```java
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfig.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job myJob = context.getBean("myJob", Job.class);
JobParameters jobParameters = new JobParametersBuilder()
.addLong("timestamp", System.currentTimeMillis())
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(myJob, jobParameters);
System.out.println("Job Execution Status : " + jobExecution.getStatus());
}
}
```
以上就是一个简单的Spring Batch使用示例,它可以读取、处理和写入数据。需要注意的是,这只是一个简单的示例,实际使用中可能还需要添加更多的功能。
阅读全文