33
33
import org .slf4j .LoggerFactory ;
34
34
35
35
import java .util .*;
36
+ import java .util .concurrent .Executors ;
37
+ import java .util .concurrent .ScheduledExecutorService ;
38
+ import java .util .concurrent .ScheduledFuture ;
39
+ import java .util .concurrent .TimeUnit ;
36
40
37
41
import static com .arangodb .internal .serde .SerdeUtils .constructParametricType ;
38
42
@@ -48,10 +52,10 @@ public class ExtendedHostResolver implements HostResolver {
48
52
private final ArangoConfig config ;
49
53
private final ConnectionFactory connectionFactory ;
50
54
private final Integer acquireHostListInterval ;
51
- private long lastUpdate ;
55
+ private final ScheduledExecutorService scheduler ;
52
56
private ArangoExecutorSync executor ;
53
57
private InternalSerde arangoSerialization ;
54
-
58
+ private ScheduledFuture <?> schedule ;
55
59
56
60
public ExtendedHostResolver (final List <Host > hosts , final ArangoConfig config ,
57
61
final ConnectionFactory connectionFactory , Integer acquireHostListInterval ) {
@@ -61,22 +65,34 @@ public ExtendedHostResolver(final List<Host> hosts, final ArangoConfig config,
61
65
this .config = config ;
62
66
this .connectionFactory = connectionFactory ;
63
67
64
- lastUpdate = 0 ;
68
+ scheduler = Executors .newSingleThreadScheduledExecutor (r -> {
69
+ Thread t = Executors .defaultThreadFactory ().newThread (r );
70
+ t .setDaemon (true );
71
+ return t ;
72
+ }
73
+ );
65
74
}
66
75
67
76
@ Override
68
77
public void init (ArangoExecutorSync executor , InternalSerde arangoSerialization ) {
69
78
this .executor = executor ;
70
79
this .arangoSerialization = arangoSerialization ;
80
+ resolve ();
81
+ schedule = scheduler .scheduleAtFixedRate (this ::resolve , acquireHostListInterval , acquireHostListInterval , TimeUnit .MILLISECONDS );
71
82
}
72
83
73
84
@ Override
74
- public HostSet resolve (boolean initial , boolean closeConnections ) {
75
-
76
- if (!initial && isExpired ()) {
85
+ public void close () {
86
+ schedule .cancel (false );
87
+ scheduler .shutdown ();
88
+ }
77
89
78
- lastUpdate = System .currentTimeMillis ();
90
+ @ Override
91
+ public HostSet getHosts () {
92
+ return hosts ;
93
+ }
79
94
95
+ private void resolve () {
80
96
final Collection <String > endpoints = resolveFromServer ();
81
97
if (LOGGER .isDebugEnabled ()) {
82
98
LOGGER .debug ("Resolve {} Endpoints" , endpoints .size ());
@@ -110,17 +126,11 @@ public HostSet resolve(boolean initial, boolean closeConnections) {
110
126
}
111
127
}
112
128
hosts .clearAllMarkedForDeletion ();
113
- }
114
-
115
- return hosts ;
116
129
}
117
130
118
131
private Collection <String > resolveFromServer () {
119
-
120
132
Collection <String > response ;
121
-
122
133
try {
123
-
124
134
response = executor .execute (
125
135
new InternalRequest (ArangoRequestParam .SYSTEM , RequestType .GET , "/_api/cluster/endpoints" ),
126
136
response1 -> {
@@ -136,7 +146,6 @@ private Collection<String> resolveFromServer() {
136
146
}, null );
137
147
} catch (final ArangoDBException e ) {
138
148
final Integer responseCode = e .getResponseCode ();
139
-
140
149
// responseCode == 403: single server < 3.7
141
150
// responseCode == 501: single server >= 3.7
142
151
if (responseCode != null && (responseCode == 403 || responseCode == 501 )) {
@@ -145,12 +154,6 @@ private Collection<String> resolveFromServer() {
145
154
throw e ;
146
155
}
147
156
}
148
-
149
157
return response ;
150
158
}
151
-
152
- private boolean isExpired () {
153
- return System .currentTimeMillis () > (lastUpdate + acquireHostListInterval );
154
- }
155
-
156
159
}
0 commit comments