RestHighLevelClient工厂方法
虽然最终被弃用,但是记录下留个纪念
ElasticSearchPoolUtil
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* ElasticSearch 连接池工具类
*/
@Slf4j
@Component
public class ElasticSearchPoolUtil {
@Autowired
ElasticsearchClientPoolFactory esClientPoolFactory;
private static GenericObjectPool<RestHighLevelClient> clientPool;
@PostConstruct
public void init() {
// 对象池配置类,不写也可以,采用默认配置
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
// 利用对象工厂类和配置类生成对象池
clientPool = new GenericObjectPool<>(esClientPoolFactory, poolConfig);
clientPool.setMaxTotal(10);
}
/**
* 获得对象
*
* @return
* @throws Exception
*/
public static RestHighLevelClient getClient()