how to select which spring batch job to run based on application argument - spring boot java config

amacoder

I have two independent spring batch jobs in the same project because I want to use the same infrastructure-related beans. Everything is configured in Java. I would like to know if there's a proper way to start the jobs independent based for example on the first java app argument in the main method for example. If I run SpringApplication.run only the second job gets executed by magic. The main method looks like:

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {                
        SpringApplication app = new SpringApplication(Application.class);
        app.setWebEnvironment(false);
        ApplicationContext ctx= app.run(args);              
    }

}

and the two jobs are configured as presented in the Spring Batch Getting Started tutorial on Spring.io. Here is the configuration file of the first job, the second being configured in the same way.

@Configuration
@EnableBatchProcessing
@Import({StandaloneInfrastructureConfiguration.class, ServicesConfiguration.class})
public class AddPodcastJobConfiguration {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    //reader, writer, processor...

}

To enable modularization I created an AppConfig class, where I define factories for the two jobs:

@Configuration
@EnableBatchProcessing(modular=true)
public class AppConfig {

    @Bean
    public ApplicationContextFactory addNewPodcastJobs(){
        return new GenericApplicationContextFactory(AddPodcastJobConfiguration.class);
    }

    @Bean
    public ApplicationContextFactory newEpisodesNotificationJobs(){
        return new GenericApplicationContextFactory(NotifySubscribersJobConfiguration.class);
    }    

}

P.S. I am new to Spring configuration in Java configuration Spring Boot and Spring Batch...

amacoder

To run the jobs you like from the main method you can load the the required job configuration bean and the JobLauncher from the application context and then run it:

@ComponentScan
@EnableAutoConfiguration
public class ApplicationWithJobLauncher {

    public static void main(String[] args) throws BeansException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException {

        Log log = LogFactory.getLog(ApplicationWithJobLauncher.class);

        SpringApplication app = new SpringApplication(ApplicationWithJobLauncher.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext ctx= app.run(args);
        JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
        JobParameters jobParameters = new JobParametersBuilder()
            .addDate("date", new Date())
            .toJobParameters();  

        if("1".equals(args[0])){
            //addNewPodcastJob
            Job addNewPodcastJob = ctx.getBean("addNewPodcastJob", Job.class);          
            JobExecution jobExecution = jobLauncher.run(addNewPodcastJob, jobParameters);                   
        } else {
            jobLauncher.run(ctx.getBean("newEpisodesNotificationJob",  Job.class), jobParameters);   

        } 

        System.exit(0);
    }
}

What was causing my lots of confusion was that the second job were executed, even though the first job seemed to be "picked up" by the runner... Well the problem was that in both job's configuration file I used standard method names writer(), reader(), processor() and step() and it used the ones from the second job that seemed to "overwrite" the ones from the first job without any warnings... I used though an application config class with @EnableBatchProcessing(modular=true), that I thought would be used magically by Spring Boot :

@Configuration
@EnableBatchProcessing(modular=true)
public class AppConfig {

    @Bean
    public ApplicationContextFactory addNewPodcastJobs(){
        return new GenericApplicationContextFactory(AddPodcastJobConfiguration.class);
    }

    @Bean
    public ApplicationContextFactory newEpisodesNotificationJobs(){
        return new GenericApplicationContextFactory(NotifySubscribersJobConfiguration.class);
    }    

}

I will write a blog post about it when it is ready, but until then the code is available at https://github.com/podcastpedia/podcastpedia-batch (work/learning in progress)..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select which job should run in Spring Batch + Spring Rest API

From Dev

How to run spring batch job multiple times with different job parameters?

From Dev

How Spring Boot run batch jobs

From Dev

Spring batch how to run a job in loop until it meets a condition?

From Dev

How to run spring-boot as a client application?

From Dev

How to run multiple controllers in a Spring Boot application?

From Dev

How to run spring-boot as a client application?

From Dev

How to run spring boot application without Eclipse?

From Dev

run spring batch job from the controller

From Dev

Best approach to run a Spring Batch job

From Dev

Restarting a spring batch job that is run by multiple threads

From Dev

Spring batch job application hangs with step partition

From Dev

Spring batch job application hangs with step partition

From Dev

Is there a difference between Run As: Spring Boot App and Run As: Java Application?

From Dev

How to run integration test of a spring-boot based application through maven-failsafe-plugin?

From Dev

tests for spring boot application are not run

From Dev

How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

From Dev

RabbitTemplate not ending the spring boot + spring batch job app

From Dev

Continuously running a Spring Batch job using Spring boot

From Dev

How to load Java based SecurityConfig in XML based Spring config file?

From Dev

Run Flyway Java-based callbacks with Spring Boot

From Dev

Spring-boot application can only be launched with spring-boot:run when forking - java -jar fails

From Dev

Spring-boot application can only be launched with spring-boot:run when forking - java -jar fails

From Dev

how to run/turn off selective tests based on profiles in spring boot

From Dev

How to run a standalone/interactive Spring Boot CRaSH Shell application?

From Dev

How can I run a Spring Boot application on port 80

From Dev

How to run hawt.io in spring boot application with embedded tomcat

From Dev

How spring batch share data between job

From Dev

How to dynamically schedule a Spring Batch job with ThreadPoolTaskScheduler

Related Related

  1. 1

    How to select which job should run in Spring Batch + Spring Rest API

  2. 2

    How to run spring batch job multiple times with different job parameters?

  3. 3

    How Spring Boot run batch jobs

  4. 4

    Spring batch how to run a job in loop until it meets a condition?

  5. 5

    How to run spring-boot as a client application?

  6. 6

    How to run multiple controllers in a Spring Boot application?

  7. 7

    How to run spring-boot as a client application?

  8. 8

    How to run spring boot application without Eclipse?

  9. 9

    run spring batch job from the controller

  10. 10

    Best approach to run a Spring Batch job

  11. 11

    Restarting a spring batch job that is run by multiple threads

  12. 12

    Spring batch job application hangs with step partition

  13. 13

    Spring batch job application hangs with step partition

  14. 14

    Is there a difference between Run As: Spring Boot App and Run As: Java Application?

  15. 15

    How to run integration test of a spring-boot based application through maven-failsafe-plugin?

  16. 16

    tests for spring boot application are not run

  17. 17

    How to configure EntityManagerFactoryBuilder bean when testing Spring Boot Batch application?

  18. 18

    RabbitTemplate not ending the spring boot + spring batch job app

  19. 19

    Continuously running a Spring Batch job using Spring boot

  20. 20

    How to load Java based SecurityConfig in XML based Spring config file?

  21. 21

    Run Flyway Java-based callbacks with Spring Boot

  22. 22

    Spring-boot application can only be launched with spring-boot:run when forking - java -jar fails

  23. 23

    Spring-boot application can only be launched with spring-boot:run when forking - java -jar fails

  24. 24

    how to run/turn off selective tests based on profiles in spring boot

  25. 25

    How to run a standalone/interactive Spring Boot CRaSH Shell application?

  26. 26

    How can I run a Spring Boot application on port 80

  27. 27

    How to run hawt.io in spring boot application with embedded tomcat

  28. 28

    How spring batch share data between job

  29. 29

    How to dynamically schedule a Spring Batch job with ThreadPoolTaskScheduler

HotTag

Archive