1. 程式人生 > >使用java建立ES(ElasticSearch)連線池

使用java建立ES(ElasticSearch)連線池

1.首先要有一個建立連線的工廠類

package com.aly.util;


import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * EliasticSearch連線池工廠物件
 * @author 00000
 *
 */
public class EsClientPoolFactory implements PooledObjectFactory<RestHighLevelClient>{

	@Override
	public void activateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
		System.out.println("activateObject");
		
	}
	
	/**
	 * 銷燬物件
	 */
	@Override
	public void destroyObject(PooledObject<RestHighLevelClient> pooledObject) throws Exception {
		RestHighLevelClient highLevelClient = pooledObject.getObject();
		highLevelClient.close();
	}
	
	/**
	 * 生產物件
	 */
//	@SuppressWarnings({ "resource" })
	@Override
	public PooledObject<RestHighLevelClient> makeObject() throws Exception {
//		Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
		RestHighLevelClient client = null;
		try {
			/*client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));*/
			client = new RestHighLevelClient(RestClient.builder(
					new HttpHost("192.168.1.121", 9200, "http"), new HttpHost("192.168.1.122", 9200, "http"),
					new HttpHost("192.168.1.123", 9200, "http"), new HttpHost("192.168.1.125", 9200, "http"),
					new HttpHost("192.168.1.126", 9200, "http"), new HttpHost("192.168.1.127", 9200, "http")));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DefaultPooledObject<RestHighLevelClient>(client);
	}

	@Override
	public void passivateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
		System.out.println("passivateObject");
	}

	@Override
	public boolean validateObject(PooledObject<RestHighLevelClient> arg0) {
		return true;
	}
	
	
}

2.然後再寫我們的連線池工具類

package com.aly.util;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * ElasticSearch 連線池工具類
 * 
 * @author 00000
 *
 */
public class ElasticSearchPoolUtil {
	// 物件池配置類,不寫也可以,採用預設配置
	private static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
	// 採用預設配置maxTotal是8,池中有8個client
	{
		poolConfig.setMaxTotal(8);
	}
	// 要池化的物件的工廠類,這個是我們要實現的類
	private static EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory();
	// 利用物件工廠類和配置類生成物件池
	private static GenericObjectPool<RestHighLevelClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,
			poolConfig);

	/**
	 * 獲得物件
	 * 
	 * @return
	 * @throws Exception
	 */
	public static RestHighLevelClient getClient() throws Exception {
		// 從池中取一個物件
		RestHighLevelClient client = clientPool.borrowObject();
		return client;
	}

	/**
	 * 歸還物件
	 * 
	 * @param client
	 */
	public static void returnClient(RestHighLevelClient client) {
		// 使用完畢之後,歸還物件
		clientPool.returnObject(client);
	}

}