Skip to content

Commit c6b636d

Browse files
committed
Add additional examples
dnsjava#217
1 parent bbe9e85 commit c6b636d

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

EXAMPLES.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,33 @@ import org.xbill.DNS.*;
1313
InetAddress addr = Address.getByName("www.dnsjava.org");
1414
```
1515

16-
## Get the MX target and preference of a name
16+
## Get the MX target and preference of a name (modern)
17+
```java
18+
Resolver r = new SimpleResolver("8.8.8.8");
19+
LookupSession s = LookupSession.builder().resolver(r).build();
20+
Name mxLookup = Name.fromString("gmail.com.");
21+
s.lookupAsync(mxLookup, Type.MX)
22+
.whenComplete(
23+
(answers, ex) -> {
24+
if (ex == null) {
25+
if (answers.getRecords().isEmpty()) {
26+
System.out.println(mxLookup + " has no MX");
27+
} else {
28+
for (Record rec : answers.getRecords()) {
29+
MXRecord mx = ((MXRecord) rec);
30+
System.out.println(
31+
"Host " + mx.getTarget() + " has preference " + mx.getPriority());
32+
}
33+
}
34+
} else {
35+
ex.printStackTrace();
36+
}
37+
})
38+
.toCompletableFuture()
39+
.get();
40+
```
41+
42+
## Get the MX target and preference of a name (legacy)
1743

1844
```java
1945
Record[] records = new Lookup("gmail.com", Type.MX).run();
@@ -23,6 +49,24 @@ for (int i = 0; i < records.length; i++) {
2349
}
2450
```
2551

52+
## Simple lookup with a Resolver
53+
```java
54+
Record queryRecord = Record.newRecord(Name.fromString("dnsjava.org."), Type.A, DClass.IN);
55+
Message queryMessage = Message.newQuery(queryRecord);
56+
Resolver r = new SimpleResolver("8.8.8.8");
57+
r.sendAsync(queryMessage)
58+
.whenComplete(
59+
(answer, ex) -> {
60+
if (ex == null) {
61+
System.out.println(answer);
62+
} else {
63+
ex.printStackTrace();
64+
}
65+
})
66+
.toCompletableFuture()
67+
.get();
68+
```
69+
2670
## Query a remote name server for its version
2771

2872
```java

0 commit comments

Comments
 (0)