Spring boot application for background workers

yaseco :

I have defined the following Spring boot application:

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run(args);
    }

    public void run(String... args) throws Exception {
        Thread.currentThread().join();
    }
}

I also have a package of workers (i.e. could be classes which implements Runnable). They are supposed to run indefinitely.

What is the "Spring way" to run them? (and doing so automatically, without explicitly knowing them)

Thanks

moilejter :

You could (1) have your classes that implement Runnable be annotated with @Component, so that Spring can find them. You can then (2) write a WorkManager, annotated with @Service, with an @Autowired List - which Spring would initialize with a list instances of all your classes from (1). And could (3) write a @PostConstruct method in your WorkManager that would iterate over that list of Runnables, and pass each one to a TaskExecutor to run...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=90323&siteId=1