ESClientMgr.java 3.14 KB
package com.yoho.search.service.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.yoho.search.base.utils.Configuration;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.IElasticsearchClient;
import com.yoho.search.core.es.IYohoIndexClientFactory;
import com.yoho.search.core.es.impl.YohoIndexClientFactoryImpl;

/**
 * Created by ginozhang on 2016/11/2.
 */
@Component
@Lazy(value=false)
public class ESClientMgr {

    private volatile Map<String, IElasticsearchClient> nameToClientMap = new ConcurrentHashMap<String, IElasticsearchClient>(50);
    
    private static final Logger logger = LoggerFactory.getLogger(ESClientMgr.class);
    
    @PostConstruct
    void init(){
    	
    	Configuration.getConfiguration().initConfig("configuration.xml");
    	
    	List<String> indexNames = new ArrayList<String>();
    	
    	indexNames.add(ISearchConstants.INDEX_NAME_PRODUCT_COLOR);
    	indexNames.add(ISearchConstants.INDEX_NAME_PRODUCT_SORT);
    	indexNames.add(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
    	indexNames.add(ISearchConstants.INDEX_NAME_SUGGEST);
    	indexNames.add(ISearchConstants.INDEX_NAME_CONVERSION);
    	indexNames.add(ISearchConstants.INDEX_NAME_STYLE);
    	indexNames.add(ISearchConstants.INDEX_NAME_STANDARD);
    	indexNames.add(ISearchConstants.INDEX_NAME_BRAND);
    	indexNames.add(ISearchConstants.INDEX_NAME_SIZE);
    	indexNames.add(ISearchConstants.INDEX_NAME_TBLPRODUCT);
    	
    	indexNames.add(ISearchConstants.INDEX_NAME_PRODUCT_PRICE_PLAN);
    	indexNames.add(ISearchConstants.INDEX_NAME_ROBOTQUESTION);
    	indexNames.add(ISearchConstants.INDEX_NAME_HELPER);
    	indexNames.add(ISearchConstants.INDEX_NAME_SHOPS);
    	indexNames.add(ISearchConstants.INDEX_NAME_PROMOTION);
    	indexNames.add(ISearchConstants.INDEX_NAME_IMAGE_VECTORS);
    	indexNames.add(ISearchConstants.INDEX_NAME_IMAGE_VECTORS);
    	indexNames.add(ISearchConstants.INDEX_NAME_PROMOTIONINDEX);
    	
    	new Thread(new Runnable() {
			@Override
			public void run() {
				for (String indexName : indexNames) {
					getClient(indexName);
				}
			}
		}).start();
    }
    
    
    public IElasticsearchClient getClient(String indexName) {
        return nameToClientMap.computeIfAbsent(indexName, (name) -> {
            IYohoIndexClientFactory factory = new YohoIndexClientFactoryImpl();
            String clusterName = Configuration.getString("search.es.cluster.name");
            String servers = Configuration.getString("search.es.servers");
            Assert.notNull(clusterName, "ClusterName for ES cannot be null.");
            Assert.notNull(servers, "Servers for ES cannot be null.");
            logger.warn("begin to createIndexClient, clusterName is [{}],servers is [{}],name is[{}]",clusterName,servers,name);
            return factory.createIndexClient(clusterName, servers, name);
        });
    }
}