I have a springboot application in the form
src/main/java/example
- Application.java
- JobConfiguration.java
scheduler
- Job1.java
- Job1Runner.java
- Job2.java
- Job2Runner.java
I like to be able to run my jobs on demand locally so I create a seperate Runner class for every job (e.g JobRunner.java
) but currently when I run my Application
class it also runs my Job1Runner
class because it extends CommandLineRunner. Is there a way I can keep my runners seperate? (so the Application
class doesnt run them, and the JobRunner
s dont run each other etc)
Application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
JobConfiguration
@Configuration
@EnableScheduling
public class JobConfiguration implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// Register jobs
}
}
Example JobRunner
@SpringBootApplication(scanBasePackages = "com.example")
public class JobXRunner implements CommandLineRunner { // Where X is my job index
@Autowired
private final JobX job;
@Override
public void run(String... args) throws Exception {
job.run();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(JobXRunner.class, args);
}
}