2017-03-16 114 views
1

我正在處理簡單的Spring批處理作業。當我啓動它的工作在無限循環中運行。它不停止。根據我的調度程序時間,它應該在每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; 
    } 
} 

回答

2

直到您的ItemReader返回null,一個步驟將一直運行。在你的情況下,你的ItemReader永遠不會(它總是會返回一個新的Person),所以它永遠不會結束。

+0

是的,它是那麼簡單。 :) – KSHiTiJ

0

添加標誌以防止讀者在無限循環中運行。

讀者:

public class PersonReader implements ItemReader<Person> { 
    private boolean batchJobState = false; 

    @Override 
    public Person read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { 
     if(!batchJobState){ 
      Person person = new Person(); 
      System.out.println("In PersonReader"); 
      batchJobState=true; 
      return person; 
     } 
     return null; 
    } 
}