forked from spotify/dns-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPollingUsage.java
104 lines (85 loc) · 3.1 KB
/
PollingUsage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2015 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.spotify.dns.examples;
import com.google.common.collect.Sets;
import com.spotify.dns.ChangeNotifier;
import com.spotify.dns.DnsException;
import com.spotify.dns.DnsSrvResolver;
import com.spotify.dns.DnsSrvResolvers;
import com.spotify.dns.DnsSrvWatcher;
import com.spotify.dns.DnsSrvWatchers;
import com.spotify.dns.ErrorHandler;
import com.spotify.dns.LookupResult;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
public final class PollingUsage {
public static void main(String[] args) throws IOException {
DnsSrvResolver resolver = DnsSrvResolvers.newBuilder()
.cachingLookups(true)
.dnsLookupTimeoutMillis(1000)
.build();
DnsSrvWatcher<LookupResult> watcher = DnsSrvWatchers.newBuilder(resolver)
.polling(1, TimeUnit.SECONDS)
.withErrorHandler(new ErrorPrinter())
.build();
boolean quit = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (!quit) {
System.out.print("Enter a SRV name (empty to quit): ");
String line = in.readLine();
if (line == null || line.isEmpty()) {
quit = true;
} else {
try {
ChangeNotifier<LookupResult> notifier = watcher.watch(line);
notifier.setListener(new ChangeListener(line), false);
} catch (DnsException e) {
e.printStackTrace(System.out);
}
}
}
}
static class ErrorPrinter implements ErrorHandler {
@Override
public void handle(String fqdn, DnsException exception) {
System.out.println("Error with " + fqdn);
exception.printStackTrace();
}
}
static class ChangeListener implements ChangeNotifier.Listener<LookupResult> {
final String name;
ChangeListener(String name) {
this.name = name;
}
@Override
public void onChange(ChangeNotifier.ChangeNotification<LookupResult> changeNotification) {
System.out.println("\nEndpoints changed for " + name);
for (LookupResult endpoint : changeNotification.previous()) {
System.out.println(" prev: " + endpoint);
}
for (LookupResult endpoint : changeNotification.current()) {
System.out.println(" curr: " + endpoint);
}
final Sets.SetView<LookupResult> unchanged =
Sets.intersection(changeNotification.current(), changeNotification.previous());
for (LookupResult endpoint : unchanged) {
System.out.println(" noch: " + endpoint);
}
}
}
}