【发布时间】:2019-10-24 17:50:29
【问题描述】:
我正在使用 Spring Cloud 数据流并创建了一个包含作业的 Spring Cloud 任务。该作业有一个名为 last_modified_date 的参数,它是可选的。在代码中,我已经指定了在 last_modified_date 为空的情况下取哪个日期,也就是说,它没有作为参数传递。问题是,如果对于作业的一个实例我传递了 last_modified_date 但对于下一个我没有传递,它会在最后一次执行中选择一个,而不是将它作为 null 传递并从代码中获取它。
@Component
@StepScope
public class SalesforceAdvertiserLoadTasklet implements Tasklet {
@Value("#{jobParameters['last_modified_date']}")
protected Date lastModifiedDate;
private static final Logger logger =
LoggerFactory.getLogger(SalesforceAdvertiserLoadTasklet.class);
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
if(lastModifiedDate == null) {
lastModifiedDate =
Date.from(LocalDate.now().minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
}
logger.info("In Method: runSalesforceAdvertiserLoadJob launch started on last_modified_date {}",
lastModifiedDate);
logger.info("Getting advertisers from SalesForce");
try {
getAdvertisersFromSalesforceAndAddtoDb();
} catch (JsonSyntaxException | IOException | ParseException e) {
logger.error("ERROR--> {}", e.getMessage());
}
return RepeatStatus.FINISHED;
}
@Bean
public JobParametersIncrementer runIdIncrementor() {
return new RunIdIncrementer();
}
@Bean
public Job salesforceAdvertiserLoadJob() {
return jobBuilderFactory.get(SalesforceJobName.salesforceAdvertiserLoadJob.name())
.incrementer(runIdIncrementor())
.listener(batchJobsExecutionListener)
.start(stepsConfiguration.salesforceAdvertiserLoadStep()).build();
}
有什么方法可以阻止新作业实例从以前的作业实例中获取参数?
【问题讨论】:
-
你能提供你是如何启动这项工作的以及工作定义本身吗?
-
@MichaelMinella 我只是在创建工作的 bean。在运行 jar 时,作业开始。
-
last_modified_date最初来自哪里? -
@PhilipWrage last_modified_date 可能会在有人想要在不同日期运行作业时出现。所以也许我们每个月都会自己给 last_modified_date 一次。
-
我的意思是编程的值是如何设置的?命令行参数?属性文件?休息控制器?等等。
标签: spring spring-boot spring-batch batch-processing spring-cloud