我正在使用v 1.3.2上的传输客户端
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("1.1.1.1", 9300));
我用以下方法关闭方法:
client.close();
一切似乎都没问题,除了我在“" new TransportClient(设置)"”字样下面收到一个令人讨厌的黄色警告。在Eclipse中说:
Resource leak: '<unassigned Closeable value>' is never closed
看起来像这样:
所以我的问题是,即使我在方法结束时关闭客户端,新的TransportClient仍然会发生泄漏。或者我应该在eclipse中忽略该警告?
Per @Lucas回复现在看起来像这样:
答案 0 :(得分:4)
为什么不尝试使用try-with-resources
:
try(Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("1.1.1.1", 9300));) {
//your code
} catch (//your exceptions if any) {
//handle exceptions
}
当你完成它们后它会自动关闭资源,并且应该让Eclipse安静下来
答案 1 :(得分:2)
我发现分离TransportClient和addTransportAddress的创建会停止警告:
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
TransportClient client = new TransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress("1.1.1.1", 9300));
祝你好运!