Spring Boot Multiple Application Runner

This is very simple example using Spring Boot with 2 stand alone application in 1 project.

The point is you need Java config class and a class which implements ApplicationRunner Interface(or CommandLineRunner) for each job.
And use @ConditionalOnProperty as parameter when execute this.

Moreover if you want it to exit with execution's status like 0 or 1, just throw RuntimeException implements ExitCodeGenerator Interface throws exit code.

  1. TaskSuccessConfig.java
  2. TaskFailureConfig.java
  • ApplicationRunner
  1. TaskSuccess.java
  2. TaskFailure.java
  • package tree

f:id:tomoTaka:20170429205247p:plain

  • TaskSuccess execution log
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar --task=success

You can see the arguments [--task=success] in the log.
f:id:tomoTaka:20170429210536p:plain

  • TaskFailure execution log
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar --task=failure
You can see the exit code is 1:Failure in the log.

f:id:tomoTaka:20170429210546p:plain

  • DemoApplication.java
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

TaskSuccess

  • TaskSuccessConfig.java
@Configuration
@ConditionalOnProperty(name={"task"}, havingValue="success")
public class TaskSuccessConfig {
    @Bean
    public ApplicationRunner getRunner() {
        return new TaskSuccess();
    }
}
public class TaskSuccess implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(TaskSuccess.class);
    @Autowired
    private DemoService demoService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("args=[{}]", args.getSourceArgs());
        try {
            demoService.convertInt("123");
        } catch (Exception e) {
            throw new FailureException(e.getMessage());
        }
    }
}

TaskFailure

  • TaskFailureConfig.java
@Configuration
@ConditionalOnProperty(name = { "task" }, havingValue = "failure")
public class TaskFailureConfig {
    @Bean
    public ApplicationRunner getRunner() {
        return new TaskFailure();
    }
}
public class TaskFailure implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(TaskFailure.class);
    @Autowired
    private DemoService demoService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("args=[{}]", args.getSourceArgs());
        try {
            demoService.convertInt("abc");
        } catch (Exception e) {
            throw new FailureException(e.getMessage());
        }
    }
}

common class

  • FailureException.java
public class FailureException extends RuntimeException implements ExitCodeGenerator {
    private static final long serialVersionUID = 1L;
    public FailureException(String message) {
        super(message);
    }
    @Override
    public int getExitCode() {
        return 1; // you can set the exit code here
    }
}
  • DemoServiceImpl.java
@Service
public class DemoServiceImpl implements DemoService {
    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
    @Override
    public void convertInt(String val) {
        logger.info("val=[{}]", val);
        Integer intVal = Integer.valueOf(val);
        logger.info("int val=[{}]", intVal);
    }
}

whole code is here.
github.com

I really enjoy coding with Spring Boot!!!

keep coding ;-)