在elasticsearch中创建TransportClient时限制ThreadPool中的线程数

时间:2015-01-14 01:22:09

标签: java multithreading elasticsearch

我在elasticsearch中创建了一个TransportClient实例。下面是相同的代码。问题是我试图降低使用TransportClient启动的线程池产生的线程数。但是我使用弹性搜索的设置总是用12个线程初始化线程池。请让我知道如何配置它以获得理想的线程。

public static TransportClient getTransportClient(String ip, int port) {

    ImmutableSettings.Builder settings = ImmutableSettings
            .settingsBuilder();
    settings.put("cluster.name", "elasticsearch");
    settings.put("threadpool.bulk.type",  "fixed");
    settings.put("threadpool.bulk.size" ,5);
    settings.put("threadpool.bulk.queue_size", 5);
    settings.put("threadpool.index.type" , "fixed");
    settings.put("threadpool.index.size" , 5);
    settings.put("threadpool.index.queue_size" , 10);
    settings.put("threadpool.search.type",  "fixed");
    settings.put("threadpool.search.size" ,5);
    settings.put("threadpool.search.queue_size", 5);

    settings.build();

    TransportClient instance = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(ip, port));

    return instance;
}

1 个答案:

答案 0 :(得分:1)

尝试

Settings settings = ImmutableSettings.settingsBuilder()
                    .put("transport.netty.workerCount",NUM_THREADS)
                    .build();

归功于JanuZ,取自http://www.lucidelectricdreams.com/2013/11/reducing-number-of-threads-created-by.html