Project

General

Profile

1
package eu.dnetlib.data.index;
2

    
3
import eu.dnetlib.functionality.index.utils.ZkServers;
4
import org.apache.commons.logging.Log;
5
import org.apache.commons.logging.LogFactory;
6
import org.apache.solr.client.solrj.impl.CloudSolrClient;
7
import org.apache.solr.client.solrj.response.SolrPingResponse;
8

    
9
/**
10
 * Created by michele on 11/11/15.
11
 */
12
public class CloudIndexClientFactory {
13

    
14
	private static final Log log = LogFactory.getLog(CloudIndexClientFactory.class);
15

    
16
	public static CloudIndexClient newIndexClient(final String baseURL, final String collection, final boolean parallelUpdates)
17
			throws CloudIndexClientException {
18
		try {
19
			log.info(String.format("Initializing solr server (%s) ...", baseURL));
20

    
21
			final ZkServers zk = ZkServers.newInstance(baseURL);
22
			final CloudSolrClient client = new CloudSolrClient.Builder(zk.getHosts(), zk.getChroot())
23
					.withParallelUpdates(parallelUpdates)
24
					.build();
25

    
26
			client.connect();
27
			client.setDefaultCollection(collection);
28

    
29
			final SolrPingResponse rsp = client.ping();
30
			if (rsp.getStatus() != 0) {
31
				log.error("Invalid connection to solr Server (status = 0)");
32
				throw new CloudIndexClientException("Invalid connection to solr Server (status = 0)");
33
			}
34
			return new CloudIndexClient(client);
35
		} catch (Throwable e) {
36
			log.error("The initialization of indexClient is FAILED", e);
37
			throw new CloudIndexClientException("The initialization of indexClient is FAILED", e);
38
		}
39
	}
40

    
41
}
(3-3/3)