跨Portlet的Elastic Search Singleton Transport客户端

时间:2017-03-03 12:17:26

标签: java elasticsearch liferay liferay-6

您好我正在尝试为弹性搜索传输客户端创建单例实例。如果事务发生在portlet本身内,则客户端的实例是单例,但是当我在另一个portlet上移植/访问客户端时,它创建了一个新实例。有没有办法我可以实现单例传输客户端甚至跨portlet。

以下是客户端类的片段。

private ESTransportClient(){
    // private constructor
}

private static TransportClient _client = null;

public static TransportClient getClient(){
    if(_client==null){
        LOG.trace("ESTransportClient is null...............");
        synchronized(ESTransportClient.class){
            LOG.trace("Creating new instance for client..........");
            String elasticSearchClusterName = "myclustername";
            String elasticSearchIP = "myIP";
            int elasticSearchPort = 9300;
            String elasticUserName = "username";
            String elasticPassword = "password";
            boolean isAuthEnabled = true;

            Builder builder = Settings.builder()
                      .put("cluster.name", elasticSearchClusterName)
                      .put("client.transport.sniff", true);


            if(isAuthEnabled){
                builder.put("shield.user", elasticUserName+":"+elasticPassword);            
            }

            Settings settings = builder.build();                

            _client = TransportClient.builder()
                    .addPlugin(ShieldPlugin.class)
                    .settings(settings)
                    .build(); 

            InetSocketTransportAddress address = new InetSocketTransportAddress(new InetSocketAddress(elasticSearchIP, elasticSearchPort));
            _client.addTransportAddress(address); 
        }
    }else{
        LOG.trace("ESTransportClient not null. Returning the existing instance of client....");
    }
    return _client;
}

提前致谢。

0 个答案:

没有答案