我正在从事简单的Spring Batch Job。当我启 Action 业时,它以无限循环运行。它并没有停止。根据我的调度程序时间,它应该每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").<Person, Person> 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<Person> {
    @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<Person> {
    @Override
    public void write(List<? extends Person> arg0) throws Exception {
        System.out.println("IN PersonWriter");
    }
}

PersonProcessor.java
public class PersonProcessor implements ItemProcessor<Person, Person> {
    @Override
    public Person process(Person arg0) throws Exception  {
        System.out.println("In PersonProcessor");
        return arg0;
    }
}

最佳答案

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

10-07 16:39
查看更多