英文:
Springbatch - Modifying my application so I can pass JobParameters to my Setup
问题
我目前有一个类似于以下的 Spring Batch 应用程序:
@SpringBootApplication
@EnableBatchProcessing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我有一个存储库配置文件,在本地使用 h2 数据库来维护作业存储库、创建作业启动器和其他一些内容。对于本帖子而言,重要的部分是:
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
最后,我在另一个配置文件中有一个作业 bean:
@Bean
public Job databaseCursorJob(@Qualifier("databaseCursorStep") Step exampleJobStep,
JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("databaseCursorJob")
.incrementer(new RunIdIncrementer())
.flow(exampleJobStep)
.end()
.build();
}
我需要能够传递作业参数,例如:
JobParameters jobParameters = new JobParametersBuilder()
.addDate("date", new Date())
.toJobParameters();
主要是为了确保每个作业实例的唯一性。然而,我的问题是,我无法弄清楚如何调整我的应用程序以将这些参数传递给作业。我通常在作业启动器的 run 方法中看到这些参数传递,例如:
jobLauncher.run(job, new JobParameters());
但实际上我从未调用过这个方法,我猜可能是因为我使用了 Spring Boot 应用程序,这似乎是通过应用程序上的 @EnableBatchProcessing
注解在幕后自动完成的。
因此,我需要重新配置我的应用程序,以更改作业启动方式,以便能够传递作业参数。然而,我一直未能成功找出解决方法。如果有任何想法,我将不胜感激。谢谢。
英文:
I currently have springbatch application like this:
@SpringBootApplication
@EnableBatchProcessing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I have a repository config file that uses an h2 db locally for maintaining the job repository, creating the joblauncher, and other things. The salient part for this post is:
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
Finally, I have job bean in another config file:
@Bean
public Job databaseCursorJob(@Qualifier("databaseCursorStep") Step exampleJobStep,
JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("databaseCursorJob")
.incrementer(new RunIdIncrementer())
.flow(exampleJobStep)
.end()
.build();
}
I need to be able to pass jobparameters like:
JobParameters jobParameters = new JobParametersBuilder()
.addDate("date", new Date())
.toJobParameters();
, mostly just to ensure uniqueness for each job instance. However, my problem is that I cannot figure out how to tweak my application to pass these to the job. I typically see these passed in run method of job launcher like:
jobLauncher.run(job, new JobParameters());
, but I never actually ever call that method, as I guess because I use the springboot application, it is done sort under the covers presumably via the @EnableBatchProcessing annotation on the app.
So, I need to reconfigure my application to change how the job is launched to be able to pass the jobparameters. However I have been unsuccessful in figuring that out. I would be greatful for any ideas. Thank you.
答案1
得分: 0
好的,以下是您要翻译的内容:
我有一个解决方案。这是我的好朋友Hassine先生指出给我的,他似乎很擅长找出我自己无法找到的springbatch解决方案!
根据Hassine先生的链接:https://github.com/spring-guides/gs-batch-processing/issues/15
我拿起了我的原始应用:
@SpringBootApplication
@EnableBatchProcessing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
并替换为:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
Job job = applicationContext.getBean(Job.class);
JobParameters jobParameters = new JobParametersBuilder()
.addDate("date", new Date())
.toJobParameters();
jobLauncher.run(job, jobParameters);
}
}
此外,我还不得不在我的application.properties中添加:
spring.batch.job.enabled=false
这会强制springboot绕过自动的jobLauncher.run(job, jobParameters),而是寻找我在上述代码中的主类中提供的任何替代启动方式。这样,我可以直接添加jobParameter,为每个作业实例添加一个时间戳,以确保每个作业实例都能得到唯一的标识,这(希望)将防止spring尝试重新启动带有旧作业实例标识符的孤立作业实例。
英文:
ok I have a solution. This was pointed out to me by my good friend Mr Hassine who seems to have a knack for uncovering solutions for springbatch that I could not uncover myself!
Following Mr Hsssine's link: https://github.com/spring-guides/gs-batch-processing/issues/15
I took my original application:
@SpringBootApplication
@EnableBatchProcessing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
, and substituted:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
Job job = applicationContext.getBean(Job.class);
JobParameters jobParameters = new JobParametersBuilder()
.addDate("date", new Date())
.toJobParameters();
jobLauncher.run(job, jobParameters);
}
}
Additionally, I had to add:
spring.batch.job.enabled=false
to my application.properties, which forces springboot to bypass the auto-magic jobLauncher.run(job, jobParameters), and instead seeks out any alternative launching as I provided in my main class in the above code. This way, I could directly add the jobParameter which adds a timestamp to ensure each job instance is uniquely identified which (hopefully) will prevent vain attempts on the part of spring to try re-starting orphaned job instances with old job instance identifiers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论