batch spring 重复执行_Spring Batch Job在无限循环中运行

文寒
2023-12-01

我在做简单的春季批处理工作。当我启动作业时,它在无限循环中运行。它不会停止。根据我的计划时间,它应该每10秒运行一次。但当工作开始时,它并没有停止。它只是从读写器、处理器和写入器分别打印系统输出。我正在与读者,处理器和作家创造工作。我是通过注释来完成所有配置的。不是XML。

这里是批量配置@Configuration

@EnableBatchProcessing

@EnableScheduling

public class BatchJobConfig {

@Autowired

private JobBuilderFactory jobBuilderFactory;

@Autowired

private StepBuilderFactory stepBuilderFactory;

@Autowired

private SimpleJobLauncher jobLauncher;

@Scheduled(cron="*/10 * * * * *")

public void perform() throws Exception

{

Job job = job(step1());

JobParameters jobParameters = new JobParameters();

jobLauncher.run(job, jobParameters);

}

@Bean

public Step step1()

{

return stepBuilderFactory.get("step1"). chunk(1)

.reader(reader()).processor(processor()).writer(writer())

.build();

}

@Bean

public Job job(Step step1) throws Exception

{

return jobBuilderFactory.get("job")

.incrementer(new RunIdIncrementer()).flow(step1())

.end().build();

}

@Bean

public DataSource dataSource()

{

EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();

return embeddedDatabaseBuilder.addScript("classpath:org/springframework/batch/core/schema-drop-hsqldb.sql")

.addScript("classpath:org/springframework/batch/core/schema-hsqldb.sql")

.setType(EmbeddedDatabaseType.HSQL)

.build();

}

@Bean

public PersonReader reader() {

return new PersonReader();

}

@Bean

public PersonWriter writer() {

return new PersonWriter();

}

@Bean

public PersonProcessor processor() {

return new PersonProcessor();

}

@Bean

public MapJobRepositoryFactoryBean mapJobRepositoryFactory(ResourcelessTransactionManager txManager) throws Exception {

MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(txManager);

factory.afterPropertiesSet();

return factory;

}

@Bean

public JobRepository jobRepository(MapJobRepositoryFactoryBean factory) throws Exception {

return factory.getObject();

}

@Bean

public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {

SimpleJobLauncher launcher = new SimpleJobLauncher();

launcher.setJobRepository(jobRepository);

return launcher;

}

@Bean

public ResourcelessTransactionManager transactionManager() {

return new ResourcelessTransactionManager();

}

}

personreader.java个人阅读器

public class PersonReader implements ItemReader {

@Override

public Person read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

Person person = new Person();

System.out.println("In PersonReader");

return person;

}

}

personwriter.java个人作家

public class PersonWriter implements ItemWriter {

@Override

public void write(List extends Person> arg0) throws Exception {

System.out.println("IN PersonWriter");

}

}

个人处理器.java

public class PersonProcessor implements ItemProcessor {

@Override

public Person process(Person arg0) throws Exception {

System.out.println("In PersonProcessor");

return arg0;

}

}

最佳答案:

将运行一个步骤,直到ItemReader返回空值。在您的情况下,您的ItemReader永远不会结束(它总是返回一个新的Person),因此它永远不会结束。

 类似资料: