我已经创建了Spring Batch(RESTReader、自定义处理器和自定义ItemWriter),我计划了它。Spring批工作良好。当监听器在每个计划的间隔内完成打印作业时,计划似乎可以工作,但似乎不读或写。
我的Sprint启动应用程序
@EnableScheduling
@SpringBootApplication
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class,args);
}
}
@Component
public class BatchScheduler {
Logger log = Logger.getLogger(BatchScheduler.class);
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobCompletionNotificationListener listener;
@Autowired
private Step step1;
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
private CreateRegistrationsBatchConfiguration job;
@Scheduled(fixedRate = 85000)
public void runJob() {
try{
JobExecution execution = jobLauncher.run(
job.importRegistrationJob(jobBuilderFactory, listener,
step1),
new JobParametersBuilder().addLong("uniqueness",
System.nanoTime()).toJobParameters()
);
log.info("Job finished with status :" + execution.getStatus());
}catch(Exception exception) {
log.error(exception.getMessage());
}
}
}
@Configuration
@EnableBatchProcessing
public class CreateRegistrationsBatchConfiguration {
Logger log =
Logger.getLogger(CreateRegistrationsBatchConfiguration.class);
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private Environment environment;
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
ItemReader<EmployeeEmploymentDTO> restEmployeeReader(Environment
environment,
RestTemplate restTemplate) {
return new RESTEmployeeReader(
environment.getRequiredProperty("rest.api.url"),
restTemplate
);
}
@Bean
public RegistrationItemProcessor processor() {
return new RegistrationItemProcessor();
}
@Bean
public ItemWriter<List<Registration>> writer() {
return new MultiOutputItemWriter();
}
@Bean
public Job importRegistrationJob(JobBuilderFactory jobs, JobCompletionNotificationListener listener, Step step1) {
return jobBuilderFactory.get("importRegistrationJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1)
.end()
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory,
ItemReader<EmployeeEmploymentDTO> reader,
ItemWriter<List<Registration>> writer,
ItemProcessor<EmployeeEmploymentDTO, List<Registration>>
processor) {
return stepBuilderFactory.get("step1").allowStartIfComplete(true)
.<EmployeeEmploymentDTO, List<Registration>> chunk(10)
.reader(restEmployeeReader(environment,restTemplate()))
.processor(processor())
.writer(writer)
.build();
}
}
+==+=====+===+=====+=====================+=====================+=====================+===========+===========+==+=====================+==+
| | 338 | 2 | 337 | 2018-05-18 14:36:36 | 2018-05-18 14:36:36 | 2018-05-18 14:37:47 | COMPLETED | COMPLETED | | 2018-05-18 14:37:47 | |
+==+=====+===+=====+=====================+=====================+=====================+===========+===========+==+=====================+==+
| | 339 | 2 | 338 | 2018-05-18 14:38:01 | 2018-05-18 14:38:01 | 2018-05-18 14:38:01 | COMPLETED | COMPLETED | | 2018-05-18 14:38:01 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 340 | 2 | 339 | 2018-05-18 14:39:26 | 2018-05-18 14:39:26 | 2018-05-18 14:39:26 | COMPLETED | COMPLETED | | 2018-05-18 14:39:26 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 341 | 2 | 340 | 2018-05-18 14:40:51 | 2018-05-18 14:40:51 | 2018-05-18 14:40:51 | COMPLETED | COMPLETED | | 2018-05-18 14:40:51 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 342 | 2 | 341 | 2018-05-18 14:42:16 | 2018-05-18 14:42:16 | 2018-05-18 14:42:16 | COMPLETED | COMPLETED | | 2018-05-18 14:42:16 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 343 | 2 | 342 | 2018-05-18 14:43:41 | 2018-05-18 14:43:41 | 2018-05-18 14:43:41 | COMPLETED | COMPLETED | | 2018-05-18 14:43:41 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 344 | 2 | 343 | 2018-05-18 14:45:06 | 2018-05-18 14:45:06 | 2018-05-18 14:45:06 | COMPLETED | COMPLETED | | 2018-05-18 14:45:06 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 345 | 2 | 344 | 2018-05-18 14:46:31 | 2018-05-18 14:46:31 | 2018-05-18 14:46:31 | COMPLETED | COMPLETED | | 2018-05-18 14:46:31 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 346 | 2 | 345 | 2018-05-18 14:47:56 | 2018-05-18 14:47:56 | 2018-05-18 14:47:56 | COMPLETED | COMPLETED | | 2018-05-18 14:47:56 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 347 | 2 | 346 | 2018-05-18 14:49:21 | 2018-05-18 14:49:21 | 2018-05-18 14:49:21 | COMPLETED | COMPLETED | | 2018-05-18 14:49:21 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 348 | 2 | 347 | 2018-05-18 14:50:46 | 2018-05-18 14:50:46 | 2018-05-18 14:50:46 | COMPLETED | COMPLETED | | 2018-05-18 14:50:46 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
BATCH_STEP_EXECUTION
+==========================================================================================================================================================================================================================================================+
| STEP_EXECUTION_ID, VERSION, STEP_NAME, JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, COMMIT_COUNT, READ_COUNT, FILTER_COUNT, WRITE_COUNT, READ_SKIP_COUNT, WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, ROLLBACK_COUNT, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED |
+==========================================================================================================================================================================================================================================================+
| '338', '16', 'step1', '338', '2018-05-18 14:36:36', '2018-05-18 14:37:47', 'COMPLETED', '14', '132', '33', '99', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:37:47' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '339', '3', 'step1', '339', '2018-05-18 14:38:01', '2018-05-18 14:38:01', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:38:01' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '340', '3', 'step1', '340', '2018-05-18 14:39:26', '2018-05-18 14:39:26', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:39:26' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '341', '3', 'step1', '341', '2018-05-18 14:40:51', '2018-05-18 14:40:51', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:40:51' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '342', '3', 'step1', '342', '2018-05-18 14:42:16', '2018-05-18 14:42:16', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:42:16' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '343', '3', 'step1', '343', '2018-05-18 14:43:41', '2018-05-18 14:43:41', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:43:41' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '344', '3', 'step1', '344', '2018-05-18 14:45:06', '2018-05-18 14:45:06', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:45:06' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '345', '3', 'step1', '345', '2018-05-18 14:46:31', '2018-05-18 14:46:31', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:46:31' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '346', '3', 'step1', '346', '2018-05-18 14:47:56', '2018-05-18 14:47:56', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:47:56' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '347', '3', 'step1', '347', '2018-05-18 14:49:21', '2018-05-18 14:49:21', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:49:21' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '348', '3', 'step1', '348', '2018-05-18 14:50:46', '2018-05-18 14:50:46', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:50:46' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '349', '3', 'step1', '349', '2018-05-18 14:52:11', '2018-05-18 14:52:11', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:52:11' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
控制台日志的一部分
BatchApplication-在27.638秒内启动BatchApplication(运行于49.853的JVM)13:30:04.905[pool-11-thread-1]信息org.springframework.batch.core.launch.support.simpleJobLauncher-job:[flowjob:[name=importRegistrationjob]]用以下参数启动:[{uniqueness=104422824800127}]13:30:04.946
[pool-11-thread-1]信息信息
JobCompletionNotificationListener-!!!工作完成了!验证结果时间13:31:14.015
[pool-11-thread-1]信息org.springframework.batch.core.launch.support.simpleJobLauncher-作业:[flowjob:[name=importregistrationjob]]已完成,参数如下:[{uniqueness=104507836733954}],状态如下:[completed]13:31:29.766[pool-11-thread-1]信息
-作业已完成,状态:已完成13:32:54.713
[pool-11-thread-1]信息org.springframework.batch.core.launch.support.simplejoblauncher-job:[flowjob:[name=importregistrationjob]]使用以下参数启动:[{uniqueness=104592836710544}]13:32:54.721
[pool-11-thread-1]信息org.springframework.batch.core.job.simplejoblauncher-执行步骤:[step1]13:32:54.750[pool-11-thread-1]信息工作完成了!验证结果时间13:32:54.750
[pool-11-thread-1]信息org.springframework.batch.core.launch.support.simplejoblauncher-job:[flowjob:[name=importregistrationjob]]用以下参数启动:[{uniqueness=104677826302144}]13:34:19.722
[pool-11-thread-1]信息org.springframework.batch.core.job.simplestephandler-执行步骤:[step1]
在我为阅读器、处理器和编写器添加@stepscope后,它起作用了
@Bean
@StepScope
ItemReader<EmployeeEmploymentDTO> restEmployeeReader(Environment environment,
RestTemplate restTemplate) {
return new RESTEmployeeReader(
environment.getRequiredProperty("rest.api.url"),
restTemplate
);
}
@Bean
@StepScope
public RegistrationItemProcessor processor() {
return new RegistrationItemProcessor();
}
@Bean
@StepScope
public ItemWriter<List<Registration>> writer() {
return new MultiOutputItemWriter();
}
我当时正在开发一个Spring批处理应用程序,使用java配置执行两个批处理作业。最近,我添加了一个Spring调度程序来调度我编写的一个作业。侦听器在作业第一次完成时被调用,但在下一次执行后不会被调用。以下是我的作业配置代码: 下面是我的调度程序的代码: 我的听众如下: 以下是控制台输出: 请告诉我我做错了什么,为什么听者没有被执行后续尝试。
在一个项目中,我们必须运行一个定期开始的作业(现在QA env上每5分钟开始一次),该作业处理40K用户的一些任务。我们决定使用Spring Batch,因为它非常适合,并且几乎用默认配置实现了它(例如,它使用)。好的,有一个工作由一个步骤组成: 开箱即用 在内存中执行轻量级计算的自定义 自定义,它通过多个JPQL和本机查询将数据保存到同一个PostgreSQL db。 作业本身是用调度的,并且每
我有一个,每个月底运行一次。运行后,它会将一些数据保存到数据库中。 当我扩展应用程序时(例如有2个实例),两个实例都运行计划作业,并且都保存数据,在一天结束时,我的数据库有相同的数据。 所以我希望计划作业只运行一次,而不管云上的实例数量如何。
Spring批处理作业与flatfileitemreader(从csv读取)、processor(更新adwords api提要详细信息,对于csv文件中的每个记录(大约有40条记录),这一步大约需要40秒)和正在更新DB中记录的定制writer一起使用。 web.xml
我在版本2.1.4中开始学习Spring Boot Batch 这是我的配置 最后一节课是我的排定程序:
是否可以只在指定的时间安排一次Spring服务方法?例如,当前时间是下午2点,但当我点击action按钮时,我希望我的服务方法在晚上8点开始。我很熟悉@Schedured注释,我不确定如何编写不定期运行的cron表达式。这个每天晚上8点触发。 有什么建议吗?