基于spring batch,写一段示例代码,可以通过指定的param传入job和step中进行使用,step采取reader和wirter的读法
时间: 2024-03-31 21:33:53 浏览: 114
[原创]Spring Batch 示例程序
5星 · 资源好评率100%
好的,以下是一个基于 Spring Batch 的示例代码,可以通过传入参数来指定 Job 和 Step,Step 中采用了 Reader 和 Writer 的方式进行读写:
```java
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
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.core.launch.JobLauncher;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobLauncher jobLauncher;
// 读取 CSV 文件的 Reader
@Bean
public FlatFileItemReader<Person> reader() {
FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
reader.setResource(new FileSystemResource("persons.csv"));
reader.setLineMapper(new DefaultLineMapper<Person>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[] { "firstName", "lastName" });
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
setTargetType(Person.class);
}});
}});
return reader;
}
// 处理 Person 对象的 Processor
@Bean
public ItemProcessor<Person, Person> processor() {
return person -> {
person.setFullName(person.getFirstName() + " " + person.getLastName());
return person;
};
}
// 将 Person 对象写入数据库的 Writer
@Bean
public ItemWriter<Person> writer() {
return items -> {
for (Person person : items) {
System.out.println("Writing person: " + person.getFullName());
}
};
}
// Job
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(step())
.build();
}
// Step
@Bean
public Step step() {
return stepBuilderFactory.get("step")
.<Person, Person>chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
public void run(String jobName, String stepName) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("jobName", jobName)
.addString("stepName", stepName)
.toJobParameters();
jobLauncher.run(jobRegistry.getJob(jobName), jobParameters);
}
}
```
可以在运行时传入指定的 Job 和 Step 名称,例如:
```java
BatchConfiguration batchConfiguration = new BatchConfiguration();
batchConfiguration.run("job", "step");
```
注意:以上代码仅为示例,实际使用时需要根据具体需求进行修改。
阅读全文