英文:
Spring batch job runs automatically
问题
我正在使用 Spring Batch 读取 CSV 文件并将其写入数据库,使用控制器触发器。在启动应用程序之前,在我从浏览器 URL 发出请求之前,我可以在启动时从我的读取器中看到打印语句。尽管我的处理器和写入器不会打印出来,它们位于我已经自动装配的不同类中。这是因为读取器是一个 bean 吗?
我在应用程序启动时在日志中看到了来自我的 FlatFileItemReader
的打印语句。但是只有当我访问控制器 URL 时,我的处理器和写入器的打印语句才会显示在控制台中。
我尝试在 application.properties
文件中添加 spring.batch.job.enabled=false
,但它无法阻止读取器 bean 的执行。如何防止在 SpringBatchConfig 类中自动执行读取器 bean:
SpringBatchConfig 类:
@Configuration
@EnableBatchProcessing
public class SpringBatchConfig {
// ... 其他自动装配的字段 ...
@Bean
public FlatFileItemReader<User> itemReader() {
// ... 读取器的配置 ...
}
@Bean
public LineMapper<User> lineMapper() {
// ... 行映射器的配置 ...
}
@Bean
public Step step1() throws Exception {
return stepBuilderFactory.get("step1")
.<User, User>chunk(100)
.reader(itemReader())
.processor(processor1)
.writer(writer1)
.build();
}
@Bean
public Job job() throws Exception {
return this.jobBuilderFactory.get("BATCH JOB")
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
}
DBWriter 类:
@Component
public class DBWriter implements ItemWriter<User> {
// ... 写入器的配置 ...
}
Processor 类:
@Component
public class Processor implements ItemProcessor<User, User> {
// ... 处理器的配置 ...
}
Controller 类:
@RestController
@RequestMapping("/load")
public class LoadController {
// ... 控制器的配置 ...
}
英文:
I'm using a spring batch to read a CSV file and write it to the DB, using the controller trigger. On starting the application, before I hit from the browser url, I see the print statements from my reader, on the startup. Although it doesn't print it for my processor or writer, which are in separate classes which I have autowired. Is it because the reader is a bean?
I see the print statements from my FlatFileItemReader
in the log on the application startup. But the print statements for my processor and writer only show up in the console when I hit the controller url.
I've tried adding spring.batch.job.enabled=false
in the application.properties
file, but it doesnt stop the execution of the reader bean. How can I prevent auto execution of the reader bean in the SpringBatchConfig class:
SpringBatchConfig class:
@Configuration
@EnableBatchProcessing
public class SpringBatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
@Autowired
private DBWriter writer1;
@Autowired
private Processor processor1;
//Step 1 - CSV to DB
@Bean
public FlatFileItemReader<User> itemReader() {
FlatFileItemReader<User> flatFileItemReader = new FlatFileItemReader<>();
flatFileItemReader.setResource(new FileSystemResource("src/main/resources/users.csv"));
flatFileItemReader.setName("CSV-Reader");
flatFileItemReader.setLinesToSkip(1);
flatFileItemReader.setLineMapper(lineMapper());
System.out.println("inside file reader 1 !!!!!");
return flatFileItemReader;
}
@Bean
public LineMapper<User> lineMapper() {
DefaultLineMapper<User> defaultLineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setDelimiter(",");
lineTokenizer.setStrict(false);
lineTokenizer.setNames(new String[]{"id", "name", "dept", "salary"});
BeanWrapperFieldSetMapper<User> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(User.class);
defaultLineMapper.setLineTokenizer(lineTokenizer);
defaultLineMapper.setFieldSetMapper(fieldSetMapper);
return defaultLineMapper;
}
@Bean
public Step step1() throws Exception{ // Step 1 - Read CSV and Write to DB
return stepBuilderFactory.get("step1")
.<User,User>chunk(100)
.reader(itemReader())
.processor(processor1)
.writer(writer1)
.build();
}
@Bean
public Job job() throws Exception{
return this.jobBuilderFactory.get("BATCH JOB")
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
DBWriter class:
@Component
public class DBWriter implements ItemWriter<User> {
@Autowired
private UserRepository userRepository;
@Override
public void write(List<? extends User> users) throws Exception {
System.out.println("Inside DB Writer");
System.out.println("Data Saved for Users: " + users);
userRepository.save(users);
}
}
Processor class:
@Component
public class Processor implements ItemProcessor<User, User> {
private static final Map<String, String> DEPT_NAMES =
new HashMap<>();
public Processor() {
DEPT_NAMES.put("001", "Technology");
DEPT_NAMES.put("002", "Operations");
DEPT_NAMES.put("003", "Accounts");
}
@Override
public User process(User user) throws Exception {
String deptCode = user.getDept();
String dept = DEPT_NAMES.get(deptCode);
user.setDept(dept);
user.setTime(new Date());
System.out.println(String.format("Converted from [%s] to [%s]", deptCode, dept));
return user;
}
}
Controller Class:
@RestController
@RequestMapping("/load")
public class LoadController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@GetMapping("/users")
public BatchStatus load() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
Map<String, JobParameter> maps = new HashMap<>();
maps.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters parameters = new JobParameters(maps);
JobExecution jobExecution = jobLauncher.run(job, parameters);
System.out.println("JobExecution: " + jobExecution.getStatus());
System.out.println("Batch is Running...");
while (jobExecution.isRunning()) {
System.out.println("...");
}
return jobExecution.getStatus();
}
}
答案1
得分: 3
spring.batch.job.enabled=false
属性用于阻止在应用程序启动时运行作业。
创建阅读器的方法仍将在配置时间调用,因此您会看到打印语句。但这并不意味着阅读器是在运行的作业内部调用的。
英文:
The spring.batch.job.enabled=false
property is used to prevent running jobs at application startup.
The method that creates the reader will be still be called at configuration time, so it's normal that you see the print statement. But that does not mean the reader was called inside a running job.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论