1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package com.example.xiaoshiweixinback.business.config;
- import okhttp3.Dns;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.concurrent.Callable;
- import java.util.concurrent.FutureTask;
- import java.util.concurrent.TimeUnit;
- public class XDns implements Dns {
- private long timeout;
- public XDns(long timeout) {
- this.timeout = timeout;
- }
- @Override
- public List<InetAddress> lookup(final String hostname) throws UnknownHostException {
- if (hostname == null) {
- throw new UnknownHostException("hostname == null");
- } else {
- List<InetAddress> inetAddresses =new ArrayList<>();
- Boolean falg =true;
- while (falg){
- try {
- FutureTask<List<InetAddress>> task = new FutureTask<>(
- new Callable<List<InetAddress>>() {
- @Override
- public List<InetAddress> call() throws Exception {
- return Arrays.asList(InetAddress.getAllByName(hostname));
- }
- });
- new Thread(task).start();
- inetAddresses=task.get(timeout, TimeUnit.MILLISECONDS);
- falg =false;
- } catch (Exception var4) {
- continue;
- }
- }
- return inetAddresses;
- }
- }
- }
|