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

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

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组件:

@Component
public class StaticJobInitializer {

    private JobRegistry jobRegistry;
    private JobLauncher jobLauncher;

    public StaticJobInitializer(JobRegistry jobRegistry, JobLauncher jobLauncher) {
        this.jobRegistry = jobRegistry;
        this.jobLauncher = jobLauncher;
    }

    @PostConstruct
    public void init() {
        StaticJobRun.setJobRegistry(jobRegistry);
        StaticJobRun.setJobLauncher(jobLauncher);
    }
}
public final class StaticJobRun {
    private static JobRegistry jobRegistry;
    private static JobLauncher jobLauncher;

    public static JobRegistry getJobRegistry() {
        return jobRegistry;
    }

    public static JobLauncher getJobLauncher() {
        return jobLauncher;
    }

    public static void setJobLauncher(JobLauncher jobLauncher) {
        StaticJobRun.jobLauncher = jobLauncher;
    }

    public static void setJobRegistry(JobRegistry jobRegistry) {
        StaticJobRun.jobRegistry = jobRegistry;
    }
}

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

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

请注意,我已经将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

@Component
public class StaticJobInitializer {

    private JobRegistry jobRegistry;
    private JobLauncher jobLauncher;

    public StaticJobInitializer(JobRegistry jobRegistry, JobLauncher jobLauncher) {
        this.jobRegistry = jobRegistry;
        this.jobLauncher = jobLauncher;
    }

    @PostConstruct
    public void init() {
        StaticJobRun.setJobRegistry(jobRegistry);
        StaticJobRun.setJobLauncher(jobLauncher);
    }
}
public final class StaticJobRun {
    private static JobRegistry jobRegistry;
    private static JobLauncher jobLauncher;

    public static JobRegistry getJobRegistry() {
        return jobRegistry;
    }

    public static JobLauncher getJobLauncher() {
        return jobLauncher;
    }

    public static void setJobLauncher(JobLauncher jobLauncher) {
        StaticJobRun.jobLauncher = jobLauncher;
    }

    public static void setJobRegistry(JobRegistry jobRegistry) {
        StaticJobRun.jobRegistry = jobRegistry;
    }
}

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

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

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:

确定