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

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

Introduction

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

Prototype

@Deprecated
public void setConnectionTimeout(int timeout) 

Source Link

Document

HttpConnectionParams.setConnectionTimeout

Usage

From source file:com.frank.search.solr.server.support.HttpSolrClientFactoryBean.java

License:Apache License

private void createHttpSolrClient() {
    HttpSolrClient httpSolrClient = new HttpSolrClient(this.url);
    if (timeout != null) {
        httpSolrClient.setConnectionTimeout(timeout.intValue());
    }//from ww w  .  j a va  2 s.  c  o  m
    if (maxConnections != null) {
        httpSolrClient.setMaxTotalConnections(maxConnections);
    }
    this.setSolrClient(httpSolrClient);
}

From source file:com.ilscipio.scipio.solr.util.ScipioHttpSolrClient.java

License:Apache License

/**
 * Creates a new client from URL and username/password, where all operations will use
 * the given auth./*from w  w w.jav  a  2 s  .c  o  m*/
 * <p>
 * DEV NOTE: Implementation must be maintained with the superclass; the default values
 * are taken from {@link HttpSolrClient.Builder} and are subject to change at solrj updates.
 */
public static HttpSolrClient create(String baseURL, HttpClient httpClient, String solrUsername,
        String solrPassword, Integer maxConnections, Integer maxConnectionsPerHost, Integer connectTimeout,
        Integer socketTimeout) {

    if (httpClient == null) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        if (maxConnections != null) {
            params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, maxConnections);
        }
        if (maxConnectionsPerHost != null) {
            params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, maxConnectionsPerHost);
        }
        params.set(HttpClientUtil.PROP_FOLLOW_REDIRECTS, true);
        httpClient = HttpClientUtil.createClient(params);
    }

    // DEV NOTE: the defaults must match what HttpSolrClient.Builder does! Must keep up to date!
    HttpSolrClient client = new ScipioHttpSolrClient(baseURL, httpClient, new BinaryResponseParser(), false,
            new ModifiableSolrParams(), solrUsername, solrPassword);

    // TODO: In Solr 7, these are deprecated and moved to Builder/constructor 
    if (connectTimeout != null) {
        client.setConnectionTimeout(connectTimeout);
    }
    if (socketTimeout != null) {
        client.setSoTimeout(socketTimeout);
    }

    return client;
}

From source file:fr.paris.lutece.plugins.search.solr.business.SolrServerService.java

License:Open Source License

/**
* Creates the SolrServer./*from  ww w.  j  a v a 2s . c  o m*/
* @param strServerUrl the Solr server url
* @return the SolrServer.
*/
private SolrClient createSolrServer(String strServerUrl) {
    HttpSolrClient solrServer = new HttpSolrClient(strServerUrl);
    if (SOLR_SERVER_MAX_CONNECTION != NO_MAX_CONNECTION_SET) {
        solrServer.setMaxTotalConnections(SOLR_SERVER_MAX_CONNECTION);
    }
    solrServer.setConnectionTimeout(SOLR_TIMEOUT);
    return solrServer;
}

From source file:org.apache.ranger.audit.provider.solr.SolrAuditProvider.java

License:Apache License

void connect() {
    SolrClient me = solrClient;/*from w w w .jav  a 2s .c  o m*/
    if (me == null) {
        synchronized (lock) {
            me = solrClient;
            if (me == null) {
                final String solrURL = MiscUtil.getStringProperty(props, "xasecure.audit.solr.solr_url");

                if (lastConnectTime != null) {
                    // Let's wait for enough time before retrying
                    long diff = System.currentTimeMillis() - lastConnectTime.getTime();
                    if (diff < retryWaitTime) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Ignore connecting to solr url=" + solrURL + ", lastConnect=" + diff
                                    + "ms");
                        }
                        return;
                    }
                }
                lastConnectTime = new Date();

                if (solrURL == null || solrURL.isEmpty()) {
                    LOG.fatal("Solr URL for Audit is empty");
                    return;
                }

                try {
                    // TODO: Need to support SolrCloud also
                    PrivilegedExceptionAction<SolrClient> action = new PrivilegedExceptionAction<SolrClient>() {
                        @Override
                        public SolrClient run() throws Exception {
                            SolrClient solrClient = new HttpSolrClient(solrURL);
                            return solrClient;
                        };
                    };
                    UserGroupInformation ugi = MiscUtil.getUGILoginUser();
                    if (ugi != null) {
                        solrClient = ugi.doAs(action);
                    } else {
                        solrClient = action.run();
                    }
                    me = solrClient;
                    if (solrClient instanceof HttpSolrClient) {
                        HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;
                        httpSolrClient.setAllowCompression(true);
                        httpSolrClient.setConnectionTimeout(1000);
                        // solrClient.setSoTimeout(10000);
                        httpSolrClient.setMaxRetries(1);
                    }
                } catch (Throwable t) {
                    LOG.fatal("Can't connect to Solr server. URL=" + solrURL, t);
                }
            }
        }
    }
}

