123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.vcarecity.test.module;
-
- import com.google.inject.AbstractModule;
- import com.google.inject.Inject;
- import com.google.inject.Provides;
- import com.google.inject.Singleton;
- import com.vcarecity.test.properties.ConfigProperties;
- import com.vcarecity.test.properties.DbProperties;
- import com.vcarecity.test.properties.ElasticProperties;
- import com.zaxxer.hikari.HikariConfig;
- import com.zaxxer.hikari.HikariDataSource;
- import org.apache.http.HttpHost;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.Credentials;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.CredentialsProvider;
- import org.apache.http.impl.client.BasicCredentialsProvider;
- import org.elasticsearch.client.RestClient;
- import org.elasticsearch.client.RestClientBuilder;
- import org.elasticsearch.client.RestHighLevelClient;
-
- import javax.sql.DataSource;
- import java.io.IOException;
-
- /**
- * @author Kerry on 19/12/09
- */
-
- public class DbModule extends AbstractModule {
- @Override
- protected void configure() {
- // bind(DataSource.class).to(HikariDataSource.class);
- }
-
- @Provides
- @Inject
- @Singleton
- public DataSource dataSource(ConfigProperties configProperties) {
-
- final DbProperties database = configProperties.getDatabase();
- HikariConfig config = new HikariConfig();
- config.setJdbcUrl(database.getUrl());
- config.setUsername(database.getUser());
- config.setPassword(database.getPassword());
- //config.addDataSourceProperty("cachePrepStmts", "true");
- //config.addDataSourceProperty("prepStmtCacheSize", "250");
- //config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
- //config.addDataSourceProperty("useServerPrepStmts", "true");
- //config.addDataSourceProperty("useLocalSessionState", "true");
- //config.addDataSourceProperty("rewriteBatchedStatements", "true");
- //config.addDataSourceProperty("cacheResultSetMetadata", "true");
- //config.addDataSourceProperty("cacheServerConfiguration", "true");
- //config.addDataSourceProperty("elideSetAutoCommits", "true");
- //config.addDataSourceProperty("maintainTimeStats", "false");
-
- config.setMaximumPoolSize(2);
- config.setMinimumIdle(1);
- final HikariDataSource hikariDataSource = new HikariDataSource(config);
- Runtime.getRuntime().addShutdownHook(new Thread(hikariDataSource::close));
- return hikariDataSource;
- }
-
- @Provides
- @Inject
- @Singleton
- public RestHighLevelClient restHighLevelClient(ConfigProperties configProperties) {
- final ElasticProperties elastic = configProperties.getElasticsearch();
- final String[] split = elastic.getHost().split(",");
-
- HttpHost[] hosts = new HttpHost[split.length];
- for (int i = 0; i < hosts.length; i++) {
- final String[] address = split[i].split(":");
- if (address.length > 1) {
- hosts[i] = new HttpHost(address[0], Integer.parseInt(address[1]));
- } else {
- hosts[i] = new HttpHost(address[0], 9200);
- }
- }
-
- final RestClientBuilder builder = RestClient.builder(hosts);
-
- CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- Credentials credentials = new UsernamePasswordCredentials(elastic.getUser(), elastic.getPassword());
- credentialsProvider.setCredentials(AuthScope.ANY, credentials);
-
- builder.setHttpClientConfigCallback(http -> {
- http.setDefaultCredentialsProvider(credentialsProvider);
- return http;
- });
-
- final RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
- try {
- restHighLevelClient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }));
- return restHighLevelClient;
- }
-
-
- }
|