2018-07-11 15:47:36 +02:00
|
|
|
package eu.eudat.configurations;
|
|
|
|
|
|
|
|
import org.apache.http.HttpHost;
|
2020-03-09 15:47:32 +01:00
|
|
|
import org.apache.http.auth.AuthScope;
|
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
|
import org.apache.http.client.CredentialsProvider;
|
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
2021-06-25 11:32:58 +02:00
|
|
|
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
|
|
|
|
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
|
|
|
|
import org.apache.http.nio.reactor.IOReactorException;
|
|
|
|
import org.apache.http.nio.reactor.IOReactorExceptionHandler;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
2021-06-25 11:32:58 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-07-11 15:47:36 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
2021-06-25 11:32:58 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2018-07-11 15:47:36 +02:00
|
|
|
/**
|
|
|
|
* Created by ikalyvas on 7/5/2018.
|
|
|
|
*/
|
|
|
|
@Configuration
|
|
|
|
public class ElasticSearchConfiguration {
|
2021-06-25 11:32:58 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfiguration.class);
|
2018-07-11 15:47:36 +02:00
|
|
|
|
|
|
|
private Environment environment;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public ElasticSearchConfiguration(Environment environment) {
|
|
|
|
this.environment = environment;
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:47:32 +01:00
|
|
|
@Bean(destroyMethod = "close")
|
2018-07-11 15:47:36 +02:00
|
|
|
public RestHighLevelClient client() throws Exception {
|
2020-03-09 15:47:32 +01:00
|
|
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
|
credentialsProvider.setCredentials(AuthScope.ANY,
|
|
|
|
new UsernamePasswordCredentials(this.environment.getProperty("elasticsearch.username"), this.environment.getProperty("elasticsearch.password")));
|
|
|
|
|
2021-06-25 11:32:58 +02:00
|
|
|
try {
|
|
|
|
DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
|
|
|
|
ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {
|
|
|
|
@Override
|
|
|
|
public boolean handle(IOException e) {
|
|
|
|
logger.warn("System may be unstable: IOReactor encountered a checked exception : " + e.getMessage(), e);
|
|
|
|
return true; // Return true to note this exception as handled, it will not be re-thrown
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handle(RuntimeException e) {
|
|
|
|
logger.warn("System may be unstable: IOReactor encountered a runtime exception : " + e.getMessage(), e);
|
|
|
|
return true; // Return true to note this exception as handled, it will not be re-thrown
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
RestHighLevelClient client = new RestHighLevelClient(
|
|
|
|
RestClient.builder(
|
|
|
|
new HttpHost(this.environment.getProperty("elasticsearch.host"),
|
|
|
|
Integer.parseInt(this.environment.getProperty("elasticsearch.port")), "http"))
|
|
|
|
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
|
|
|
|
.setDefaultCredentialsProvider(credentialsProvider).setConnectionManager(new PoolingNHttpClientConnectionManager(ioReactor))));
|
|
|
|
return client;
|
|
|
|
}catch (IOReactorException ex) {
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
2018-07-11 15:47:36 +02:00
|
|
|
}
|
|
|
|
}
|