作业完成后的 spring 批次一次又一次地启动

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

如您所见,工作已完成:

INFO SimpleJobLauncher - 作业:[FlowJob:[name = populateCacheWithCalendarData]]完成以下内容
参数:[]及以下状态:[已完成]

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

INFO ScheduledTasks - 运行计划任务[populateCacheWithCalendarData] 25438:INFO SimpleJobLauncher - 作业:[FlowJob:[name = populateCacheWithCalendarData]]使用以下参数启动:[]

我有一个如下配置的计划任务:

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

我不知道为什么会这样做,因为经过一段时间后它停止了 .

提前致谢

回答(1)

2 years ago

问题在于*让调度员每隔一分钟,每分钟和儿子就开始工作 . 我替换它:

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

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