LumberSzquatch
LumberSzquatch

Reputation: 1083

Spring Boot Application with Spring Batch not Running Jobs

I have spring boot application I created from here at spring's site. I'm tampering with it and have basically duplicated it into two jobs, as I'm creating a bigger application that will be structured this way. Nothing is running though and I know it's most likely something small I'm missing.

If I stuff everything into one package just like the tutorial, it works fine, but if I extract the Application.java out into its own package, it seems to not acknowledge the other two packages. Even with passing through -Dspring.batch.job.names=myJob.

This is all there is to my main class:

@SpringBootApplication
@EnableBatchProcessing
public class Application implements CommandLineRunner {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("Running...");

    }
}

The project structure looks like:

\---main
    +---java
    |   +---app
    |   |       Application.java
    |   |       
    |   +---job1
    |   |       Job1BatchConfiguration.java
    |   |       Job1JobCompletionNotificationListener.java
    |   |       Job1Person.java
    |   |       Job1PersonItemProcessor.java
    |   |       
    |   \---job2
    |           Job2BatchConfiguration.java
    |           Job2JobCompletionNotificationListener.java
    |           Job2Person.java
    |           Job2PersonItemProcessor.java
    |           
    \---resources
            sample-data-2.csv
            sample-data.csv
            schema-all.sql

Again, if I put Application.java into either the job1 or job2 package, that job will execute, if I stuff all of them into a single package and pass through -Dspring.batch.job.names=job1,job2, it will execute both (or one if I want). But how can I get this command to work when the Application.java is at a different package scope? Is there a way to make it see those?


EDIT: So it looks like I can annotate my Application.java with

@Import({Job1BatchConfiguration.class, Job1JobCompletionNotificationListener.class, Job2BatchConfiguration.class, Job2JobCompletionNotificationListener.class})

But if I exceed more than even three jobs, this solution seems to get really sloppy. Any way to condense this functionality for the jobs?

Upvotes: 4

Views: 9657

Answers (2)

My problem was solved putting my main class in package root of my struct.

Using the example showed, the location of Application is:

\---main
    +---java
    |   +---Application.java (Change location this file to package root)
    |   |       
    |   +---job1
    |   |       Job1BatchConfiguration.java
    |   |       Job1JobCompletionNotificationListener.java
    |   |       Job1Person.java
    |   |       Job1PersonItemProcessor.java
    |   |       
    |   \---job2
    |           Job2BatchConfiguration.java
    |           Job2JobCompletionNotificationListener.java
    |           Job2Person.java
    |           Job2PersonItemProcessor.java
    |           
    \---resources
            sample-data-2.csv
            sample-data.csv
            schema-all.sql

Upvotes: 2

dimitrisli
dimitrisli

Reputation: 21411

In your Application class add the annotation of @ComponentScan pointing at a higher package:

@ComponentScan("app")

or alternatively you can include multiple packages as well:

@ComponentScan("app.job1", "app.job2")

This annotation scans the packages and registers not only @Component or other beans but also @Configuration classes too.

Upvotes: 3

Related Questions