gpt4 book ai didi

java - Spring Boot 批处理调度程序运行一次

转载 作者:太空宇宙 更新时间:2023-11-04 09:41:02 25 4
gpt4 key购买 nike

我从2.1.4版本开始学习Spring Boot Batch

我想在调度程序中运行我的作业,并且该作业仅运行一次。我的意思是 ItemProcessor 和 ItemWriter 仅运行一次。 ItemReader 每次都会运行。任何人都知道我做错了什么。将来,我想将调度程序更改为 Java WatchService 并将 filePath 传递给作业,但现在 filePath 的参数就像函数参数中的字符串。这是我的代码:

这是我的读者:

@Component
public class UserReaderImpl {
@StepScope
public ItemReader<UserCsvStructure> read(String filepath) {
FlatFileItemReader<UserCsvStructure> reader = new FlatFileItemReader();
reader.setLinesToSkip(1);
reader.setResource(new FileSystemResource(filepath));
reader.setLineMapper(new DefaultLineMapper<UserCsvStructure>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(new String[]{"firstName","lastName","email"});
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<UserCsvStructure>() {
{
setTargetType(UserCsvStructure.class);
}
});
}
});
return reader;
}
}

这在我的 ItemProcessor 中

@StepScope
@Component
public class UserProcessorImpl implements ItemProcessor<UserCsvStructure, User> {
@Override
public User process(UserCsvStructure userCsvStructure) throws Exception {
return User.builder()
.email(userCsvStructure.getEmail())
.firstName(userCsvStructure.getFirstName())
.lastName(userCsvStructure.getLastName())
.build();
}
}

这是我的 ItemWriter

@Component
@StepScope
public class UserWriterImpl implements ItemWriter<User>{
@Autowired
private UserRepository userRepository;

@Override
public void write(List<? extends User> list) throws Exception {
System.out.println(list);
userRepository.saveAll(list);
}
}

这是我的配置

@Component
public class UserBatchCsvConfig {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
private UserReaderImpl userReader;

@Autowired
private UserWriterImpl userWriter;

@Autowired
private UserProcessorImpl userProcessor;

public Job csvFileToDatabaseJob(UserJobCompletionNotificationListener listener, String fileName) {
return jobBuilderFactory.get("userCsvProcess")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(csvFileToDatabaseStep(fileName))
.end()
.build();
}

private Step csvFileToDatabaseStep(String fileName) {
return stepBuilderFactory.get("userCsvProcess")
.<UserCsvStructure, User>chunk(1)
.reader(userReader.read(fileName))
.processor(userProcessor)
.writer(userWriter)
.build();
}

}

最后一个类是我的调度程序:

@Component
public class UserCsvProcessor {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private UserBatchCsvConfig job;

@Autowired
private UserJobCompletionNotificationListener userJobCompletionNotificationListener;

@Scheduled(fixedDelay = 10000)
public void runJob() throws Exception {
jobLauncher.run(job.csvFileToDatabaseJob(userJobCompletionNotificationListener, "C:\\Users\\Anik\\Desktop\\angular\\test.csv"), new JobParameters());
}
}

最佳答案

我知道我应该在代码中添加什么在 UserCsvProcessor 类中,我需要将计划函数更改为:

@Scheduled(fixedDelay = 10000)
public void runJob() throws Exception {
JobParameters params = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
jobLauncher.run(job.csvFileToDatabaseJob(userJobCompletionNotificationListener, "C:\\Users\\Anik\\Desktop\\angular\\test.csv"), params);
}

如果有人有其他想法或更好的想法,只需添加答案

关于java - Spring Boot 批处理调度程序运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55984262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com