如何将@Configuration和@EnableScheduling与Spring Batch一起使用

时间:2015-12-01 21:17:35

标签: java spring spring-boot spring-batch spring-scheduled

因为"只有void {return}方法可以使用@Scheduled"进行注释,当我使用@Bean配置而不是xml时,如何使用Spring Batch和Spring Scheduler Task组态?您可以在下面找到我的完整配置文件。当我从main()触发但仅一次触发时,它运行得很好。我想添加@Scheduled(fixedrate=9999)以便以特定频率唤起同样的工作。据我所知,为了做到这一点,我应该在step1方法中添加@Scheduled,但我不能从void返回不同。

@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguration {
       private static final Logger log = LoggerFactory
                     .getLogger(BatchConfiguration.class);

       @Bean
       @StepScope
       public FlatFileItemReader<Person> reader() {
              log.info(new Date().toString());
              FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
              reader.setResource(new ClassPathResource("test_person_json.js"));
              reader.setLineMapper(new DefaultLineMapper<Person>() {
                     {
                           setLineTokenizer(new DelimitedLineTokenizer() {
                                  {
                                         setNames(new String[] {"firstName", "lastName" });
                                  }
                           });
                           setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
                                  {
                                         setTargetType(Person.class);
                                  }
                           });
                     }
              });
              return reader;
       }

       @Bean
       public ItemProcessor<Person, Person> processor() {
              return new PersonItemProcessor();
       }

       @Bean
       public ItemWriter<Person> writer(DataSource dataSource) {
              JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
              writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
              writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
              writer.setDataSource(dataSource);
              return writer;
       }

       @Bean
       public Job importUserJob(JobBuilderFactory jobs, Step s1,
                     JobExecutionListener listener) {
              return jobs.get("importUserJob").incrementer(new RunIdIncrementer())
                           .listener(listener).flow(s1).end().build();
       }

       @Bean
       public Step step1(StepBuilderFactory stepBuilderFactory,
                     ItemReader<Person> reader, ItemWriter<Person> writer,
                     ItemProcessor<Person, Person> processor) {
              return stepBuilderFactory.get("step1").<Person, Person> chunk(10)
.reader(reader).processor(processor).writer(writer).build();
       }

       @Bean
       public JdbcTemplate jdbcTemplate(DataSource dataSource) {
              return new JdbcTemplate(dataSource);
       }
}


//Question updated on Dec 3th 2015 with first suggestion
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class PersonScheduler {
    private Job myImportJob;
    private JobLauncher jobLauncher;

    @Autowired
    public PersonScheduler(JobLauncher jobLauncher, @Qualifier("myImportJob") Job myImportJob){
        this.myImportJob = myImportJob;
        this.jobLauncher = jobLauncher;
   }

   @Scheduled(fixedRate=9999)
   public void runJob{
       jobLauncher.run(myImportJob, new JobParameters());
   }
}

1 个答案:

答案 0 :(得分:4)

只需创建单独的组件,您可以在其中自动装配作业并安排它:

@Component
public class MyScheduler{
    private Job myImportJob;
    private JobLauncher jobLauncher;

    @Autowired
    public MyScheduler(JobLauncher jobLauncher, @Qualifier("myImportJob") Job myImportJob){
        this.myImportJob = myImoportJob; 
        this.jobLauncher = jobLauncher;
   }

   @Scheduled(fixedRate=9999)
   public void runJob(){
       jobLauncher.run(myImportJob, new JobParameters());
   }
}

对第三条评论的反应:

创建步骤时只需使用.allowStartIfComplete(true)