Move DnsNameResolver creation into try-catch block.

This commit is contained in:
Steveice10 2020-11-21 12:59:51 -08:00
parent bb12069f95
commit ead6afae39

View file

@ -146,11 +146,14 @@ public class TcpClientSession extends TcpSession {
System.out.println("[PacketLib] Attempting SRV lookup for \"" + name + "\"."); System.out.println("[PacketLib] Attempting SRV lookup for \"" + name + "\".");
} }
DnsNameResolver resolver = null;
AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = null; AddressedEnvelope<DnsResponse, InetSocketAddress> envelope = null;
try(DnsNameResolver resolver = new DnsNameResolverBuilder(this.group.next()) try {
.channelType(NioDatagramChannel.class) resolver = new DnsNameResolverBuilder(this.group.next())
.build()) { .channelType(NioDatagramChannel.class)
.build();
envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get(); envelope = resolver.query(new DefaultDnsQuestion(name, DnsRecordType.SRV)).get();
DnsResponse response = envelope.content(); DnsResponse response = envelope.content();
if(response.count(DnsSection.ANSWER) > 0) { if(response.count(DnsSection.ANSWER) > 0) {
DefaultDnsRawRecord record = response.recordAt(DnsSection.ANSWER, 0); DefaultDnsRawRecord record = response.recordAt(DnsSection.ANSWER, 0);
@ -185,6 +188,10 @@ public class TcpClientSession extends TcpSession {
if(envelope != null) { if(envelope != null) {
envelope.release(); envelope.release();
} }
if(resolver != null) {
resolver.close();
}
} }
} }