com.jslsolucoes.elasticsearch.ee.ElasticSearchCreator Maven / Gradle / Ivy
package com.jslsolucoes.elasticsearch.ee;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import org.apache.http.HttpHost;
import com.jslsolucoes.properties.ee.ApplicationProperties;
import com.jslsolucoes.properties.ee.ApplicationProperty;
public class ElasticSearchCreator {
@Inject
@ApplicationProperties(name = "elasticsearch.properties", defaults = {
@ApplicationProperty(key = "elasticsearch.endpoints", value = "http://localhost:9200") })
private Properties properties;
@Produces
@ApplicationScoped
public ElasticSearch getInstance() {
ElasticSearch elasticSearch = new ElasticSearch();
elasticSearch.setHosts(hosts(properties.getProperty("elasticsearch.endpoints")));
return elasticSearch;
}
private HttpHost[] hosts(String endpoints) {
List hosts = new ArrayList<>();
StringTokenizer stringTokenizer = new StringTokenizer(endpoints, ",");
while (stringTokenizer.hasMoreTokens()) {
try {
URL endpoint = new URL(stringTokenizer.nextToken());
hosts.add(new HttpHost(endpoint.getHost(), endpoint.getPort(), endpoint.getProtocol()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return hosts.toArray(new HttpHost[] {});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy