Project

General

Profile

1
package eu.dnetlib.data.index;
2

    
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
import org.apache.solr.client.solrj.impl.CloudSolrClient;
6
import org.apache.solr.client.solrj.response.SolrPingResponse;
7

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

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

    
15
	public static CloudIndexClient newIndexClient(final String baseURL, final String collection, final boolean parallelUpdates)
16
			throws CloudIndexClientException {
17
		try {
18
			final CloudSolrClient client = new CloudSolrClient.Builder().withZkHost(baseURL).build();
19

    
20
			log.info(String.format("Initializing solr server (%s) ...", baseURL));
21

    
22
			client.connect();
23

    
24
			client.setParallelUpdates(parallelUpdates);
25
			client.setDefaultCollection(collection);
26

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

    
39
}
(3-3/3)