0

我配置了一个从表中读取的作业,传递一些字符串,然后使用 @service 将数据存储到 Redis 实例中。

如您所见,作业已完成:

INFO  SimpleJobLauncher - Job: [FlowJob: [name=populateCacheWithCalendarData]] completed with the following

参数:[{time=2016-06-22T08:46:09.001}] 和以下状态:[COMPLETED]

然后它像循环一样一次又一次地启动:

INFO ScheduledTasks - 运行计划任务 [populateCacheWithCalendarData] 25438:INFO SimpleJobLauncher - 作业:[FlowJob:[name=populateCacheWithCalendarData]] 使用以下参数启动:[{time=2016-06-22T08:46:10}]

我有一个这样配置的计划任务:

@Slf4j
@Component
public class ScheduledTasks {

   @Autowired
   JobLauncher jobLauncher;

   @Autowired
   JobRegistry jobRegistry;

   // scheduled every 14 hours
   @Scheduled(cron = "* * */1 * * *")
   public void doPopulateCacheWithCalendarDataJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ populateCacheWithCalendarData ]");
      Job calendarJob = jobRegistry.getJob("populateCacheWithCalendarData");
      JobParametersBuilder paramsBuilder = new JobParametersBuilder().addString("time", LocalDateTime.now().toString());

      // then
      jobLauncher.run(calendarJob, paramsBuilder.toJobParameters());
   }
}

作业配置:

@Configuration
@EnableBatchProcessing
public class CalendarBatchConfiguration extends AbstractBatchConfiguration {

   @Bean
   public Job populateCacheWithCalendarData() {

      return jobBuilderFactory.get("populateCacheWithCalendarData")
              .incrementer(new RunIdIncrementer())
              .flow(calendarStep1())
              .end()
              .build();
   }


   /**
    * Look up hotel tickers and creates a csv file with them.
    *
    * @return ariStep1
    */
   @Bean
   public Step calendarStep1() {

      return stepBuilderFactory.get("calendarStep1")
              .<Hotel, String>chunk(100)
              .reader(calendarReader())
              .processor(calendarProcessor())
              .writer(calendarWriter2())
              .build();
   }

@Bean
   public JdbcCursorItemReader<Hotel> calendarReader() {

      JdbcCursorItemReader<Hotel> reader = new JdbcCursorItemReader<>();
      reader.setSql("SELECT identificador, es_demo FROM instanciasaplicaciones WHERE es_demo = 0 AND version = 6");
      reader.setDataSource(this.dataSource);
      reader.setRowMapper((resultSet, i) -> new Hotel(resultSet.getString("identificador"), resultSet.getString("es_demo")));


      return reader;
   }


   @Bean
   public HotelItemProcessor calendarProcessor() {

      return new HotelItemProcessor();
   }

@Bean
   public CalendarItemWriter calendarWriter2() {

      return new CalendarItemWriter();
   }
}

处理器和写入器:

@Slf4j
public class CalendarItemProcessor implements ItemProcessor<String, String> {

   @Override
   public String process(String item) throws Exception {

      log.info("Processing calendar hotel Ticker [" + item + "]");

      return item;
   }
}

@Slf4j
public class CalendarItemWriter implements ItemWriter<String> {

   @Autowired
   private CalendarService calendarService;


   @Override
   public void write(List<? extends String> hotelTickers) throws Exception {

      log.info("Creating calendar entry in Cache for items... ", hotelTickers.toString());

      hotelTickers.forEach(this::createOrUpdateCache);
   }


   /**
    * Use service to store calendar values into the cache.
    *
    * @param hotelTicker hotelTicker
    */
   private void createOrUpdateCache(String hotelTicker) {
      // store calendar ari values
      calendarService.createOrUpdateCalendarByHotelTicker(hotelTicker);
   }
}

以防万一的主要应用程序:

/**
 * Main entry point for the Application.
 */
@EnableScheduling
@EnableTransactionManagement
@SpringBootApplication
public class Application {

   public static void main(String[] args) {

      SpringApplication.run(Application.class, args);
   }
}

我不知道它为什么会这样做,因为过了很长时间它就会停止。

提前致谢

4

1 回答 1

0

问题是 * 使调度程序每秒、每分钟启动作业。我将其替换为:

 @Scheduled(cron = "0 0 */14 * * *")

我希望这可能对其他人有所帮助

于 2016-09-13T06:33:28.743 回答