1

i have a treatment in the method run of the CommandLineRunner interface that creates indexes in elasticsearch, the creation of indexes is done after the application is run. in the console of elasticsearch i can see the the creation's trace. for example:

[2018-05-29T14:22:38,579][INFO ][o.e.c.m.MetaDataCreateIndexService] [oYrx3Ep] [country] creating index, cause [api], templates [], shards [5]/[1], mappings []

[2018-05-29T14:23:41,296][INFO ][o.e.c.m.MetaDataCreateIndexService] [oYrx3Ep] [category] creating index, cause [api], templates [], shards [5]/[1], mappings []

...

how can i execute the method when the app is still in the process of being run, i don't want it to be executed after the app is run

  @Override
  public void run(String... args) throws Exception {
       ....
        try {
          Response response = elasticsearchConfiguration.restClient().performRequest("HEAD", "/" + indexName);
          Integer statusCode = response.getStatusLine().getStatusCode();
          if (statusCode.equals(STATUS_CODE)) {
            restHighLevelClient.indices().create(new CreateIndexRequest(indexName));
          }
        } catch (IOException e) {
          logger.error(ERROR + e);
        }
       ....
}
Ragnar
  • 21
  • 1
  • 5
  • https://stackoverflow.com/questions/27405713/running-code-after-spring-boot-starts has several ways, in a component a `@PostConstruct` or `@EventListener(ContextRefreshedEvent.class)` annotated method for example. Also http://www.baeldung.com/running-setup-logic-on-startup-in-spring is a nice overview – zapl May 29 '18 at 14:18

0 Answers0