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

因为“只有返回无效的方法可以与注释@Scheduled ”,我怎么可以使用Spring Batch和春季调度任务时,我使用@Bean配置,而不是xml配置? 您可以在下面找到我的完整配置文件。 当我从main()触发但是只触发一次时,它运行完美。 我想添加@Scheduled(fixedrate=9999)以便以特定频率唤起相同的工作。 据我所看到的,为了做到这一点,我预计新增@Scheduled各地第一步方法,但我不能,因为它返回从虚空不同。

@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());
   }
}

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

@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)

链接地址: http://www.djcxy.com/p/89083.html

上一篇: How to use @Configuration and @EnableScheduling together with Spring Batch

下一篇: patch Node.js package version