Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
resolves #53 by moving httpClient setting on jestClient before starti…
Browse files Browse the repository at this point in the history
…ng NodeChecker. also adds default discovery frequencies in ClientConfig.
  • Loading branch information
Cihat Keser committed Jul 11, 2013
1 parent dda3159 commit d62daa0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/main/java/io/searchbox/client/JestClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@
/**
* @author Dogukan Sonmez
*/


public class JestClientFactory {

final static Logger log = LoggerFactory.getLogger(JestClientFactory.class);
private ClientConfig clientConfig;

public JestClient getObject() {
JestHttpClient client = new JestHttpClient();
HttpClient httpclient;

if (clientConfig != null) {
log.debug("Creating HTTP client based on configuration");
HttpClient httpclient;
client.setServers(clientConfig.getServerList());
boolean isMultiThreaded = clientConfig.isMultiThreaded();
if (isMultiThreaded) {
Expand All @@ -58,7 +56,14 @@ public JestClient getObject() {
log.debug("Default http client is created without multi threaded option");
}

//set discovery
// set custom gson instance
Gson gson = clientConfig.getGson();
if (gson != null) {
client.setGson(gson);
}

client.setHttpClient(httpclient);
//set discovery (should be set after setting the httpClient on jestClient)
if (clientConfig.isDiscoveryEnabled()) {
log.info("Node Discovery Enabled...");
NodeChecker nodeChecker = new NodeChecker(clientConfig, client);
Expand All @@ -67,20 +72,15 @@ public JestClient getObject() {
} else {
log.info("Node Discovery Disabled...");
}

// set custom gson instance
Gson gson = clientConfig.getGson();
if (gson != null) {
client.setGson(gson);
}
} else {
log.debug("There is no configuration to create http client. Going to create simple client with default values");
httpclient = new DefaultHttpClient();
client.setHttpClient(new DefaultHttpClient());
LinkedHashSet<String> servers = new LinkedHashSet<String>();
servers.add("http://localhost:9200");
client.setServers(servers);
}
client.setHttpClient(httpclient);


try {
client.setAsyncClient(new DefaultHttpAsyncClient());
} catch (IOReactorException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/searchbox/client/config/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public static class Builder {
private Integer defaultMaxTotalConnectionPerRoute;
private Map<HttpRoute, Integer> maxTotalConnectionPerRoute = new HashMap<HttpRoute, Integer>();
private boolean isDiscoveryEnabled;
private long discoveryFrequency;
private TimeUnit discoveryFrequencyTimeUnit;
private long discoveryFrequency = 10L;
private TimeUnit discoveryFrequencyTimeUnit = TimeUnit.SECONDS;
private Gson gson;

public Builder(ClientConfig clientConfig) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.searchbox.client;

import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchAdminClient;
import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchNode;
import com.github.tlrx.elasticsearch.test.support.junit.runners.ElasticsearchRunner;
import io.searchbox.client.config.ClientConfig;
import io.searchbox.client.http.JestHttpClient;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.node.Node;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

/**
* @author cihat keser
*/
@RunWith(ElasticsearchRunner.class)
@ElasticsearchNode
public class JestClientFactoryIntegrationTest {


@ElasticsearchNode(name = "2nd")
Node node;
@ElasticsearchAdminClient
AdminClient adminClient;
JestClientFactory factory = new JestClientFactory();

@Test
public void testDiscovery() throws InterruptedException {
// wait for 2 active nodes
adminClient.cluster().prepareHealth().setWaitForGreenStatus().
setWaitForNodes("2").setWaitForRelocatingShards(0).execute().actionGet();

factory.setClientConfig(new ClientConfig
.Builder("http://localhost:9200")
.discoveryEnabled(true)
.discoveryFrequency(1l, TimeUnit.SECONDS)
.build());
JestHttpClient jestClient = (JestHttpClient) factory.getObject();
assertNotNull(jestClient);

// wait for NodeChecker to do the discovery
Thread.sleep(3000);

assertEquals("All 2 nodes should be discovered and be in the client's server list", 2, jestClient.getServers().size());
}
}
11 changes: 11 additions & 0 deletions src/test/java/io/searchbox/client/JestClientFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public void clientCreation() {
assertTrue(jestClient.getServers().contains("http://localhost:9200"));
}

@Test
public void clientCreationWithDiscovery() {
factory.setClientConfig(new ClientConfig.Builder("http://localhost:9200").discoveryEnabled(true).build());
JestHttpClient jestClient = (JestHttpClient) factory.getObject();
assertTrue(jestClient != null);
assertNotNull(jestClient.getAsyncClient());
assertTrue(jestClient.getHttpClient().getConnectionManager() instanceof BasicClientConnectionManager);
assertEquals(jestClient.getServers().size(), 1);
assertTrue(jestClient.getServers().contains("http://localhost:9200"));
}

@Test
public void clientCreationWithNullClientConfig() {
JestHttpClient jestClient = (JestHttpClient) factory.getObject();
Expand Down

0 comments on commit d62daa0

Please sign in to comment.