Example usage for org.apache.solr.client.solrj.impl HttpSolrClient ping

List of usage examples for org.apache.solr.client.solrj.impl HttpSolrClient ping

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl HttpSolrClient ping.

Prototype

public SolrPingResponse ping() throws SolrServerException, IOException 

Source Link

Document

Issues a ping request to check if the server is alive

Usage

From source file:edu.tamu.tcat.trc.digires.books.hathitrust.HTFilesSearchService.java

License:Apache License

public void activate() {
    Objects.requireNonNull(props, "Cannot connect to Solr Server. Configuration data is not available.");

    final URI solrEndpoint = props.getPropertyValue("solr.api.endpoint", URI.class);
    final String core = props.getPropertyValue("hathifiles", String.class);
    ExecutorService exec = Executors
            .newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("htfiles").build());
    // check to ensure that the requested SolrServer can be contacted. Run this in a background
    // thread to not prevent the service from initializing in a timely manner. Use the Future to
    // determine that the ping succeeded. Each access to the Future will either return the server
    // or re-throw the original exception, allowing it to be noticed when requested as well as
    // on system startup.
    solrServerFuture = exec.submit(() -> {
        try {//w ww.java  2 s  . c om
            HttpSolrClient server = new HttpSolrClient(solrEndpoint.resolve(core).toString());
            SolrPingResponse pingResponse = server.ping();
            if (pingResponse == null || (pingResponse.getStatus() > 299 && pingResponse.getStatus() < 200)) {
                //TODO: server.shutdown() here? ping failed, but still need to release resources?
                throw new IllegalStateException("Failed to ping configured solr server ["
                        + solrEndpoint.resolve(core) + "]: " + pingResponse);
            }
            return server;
        } catch (IOException | SolrServerException ex) {
            //TODO: server.shutdown() here? ping failed, but still need to release resources?
            throw new IllegalStateException(
                    "Failed to ping configured solr server [" + solrEndpoint.resolve(core) + "]", ex);
        } finally {
            // only needed the executor for this task, so shut it down and retain the Future
            exec.shutdown();
        }
    });
}