Configuring multiple jobs, CLI launching, and tests
What is the recommended approach(es) to arrange the Spring Batch context files for multiple jobs and their shared beans? And how do you launch them from CLI?
Launching a job from CLI can only take one context file, so in our current setup I made a "spring-batch.xml" file to hold the common Spring beans for all batch jobs and a Spring context file per batch job that imports the common one. To run from CLI, I specify the specific batch job context file and its job name. What are any better approaches to suggest?
For testing the jobs, with our current setup, only one job is loaded per Spring Test context (by specifying @ContextConfiguration location) and it is magically run:
Note that jobParameters only has the runAsDate.
jobExecution = jobLauncherTestUtils.launchJob(jobParameters);
This all worked nicely, except now I've had to add JTA for the transactions. The problem encountered is each test launches another JTA config, and the JTA manager throws exception as it is already loaded (from prior test).
To solve, a colleague suggested I convert to using the AutomaticJobRegistrar so our batch tests can define only one context configuration for all jobs, thereby preventing the additional JTA manager problem.
In starting to change to that approach, it appears I should/can:
- for CLI run, specify the common context file instead of the specific batch job file (all job files will be loaded via the registrar, so no need to specify the specific file on the CLI run)
- then can avoid having each job's Spring context import the common one (as it will be loaded by the job runner for CLI and by @ContextConfiguration for tests; getting rid of imports is a good thing (TM))
When using the AutomaticJobRegistrar, what is the recommended practice(s) to run the correct job with tests? Initial analysis suggests:
- inject MapJobRegistry to test class
Are the better recommended approaches for configuration design and tests?
I am also wondering if I need to use JobRegistryBackgroundJobRunner instead of SimpleJobLauncher when moving to the AutomaticJobRegistrar?