From source file:org.apache.ranger.patch.cliutil.DbToSolrMigrationUtil.java

License:Apache License

private SolrClient createSolrClient() throws Exception {
    SolrClient solrClient = null;/* w ww  .  ja  va 2 s .  co m*/

    registerSolrClientJAAS();
    String zkHosts = PropertiesUtil.getProperty(SOLR_ZK_HOSTS);
    if (zkHosts == null) {
        zkHosts = PropertiesUtil.getProperty("ranger.audit.solr.zookeeper");
    }
    if (zkHosts == null) {
        zkHosts = PropertiesUtil.getProperty("ranger.solr.zookeeper");
    }

    String solrURL = PropertiesUtil.getProperty(SOLR_URLS_PROP);
    if (solrURL == null) {
        // Try with url
        solrURL = PropertiesUtil.getProperty("ranger.audit.solr.url");
    }
    if (solrURL == null) {
        // Let's try older property name
        solrURL = PropertiesUtil.getProperty("ranger.solr.url");
    }

    if (zkHosts != null && !zkHosts.trim().equals("") && !zkHosts.trim().equalsIgnoreCase("none")) {
        zkHosts = zkHosts.trim();
        String collectionName = PropertiesUtil.getProperty(SOLR_COLLECTION_NAME);
        if (collectionName == null || collectionName.equalsIgnoreCase("none")) {
            collectionName = DEFAULT_COLLECTION_NAME;
        }

        logger.info("Solr zkHosts=" + zkHosts + ", collectionName=" + collectionName);

        try {
            // Instantiate
            HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer());
            CloudSolrClient solrCloudClient = new CloudSolrClient(zkHosts);
            solrCloudClient.setDefaultCollection(collectionName);
            solrClient = solrCloudClient;
            solrCloudClient.close();
        } catch (Exception e) {
            logger.fatal(
                    "Can't connect to Solr server. ZooKeepers=" + zkHosts + ", collection=" + collectionName,
                    e);
            throw e;
        }
    } else {
        if (solrURL == null || solrURL.isEmpty() || solrURL.equalsIgnoreCase("none")) {
            logger.fatal("Solr ZKHosts and URL for Audit are empty. Please set property " + SOLR_ZK_HOSTS
                    + " or " + SOLR_URLS_PROP);
        } else {
            try {
                HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer());
                solrClient = new HttpSolrClient(solrURL);
                if (solrClient instanceof HttpSolrClient) {
                    HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;
                    httpSolrClient.setAllowCompression(true);
                    httpSolrClient.setConnectionTimeout(1000);
                    httpSolrClient.setMaxRetries(1);
                    httpSolrClient.setRequestWriter(new BinaryRequestWriter());
                }
            } catch (Exception e) {
                logger.fatal("Can't connect to Solr server. URL=" + solrURL, e);
                throw e;
            }
        }
    }
    return solrClient;
}

From source file:org.apache.ranger.solr.SolrMgr.java

License:Apache License

void connect() {
    if (!initDone) {
        synchronized (lock) {
            if (!initDone) {
                if (rangerBizUtil.getAuditDBType().equalsIgnoreCase("solr")) {
                    String zkHosts = PropertiesUtil.getProperty(SOLR_ZK_HOSTS);
                    if (zkHosts == null) {
                        zkHosts = PropertiesUtil.getProperty("ranger.audit.solr.zookeeper");
                    }//  ww  w  .  j  a  v  a 2  s  .  c om
                    if (zkHosts == null) {
                        zkHosts = PropertiesUtil.getProperty("ranger.solr.zookeeper");
                    }

                    String solrURL = PropertiesUtil.getProperty(SOLR_URLS_PROP);

                    if (solrURL == null) {
                        // Try with url
                        solrURL = PropertiesUtil.getProperty("ranger.audit.solr.url");
                    }
                    if (solrURL == null) {
                        // Let's try older property name
                        solrURL = PropertiesUtil.getProperty("ranger.solr.url");
                    }

                    if (zkHosts != null && !zkHosts.trim().equals("")
                            && !zkHosts.trim().equalsIgnoreCase("none")) {
                        zkHosts = zkHosts.trim();
                        String collectionName = PropertiesUtil.getProperty(SOLR_COLLECTION_NAME);
                        if (collectionName == null || collectionName.equalsIgnoreCase("none")) {
                            collectionName = DEFAULT_COLLECTION_NAME;
                        }

                        logger.info("Solr zkHosts=" + zkHosts + ", collectionName=" + collectionName);

                        try {
                            // Instantiate
                            HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer());
                            CloudSolrClient solrCloudClient = new CloudSolrClient(zkHosts);
                            solrCloudClient.setDefaultCollection(collectionName);
                            solrClient = solrCloudClient;
                        } catch (Throwable t) {
                            logger.fatal("Can't connect to Solr server. ZooKeepers=" + zkHosts + ", collection="
                                    + collectionName, t);
                        }

                    } else {
                        if (solrURL == null || solrURL.isEmpty() || solrURL.equalsIgnoreCase("none")) {
                            logger.fatal("Solr ZKHosts and URL for Audit are empty. Please set property "
                                    + SOLR_ZK_HOSTS + " or " + SOLR_URLS_PROP);
                        } else {
                            try {
                                HttpClientUtil.setConfigurer(new Krb5HttpClientConfigurer());
                                solrClient = new HttpSolrClient(solrURL);
                                if (solrClient == null) {
                                    logger.fatal("Can't connect to Solr. URL=" + solrURL);
                                } else {
                                    if (solrClient instanceof HttpSolrClient) {
                                        HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;
                                        httpSolrClient.setAllowCompression(true);
                                        httpSolrClient.setConnectionTimeout(1000);
                                        // httpSolrClient.setSoTimeout(10000);
                                        httpSolrClient.setMaxRetries(1);
                                        httpSolrClient.setRequestWriter(new BinaryRequestWriter());
                                    }
                                    initDone = true;
                                }

                            } catch (Throwable t) {
                                logger.fatal("Can't connect to Solr server. URL=" + solrURL, t);
                            }
                        }
                    }
                }

            }
        }
    }
}

