查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.resolver.dns;
18  
19  import io.netty.channel.ChannelFactory;
20  import io.netty.channel.EventLoop;
21  import io.netty.channel.socket.DatagramChannel;
22  import io.netty.resolver.AddressResolver;
23  import io.netty.resolver.AddressResolverGroup;
24  import io.netty.resolver.NameResolver;
25  import io.netty.resolver.RoundRobinInetAddressResolver;
26  
27  import java.net.InetAddress;
28  import java.net.InetSocketAddress;
29  
30  /**
31   * A {@link AddressResolverGroup} of {@link DnsNameResolver}s that supports random selection of destination addresses if
32   * multiple are provided by the nameserver. This is ideal for use in applications that use a pool of connections, for
33   * which connecting to a single resolved address would be inefficient.
34   */
35  public class RoundRobinDnsAddressResolverGroup extends DnsAddressResolverGroup {
36  
37      public RoundRobinDnsAddressResolverGroup(DnsNameResolverBuilder dnsResolverBuilder) {
38          super(dnsResolverBuilder);
39      }
40  
41      public RoundRobinDnsAddressResolverGroup(
42              Class<? extends DatagramChannel> channelType,
43              DnsServerAddressStreamProvider nameServerProvider) {
44          super(channelType, nameServerProvider);
45      }
46  
47      public RoundRobinDnsAddressResolverGroup(
48              ChannelFactory<? extends DatagramChannel> channelFactory,
49              DnsServerAddressStreamProvider nameServerProvider) {
50          super(channelFactory, nameServerProvider);
51      }
52  
53      /**
54       * We need to override this method, not
55       * {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)},
56       * because we need to eliminate possible caching of {@link io.netty.resolver.NameResolver#resolve}
57       * by {@link InflightNameResolver} created in
58       * {@link #newResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)}.
59       */
60      @Override
61      protected final AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
62                                                                            NameResolver<InetAddress> resolver)
63              throws Exception {
64          return new RoundRobinInetAddressResolver(eventLoop, resolver).asAddressResolver();
65      }
66  }