1

I have written a Spring Boot console application (version 2.2.4. Spring-data-solr version 4.1.4) using CommandLineRunner interface. I have wired in a service that starts up a SolrTemplate connect. I use this to run a few queries against a Solr Index and then want to shut down the application.

When I debug the application I can step through the entire app, but when the app leaves main(), then it hangs. I assume that SolrTemplate is waiting for another query. Is there a way to shutdown SolrTemplate?

@Configuration
@EnableSolrRepositories(basePackages = { "org.ihc.hdd.serviceExample.facet.repository" })
public class SolrConfig {

    @Value("${spring.data.solr.zk-host}")
    private String zkHost;

    @Bean
    public SolrTemplate solrTemplate() throws Exception {
        String[] zkHosts = this.zkHost.split(",");

        CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Arrays.asList(zkHosts), Optional.empty()).build();
        return new SolrTemplate(cloudSolrClient);
    }

}
Todd347
  • 73
  • 6

1 Answers1

0

How about creating a CloudSolrClient Bean like so :

 @Bean
 protected static CloudSolrClient SolrClient() {
     String[] zkHosts = this.zkHost.split(",");
     CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Arrays.asList(zkHosts), Optional.empty()).build();

     return new CloudSolrClient(zkHostString);
 }

and passing it as an argument to SolrTemplate like :

    @Bean
    public SolrTemplate solrTemplate(CloudSolrClient cloudSolrClient) throws Exception {
    return new SolrTemplate(cloudSolrClient);
   }

and then Closing the CloudSolrClient which will most likely terminate the template with it

    if(cloudSolrClient != null)
         cloudSolrClient.close();
yassine yousfi
  • 79
  • 2
  • 11