Spring boot check external service status on boot

2.3k views Asked by At

I want check some external http service before my Spring Boot is ready.

The url to the external web service are stored in a property file with a @ConfigurationProperties class.

How do this check i tried using a springApplication.addListner() with a ping method. But the property class have not then been initialized.

public class ApplicationStartListener implements ApplicationListener<ApplicationPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event) {
String url = AppProp.getURL();
inet = InetAddress.getByName(url );
inet.isReachable(5000)

...

application.yml

tops:
    http://service.com

@Component
@ConfigurationProperties("tops")
public class AppProp{

    private static String url;


    public static String getUrl() {
1

There are 1 answers

0
dustin.schultz On

The easiest way to accomplish this is to implement the ApplicationRunner interface.

From the Spring Boot documentation [1]

If you need to run some specific code once the SpringApplication has started, you can implement the ApplicationRunner or CommandLineRunner interfaces. Both interfaces work in the same way and offer a single run method which will be called just before SpringApplication.run(…​) completes.

[1] https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-command-line-runner

Assuming you have url defined in application.properties:

@SpringBootApplication
public class MyApplication implements ApplicationRunner
{

    @Inject
    private AppConfig appConfig;

    @Inject
    private ConfigurableApplicationContext applicationContext;

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

    @Override
    public void run(ApplicationArguments args) throws Exception 
    {
        InetAddress inetAddress = InetAddress.getByName(appConfig.getUrl());
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Component
    @ConfigurationProperties
    public static class AppConfig 
    {
        private String url;

        public String getUrl() 
        {
            return url;
        }

        public void setUrl(String url) 
        {
            this.url = url;
        }
    }
}

If you need even more control than this, you can use SpringApplicationRunListener [2]

[2] http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplicationRunListener.html

@SpringBootApplication
public class MyApplication implements SpringApplicationRunListener
{

    public MyApplication() { }

    public MyApplication(SpringApplication springApplication, String[] args) { }

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

    @Override
    public void started() { }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) 
    {
        // 1st opportunity
        InetAddress inetAddress = InetAddress.getByName(environment.getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) 
    {
        // 2nd opportunity
        InetAddress inetAddress = InetAddress.getByName(context.getEnvironment().getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) 
    {
        // 3rd opportunity
        InetAddress inetAddress = InetAddress.getByName(context.getEnvironment().getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Override
    public void finished(ConfigurableApplicationContext context, Throwable exception) 
    {
        // 4th opportunity
        InetAddress inetAddress = InetAddress.getByName(context.getEnvironment().getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Component
    @ConfigurationProperties
    public static class AppConfig {
        private String url;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

then create META-INF\spring.factories and add

org.springframework.boot.SpringApplicationRunListener=com.foobar.MyApplication