From source file:org.dbflute.solr.bhv.AbstractSolrBehavior.java

License:Apache License

/**
 * Create HttpSolrClient associated with the assigned URL.
 * @param url URL (NotNull)//from ww  w. j a v  a  2  s .  com
 * @return HttpSolrClient (NotNull)
 */
protected HttpSolrClient createHttpSolrClient(String url) {
    HttpSolrClient client = new HttpSolrClient(url);
    client.setConnectionTimeout(getConnectionTimeout());
    client.setSoTimeout(getSocketTimeout());
    client.setDefaultMaxConnectionsPerHost(getDefaultMaxConnectionsPerHost());
    client.setMaxTotalConnections(getMaxTotalConnections());
    client.setFollowRedirects(false);
    return client;
}

From source file:org.seedstack.solr.internal.SolrPlugin.java

License:Mozilla Public License

private SolrClient buildHttpSolrClient(Configuration solrClientConfiguration, String url) {
    HttpSolrClient httpSolrClient = new HttpSolrClient(url);

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.CONNECTION_TIMEOUT)) {
        httpSolrClient.setConnectionTimeout(
                solrClientConfiguration.getInt(SolrConfigurationConstants.CONNECTION_TIMEOUT));
    }/*from   w  w  w .j  a  v a  2 s  . c om*/

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.QUERY_PARAMS)) {
        httpSolrClient.setQueryParams(Sets
                .newHashSet(solrClientConfiguration.getStringArray(SolrConfigurationConstants.QUERY_PARAMS)));
    }

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.SO_TIMEOUT)) {
        httpSolrClient.setSoTimeout(solrClientConfiguration.getInt(SolrConfigurationConstants.SO_TIMEOUT));
    }

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.ALLOW_COMPRESSION)) {
        httpSolrClient.setAllowCompression(
                solrClientConfiguration.getBoolean(SolrConfigurationConstants.ALLOW_COMPRESSION));
    }

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.MAX_CONNECTIONS_PER_HOST)) {
        httpSolrClient.setDefaultMaxConnectionsPerHost(
                solrClientConfiguration.getInt(SolrConfigurationConstants.MAX_CONNECTIONS_PER_HOST));
    }

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.FOLLOW_REDIRECTS)) {
        httpSolrClient.setFollowRedirects(
                solrClientConfiguration.getBoolean(SolrConfigurationConstants.FOLLOW_REDIRECTS));
    }

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.MAX_TOTAL_CONNECTIONS)) {
        httpSolrClient.setMaxTotalConnections(
                solrClientConfiguration.getInt(SolrConfigurationConstants.MAX_TOTAL_CONNECTIONS));
    }

    if (solrClientConfiguration.containsKey(SolrConfigurationConstants.USE_MULTI_PART_HOST)) {
        httpSolrClient.setUseMultiPartPost(
                solrClientConfiguration.getBoolean(SolrConfigurationConstants.USE_MULTI_PART_HOST));
    }

    return httpSolrClient;
}

From source file:org.springframework.data.solr.server.support.SolrClientFactoryBean.java

License:Apache License

/**
 * Creates an {@link HttpSolrClient}.//w w  w . j  a  v a  2  s  .  c  o m
 */
private void createHttpSolrClient() {
    final HttpSolrClient solrClient = new HttpSolrClient(path);

    if (timeout != null) {
        solrClient.setConnectionTimeout(timeout);
    }
    if (maxConnections != null) {
        solrClient.setMaxTotalConnections(maxConnections);
    }

    setSolrClient(solrClient);
}