wastl
wastl

Reputation: 2641

How do I execute a method in Spring when the Server is ready?

I have the following problem: I'm using Spring-Boot for a little private web-based project and I want Spring to make a call to a webservice when it's started. And by started I mean "when my application is ready to handle requests".

I've already tried implementing the ApplicationListener<ContextRefreshedEvent> but it did not work, as the Event happend to early (i.e. before the embedded server was ready to handle request). Also the options mentioned in this question did not solve this problem.

My question now is: Is there any possibilty to tell Spring to execute something after the server has finished starting up and is ready to handle requests?

EDIT (in response to Daniel's answer):

The problem is that I need some injected properties to make that webservice call, and since injecting static values does not work in spring this approach is no option.

My listener, that does what I want, just a bit too early looks something like this:



@Component
public class StartupListener implements ApplicationListener{


    @Autowired
    private URLProvider urlProvider;
    @Value("${server.port}")
    private int port;
    @Value("${project.name}")
    private String projectName;

    @Override
    public final void onApplicationEvent(ContextRefreshedEvent event) {
        RestTemplate template = new RestTemplate();
        String url = uriProvider.getWebserviceUrl(this.projectName);
        template.put(url, null);
    }

}

SECOND EDIT:

Although this question solves a very similar problem it seems like I'm not able to inject into the object because it needs to have a constructor of the form (org.springframework.boot.SpringApplication, [Ljava.lang.String;).

Also it would be desirebale to solve it without having to create the spring.factories file but by using annotations.

Upvotes: 3

Views: 1875

Answers (3)

PTT
PTT

Reputation: 556

  @Component
  public class StartUp implements ApplicationListener<WebServerInitializedEvent> {

  private WebClient webClient;

  @Override
  public void onApplicationEvent(WebServerInitializedEvent event) {

    String baseUrl = "http://url.com"
    webClient = WebClient.create(baseUrl);
    executeRestCall(baseUrl+"/info");
  }

  void executeRestCall(String uri) {
    try {
      webClient.get()
          .uri(uri)
          .exchange()
          .block();
    } catch (Exception e) {
      log.error("Request failed for url - {}",uri, e);
    }
  }}

Upvotes: 0

extols
extols

Reputation: 1772

In your component you can use the @PostConstruct annotation. e.g.

@Component
public class StartupListener {


    @Autowired
    private URLProvider urlProvider;
    @Value("${server.port}")
    private int port;
    @Value("${project.name}")
    private String projectName;

    @PostConstruct
    public final void init() {
        RestTemplate template = new RestTemplate();
        String url = uriProvider.getWebserviceUrl(this.projectName);
        template.put(url, null);
    }

}

This will fire once the bean has been initialised and autowiring has taken place.

Upvotes: 0

danielgmarcos
danielgmarcos

Reputation: 174

If I understand what your problem is, you could call the webservice on your application main, right after it initiates.

public static void main(String[] args) {

        new SpringApplication(Application.class).run(args);
        //call the webservice for you to handle...
    }

I'm not sure if this is what you want...

Upvotes: 1

Related Questions