/** * Create a new client object using multiple string values in a Collection instead of a standard * zkHost connection string. Note that this method will not be used if there is only one String * argument - that will use {@link #CloudSolrServer(String)} instead. * * @param zkHosts A Java Collection (List, Set, etc) of HOST:PORT strings, one for each host in * the zookeeper ensemble. Note that with certain Collection types like HashSet, the order of * hosts in the final connect string may not be in the same order you added them. * @param chroot A chroot value for zookeeper, starting with a forward slash. If no chroot is * required, use null. * @param httpClient the {@link HttpClient} instance to be used for all requests. The provided * httpClient should use a multi-threaded connection manager. * @throws IllegalArgumentException if the chroot value does not start with a forward slash. * @see #CloudSolrServer(String) */ public CloudSolrServer(Collection<String> zkHosts, String chroot, HttpClient httpClient) { StringBuilder zkBuilder = new StringBuilder(); int lastIndexValue = zkHosts.size() - 1; int i = 0; for (String zkHost : zkHosts) { zkBuilder.append(zkHost); if (i < lastIndexValue) { zkBuilder.append(","); } i++; } if (chroot != null) { if (chroot.startsWith("/")) { zkBuilder.append(chroot); } else { throw new IllegalArgumentException("The chroot must start with a forward slash."); } } /* Log the constructed connection string and then initialize. */ log.info("Final constructed zkHost string: " + zkBuilder.toString()); this.zkHost = zkBuilder.toString(); this.clientIsInternal = httpClient == null; this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient; this.lbServer = new LBHttpSolrServer(myClient); this.lbServer.setRequestWriter(new BinaryRequestWriter()); this.lbServer.setParser(new BinaryResponseParser()); this.updatesToLeaders = true; shutdownLBHttpSolrServer = true; }
/** * @param zkHost A zookeeper client endpoint. * @param updatesToLeaders If true, sends updates only to shard leaders. * @param httpClient the {@link HttpClient} instance to be used for all requests. The provided * httpClient should use a multi-threaded connection manager. * @see #CloudSolrServer(String) for full description and details on zkHost */ public CloudSolrServer(String zkHost, boolean updatesToLeaders, HttpClient httpClient) { this.zkHost = zkHost; this.clientIsInternal = httpClient == null; this.myClient = httpClient == null ? HttpClientUtil.createClient(null) : httpClient; this.lbServer = new LBHttpSolrServer(myClient); this.lbServer.setRequestWriter(new BinaryRequestWriter()); this.lbServer.setParser(new BinaryResponseParser()); this.updatesToLeaders = updatesToLeaders; shutdownLBHttpSolrServer = true; lbServer.addQueryParams(STATE_VERSION); }
@Test public void testGetRawStream() throws SolrServerException, IOException { try (CloseableHttpClient client = HttpClientUtil.createClient(null)) { HttpSolrClient solrClient = new HttpSolrClient(jetty.getBaseUrl().toString() + "/collection1", client, null); QueryRequest req = new QueryRequest(); NamedList response = solrClient.request(req); InputStream stream = (InputStream) response.get("stream"); assertNotNull(stream); stream.close(); } }
@Test public void testCompression() throws Exception { try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) { SolrQuery q = new SolrQuery("*:*"); // verify request header gets set DebugServlet.clear(); try { client.query(q); } catch (ParseException ignored) { } assertNull(DebugServlet.headers.get("Accept-Encoding")); client.setAllowCompression(true); try { client.query(q); } catch (ParseException ignored) { } assertNotNull(DebugServlet.headers.get("Accept-Encoding")); client.setAllowCompression(false); try { client.query(q); } catch (ParseException ignored) { } assertNull(DebugServlet.headers.get("Accept-Encoding")); } // verify server compresses output HttpGet get = new HttpGet(jetty.getBaseUrl().toString() + "/collection1" + "/select?q=foo&wt=xml"); get.setHeader("Accept-Encoding", "gzip"); CloseableHttpClient httpclient = HttpClientUtil.createClient(null); HttpEntity entity = null; try { HttpResponse response = httpclient.execute(get); entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); assertEquals("gzip", ceheader.getValue()); } finally { if (entity != null) { entity.getContent().close(); } httpclient.close(); } // verify compressed response can be handled try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) { client.setAllowCompression(true); SolrQuery q = new SolrQuery("foo"); QueryResponse response = client.query(q); assertEquals(0, response.getStatus()); } }
@Test public void testSetParametersExternalClient() throws IOException { try (CloseableHttpClient httpClient = HttpClientUtil.createClient(null); HttpSolrClient solrClient = new HttpSolrClient(jetty.getBaseUrl().toString(), httpClient)) { try { solrClient.setMaxTotalConnections(1); fail("Operation should not succeed."); } catch (UnsupportedOperationException ignored) { } try { solrClient.setDefaultMaxConnectionsPerHost(1); fail("Operation should not succeed."); } catch (UnsupportedOperationException ignored) { } } }
public HttpSolrServer(String baseURL, HttpClient client, ResponseParser parser) { this.baseUrl = baseURL; if (baseUrl.endsWith("/")) { baseUrl = baseUrl.substring(0, baseUrl.length() - 1); } if (baseUrl.indexOf('?') >= 0) { throw new RuntimeException( "Invalid base url for solrj. The base URL must not contain parameters: " + baseUrl); } if (client != null) { httpClient = client; internalClient = false; } else { internalClient = true; ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, 128); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 32); params.set(HttpClientUtil.PROP_FOLLOW_REDIRECTS, followRedirects); httpClient = HttpClientUtil.createClient(params); } this.parser = parser; }