4

I am trying to run a Spring Boot Application with multiple CommandLineRunner implementations in hope, that all run methods will be started.

But it is only one of them, anyway both Implementations are created.

First:

@Component
public class TestRunnerA implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        logger.info("starting: TestRunnerA");
        consume();
    }
}

Second:

   @Component
   public class TestRunnerB implements CommandLineRunner {

        @Override
        public void run(String... args) throws Exception {
            logger.info("starting: TestRunnerB");
            consume();
        }
    }

In this case, only the Run() Method of TestRunnerA ist called.

Does somebody know why?

I tried adding a @Order annotation, but still... (the first in the order is called)

Kind regards, Knut

J-Alex
  • 6,881
  • 10
  • 46
  • 64
Knut
  • 136
  • 2
  • 11
  • I found my Problem, since they are called synced, their run methods are called one bye one, but I am starting a while loop in each, hence only the first one was starting. – Knut Jan 11 '17 at 20:15

2 Answers2

0

You can annotate different runners with different spring profiles and specify required profile in your launching script using:

-Dspring.profiles.active=YourProfile
J-Alex
  • 6,881
  • 10
  • 46
  • 64
dimus
  • 36
  • 2
0

You could make a single SpringBootApplication that implements CommandLineRunner then inject all the other runners as @Component and use the first argument to delegate which command should take the rest of the arguments. Here is an example in my answer here: https://stackoverflow.com/a/58777948/986160

Important: be careful though if you put it in a crontab one call will destroy the previous one if it is not done.

Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106