我可以更改Spring Batch配置以静态运行吗?

huangapple go评论90阅读模式
英文:

Can I change Spring Batch configuration to run statically?

问题

当使用 @Scheduled 注解时,它会动态执行。我可以将其更改为静态运行吗?

英文:

[Spring Batch] When @Scheduled annotation is used, it is executed dynamically. Can I change it to run statically?

答案1

得分: 0

因为你提到了@Scheduled,我想象你的批处理是从一个Web应用程序中执行的。如果你想要立即运行它,可以:

  • 使用Spring Boot启动你的批处理,通过启动一个Spring Boot应用程序:我建议你按照这个教程操作,并将示例批处理替换为你自己的:https://spring.io/guides/gs/batch-processing/
  • 从经典的Java应用程序中手动启动你的批处理,在启动时创建一个Spring上下文(这样Spring Boot可以更好地完成工作)
  • 将你的批处理作为单元测试启动(用于集成目的):你可以按照这个教程操作(它也使用了Spring Boot):https://www.baeldung.com/spring-batch-testing-job

祝你好运。

英文:

Because you mention @Scheduled, I imagine your batch is executed from a web application. If you want to run it out of the box, you can :

  • use Spring boot to start your batch by launching a spring boot application : I advise you to follow this tuto and replace the example batch by your own : https://spring.io/guides/gs/batch-processing/
  • start your batch manually from a classic java application which create a spring context at start (so spring boot does the job really better)
  • start your batch as a unit test (for integration purpose) : you can folow this tuto (which also use spring boot) :https://www.baeldung.com/spring-batch-testing-job

Good luck

答案2

得分: -1

我想我误解了你的问题。
如果你想要从静态方法运行,有一种方法可以实现。你可以创建一个类似以下方式的StaticJobInitializer组件:

  1. @Component
  2. public class StaticJobInitializer {
  3. private JobRegistry jobRegistry;
  4. private JobLauncher jobLauncher;
  5. public StaticJobInitializer(JobRegistry jobRegistry, JobLauncher jobLauncher) {
  6. this.jobRegistry = jobRegistry;
  7. this.jobLauncher = jobLauncher;
  8. }
  9. @PostConstruct
  10. public void init() {
  11. StaticJobRun.setJobRegistry(jobRegistry);
  12. StaticJobRun.setJobLauncher(jobLauncher);
  13. }
  14. }
  1. public final class StaticJobRun {
  2. private static JobRegistry jobRegistry;
  3. private static JobLauncher jobLauncher;
  4. public static JobRegistry getJobRegistry() {
  5. return jobRegistry;
  6. }
  7. public static JobLauncher getJobLauncher() {
  8. return jobLauncher;
  9. }
  10. public static void setJobLauncher(JobLauncher jobLauncher) {
  11. StaticJobRun.jobLauncher = jobLauncher;
  12. }
  13. public static void setJobRegistry(JobRegistry jobRegistry) {
  14. StaticJobRun.jobRegistry = jobRegistry;
  15. }
  16. }

然后,你的启动作业静态方法应该如下所示:

  1. public static void startJob() {
  2. Job job = StaticJobRun.getJobRegistry().getJob("job_name");
  3. JobParameters jobParameters = new JobParametersBuilder()
  4. .toJobParameters();
  5. StaticJobRun.getJobLauncher().run(job, jobParameters);
  6. }

请注意,我已经将HTML编码(")替换为正常的引号,以确保代码的正确性。

英文:

I think I misunderstood your question.
If you want to run from static method there is one way to do this. You can make StaticJobInitializer component like this

  1. @Component
  2. public class StaticJobInitializer {
  3. private JobRegistry jobRegistry;
  4. private JobLauncher jobLauncher;
  5. public StaticJobInitializer(JobRegistry jobRegistry, JobLauncher jobLauncher) {
  6. this.jobRegistry = jobRegistry;
  7. this.jobLauncher = jobLauncher;
  8. }
  9. @PostConstruct
  10. public void init() {
  11. StaticJobRun.setJobRegistry(jobRegistry);
  12. StaticJobRun.setJobLauncher(jobLauncher);
  13. }
  14. }
  1. public final class StaticJobRun {
  2. private static JobRegistry jobRegistry;
  3. private static JobLauncher jobLauncher;
  4. public static JobRegistry getJobRegistry() {
  5. return jobRegistry;
  6. }
  7. public static JobLauncher getJobLauncher() {
  8. return jobLauncher;
  9. }
  10. public static void setJobLauncher(JobLauncher jobLauncher) {
  11. StaticJobRun.jobLauncher = jobLauncher;
  12. }
  13. public static void setJobRegistry(JobRegistry jobRegistry) {
  14. StaticJobRun.jobRegistry = jobRegistry;
  15. }
  16. }

and than your start job static method should be like this:

  1. public static void startJob(){
  2. Job job = StaticJobRun.getJobRegistry().getJob("job_name");
  3. JobParameters jobParameters = new JobParametersBuilder()
  4. .toJobParameters();
  5. StaticJobRun.getJobLauncher().run(job, new JobParameters());
  6. }

huangapple
  • 本文由 发表于 2020年8月5日 16:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定