Skip to content

Commit 65576e3

Browse files
author
a-brandt
committed
added auto reconnection when connection breaks
added fallback servers
1 parent f273d43 commit 65576e3

31 files changed

+2363
-2179
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,20 @@ The driver is configured with some default values:
7373
<tr><th>enableCURLLogger</th><td>logging flag by curl format for debug</td><td>false</td></tr>
7474
</table>
7575

76+
Since 2.5.4 you can configure a default and a fallback database:
77+
78+
<table>
79+
<tr><th>property-key</th><th>description</th><th>default value</th></tr>
80+
<tr><th>arangoHost</th><td>ArangoDB host and port </td><td>127.0.0.1:8529</td></tr>
81+
<tr><th>fallbackArangoHost</th><td>fallback ArangoDB host and port </td><td></td></tr>
82+
</table>
7683

7784
To customize the configuration the parameters can be changed in the code...
7885

7986
``` Java
8087
// Initialize configure
8188
ArangoConfigure configure = new ArangoConfigure();
82-
configure.setHost("192.168.182.50");
83-
configure.setPort(8888);
89+
configure.setArangoHost(new ArangoHost("192.168.182.50", 8888));
8490
configure.init();
8591

8692
// Create Driver (this instance is thread-safe)
@@ -102,8 +108,7 @@ To customize the configuration the parameters can be changed in the code...
102108

103109
Example for arangodb.properties:
104110
``` Java
105-
port=8888
106-
host=192.168.182.50
111+
arangoHost=192.168.182.50:8888
107112
user=root
108113
password=
109114
enableCURLLogger=true
@@ -383,3 +388,10 @@ see 2.4.4
383388
## since 2.5.3
384389
* fixed issue #9
385390
* added method to driver.getTraversal(...);
391+
392+
## since 2.5.4
393+
* fixed issue #12
394+
* added auto reconnection when connection breaks
395+
* added fallback server endpoints
396+
397+

src/main/java/com/arangodb/ArangoConfigure.java

Lines changed: 181 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.IOException;
2020
import java.io.InputStream;
21+
import java.util.ArrayList;
22+
import java.util.List;
2123
import java.util.Properties;
2224

2325
import org.slf4j.Logger;
@@ -50,10 +52,9 @@ public class ArangoConfigure {
5052
/** default property file */
5153
private static final String DEFAULT_PROPERTY_FILE = "/arangodb.properties";
5254

53-
/** server port */
54-
int port;
55-
/** server host */
56-
String host;
55+
List<ArangoHost> arangoHosts;
56+
int currentArangoHost;
57+
5758
/** connection timeout(ms) */
5859
int connectionTimeout = -1;
5960
/** socket read timeout(ms) */
@@ -77,6 +78,16 @@ public class ArangoConfigure {
7778
/** http retry count */
7879
int retryCount = 3;
7980

81+
/**
82+
* number of connect retries (0 means infinite)
83+
*/
84+
int connectRetryCount = 3;
85+
86+
/**
87+
* milliseconds
88+
*/
89+
int connectRetryWait = 1000;
90+
8091
/** Default Database */
8192
String defaultDatabase;
8293

@@ -95,8 +106,11 @@ public ArangoConfigure(String propertyPath) {
95106
}
96107

97108
private void init(String propertyPath) {
98-
this.host = DEFAULT_HOST;
99-
this.port = DEFAULT_PORT;
109+
arangoHosts = new ArrayList<ArangoHost>();
110+
ArangoHost defaultHost = new ArangoHost(DEFAULT_HOST, DEFAULT_PORT);
111+
arangoHosts.add(defaultHost);
112+
currentArangoHost = 0;
113+
100114
this.maxPerConnection = DEFAULT_MAX_PER_CONNECTION;
101115
this.maxTotalConnection = DEFAULT_MAX_CONNECTION;
102116
loadProperties(propertyPath);
@@ -128,12 +142,29 @@ public void loadProperties(String propertyPath) {
128142
//
129143
String port = prop.getProperty("port");
130144
if (port != null) {
131-
setPort(Integer.parseInt(port));
145+
arangoHosts.get(0).setPort(Integer.parseInt(port));
132146
}
133147

134148
String host = prop.getProperty("host");
135149
if (host != null) {
136-
setHost(host);
150+
arangoHosts.get(0).setHost(host);
151+
}
152+
153+
String arangoHost = prop.getProperty("arangoHost");
154+
if (arangoHost != null) {
155+
ArangoHost ah = parseArangoHost(arangoHost);
156+
if (ah != null) {
157+
arangoHosts.get(0).setHost(ah.getHost());
158+
arangoHosts.get(0).setPort(ah.getPort());
159+
}
160+
}
161+
162+
String fallbackArangoHost = prop.getProperty("fallbackArangoHost");
163+
if (fallbackArangoHost != null) {
164+
ArangoHost ah = parseArangoHost(fallbackArangoHost);
165+
if (ah != null) {
166+
addFallbackArangoHost(ah);
167+
}
137168
}
138169

139170
String timeout = prop.getProperty("timeout");
@@ -171,6 +202,16 @@ public void loadProperties(String propertyPath) {
171202
setRetryCount(Integer.parseInt(retryCount));
172203
}
173204

205+
String connnectRetryCount = prop.getProperty("connnectRetryCount");
206+
if (connnectRetryCount != null) {
207+
setConnectRetryCount(Integer.parseInt(connnectRetryCount));
208+
}
209+
210+
String connectRetryWait = prop.getProperty("connectRetryWait");
211+
if (connectRetryWait != null) {
212+
setConnectRetryWait(Integer.parseInt(connectRetryWait));
213+
}
214+
174215
String user = prop.getProperty("user");
175216
if (user != null) {
176217
setUser(user);
@@ -206,6 +247,19 @@ public void loadProperties(String propertyPath) {
206247
}
207248
}
208249

250+
private ArangoHost parseArangoHost(String str) {
251+
if (str == null) {
252+
return null;
253+
}
254+
255+
String[] split = str.split(":", 2);
256+
if (split.length != 2) {
257+
return null;
258+
}
259+
260+
return new ArangoHost(split[0], Integer.parseInt(split[1]));
261+
}
262+
209263
public void init() {
210264
this.httpManager = new BatchHttpManager(this);
211265
this.httpManager.init();
@@ -218,14 +272,34 @@ public void shutdown() {
218272
}
219273
}
220274

221-
// TODO changes this for multiple host support
222275
public String getBaseUrl() {
223-
return "http://" + this.host + ":" + this.port;
276+
ArangoHost currentHost = getCurrentHost();
277+
278+
return "http://" + currentHost.getHost() + ":" + currentHost.getPort();
224279
}
225280

226-
// TODO changes this for multiple host support
227281
public String getEndpoint() {
228-
return "tcp://" + this.host + ":" + this.port;
282+
ArangoHost currentHost = getCurrentHost();
283+
284+
return "tcp://" + currentHost.getHost() + ":" + currentHost.getPort();
285+
}
286+
287+
private ArangoHost getCurrentHost() {
288+
return arangoHosts.get(currentArangoHost);
289+
}
290+
291+
public boolean hasFallbackHost() {
292+
return arangoHosts.size() > 1;
293+
}
294+
295+
public ArangoHost changeCurrentHost() {
296+
currentArangoHost++;
297+
298+
if (currentArangoHost >= arangoHosts.size()) {
299+
currentArangoHost = 0;
300+
}
301+
302+
return getCurrentHost();
229303
}
230304

231305
public static String getDefaultHost() {
@@ -241,31 +315,51 @@ public static int getDefaultMaxConnection() {
241315
}
242316

243317
/**
244-
* Don't use method. Please use {@link #getPort() getPort}
318+
* Get the server port number
319+
*
320+
* Don't use method. Please use {@link #getArangoHost() getArangoHost}
245321
*
246322
* @deprecated
323+
* @return the port number
247324
*/
248325
@Deprecated
249326
public int getClientPort() {
250-
return port;
327+
return arangoHosts.get(0).getPort();
251328
}
252329

253330
/**
254331
* Get the server port number
255332
*
333+
* Don't use method. Please use {@link #getArangoHost() getArangoHost}
334+
*
335+
* @deprecated
256336
* @return the port number
257337
*/
338+
@Deprecated
258339
public int getPort() {
259-
return port;
340+
return arangoHosts.get(0).getPort();
260341
}
261342

262343
/**
263344
* Get the database host name
264345
*
346+
* Don't use method. Please use {@link #getArangoHost() getArangoHost}
347+
*
348+
* @deprecated
265349
* @return the host name
266350
*/
351+
@Deprecated
267352
public String getHost() {
268-
return host;
353+
return arangoHosts.get(0).getHost();
354+
}
355+
356+
/**
357+
* Get the default database host name and port
358+
*
359+
* @return the host name and port
360+
*/
361+
public ArangoHost getArangoHost() {
362+
return arangoHosts.get(0);
269363
}
270364

271365
public int getConnectionTimeout() {
@@ -293,33 +387,70 @@ public int getProxyPort() {
293387
}
294388

295389
/**
296-
* Don't use this method. Please use {@link #setPort(int) setPort}
390+
* Set the port number of the database
391+
*
392+
* Don't use this method. Please use {@link #setArangoHost(ArangoHost)
393+
* setArangoHost}
297394
*
298395
* @deprecated
396+
* @param port
397+
* the port number
299398
*/
300399
@Deprecated
301-
public void setClinetPort(int clinetPort) {
302-
this.port = clinetPort;
400+
public void setClinetPort(int clientPort) {
401+
arangoHosts.get(0).setPort(clientPort);
303402
}
304403

305404
/**
306405
* Set the port number of the database
307406
*
407+
* Don't use this method. Please use {@link #setArangoHost(ArangoHost)
408+
* setArangoHost}
409+
*
410+
* @deprecated
308411
* @param port
309412
* the port number
310413
*/
414+
@Deprecated
311415
public void setPort(int port) {
312-
this.port = port;
416+
arangoHosts.get(0).setPort(port);
313417
}
314418

315419
/**
316420
* Set the host name of the database
317421
*
422+
* Don't use this method. Please use {@link #setArangoHost(ArangoHost)
423+
* setArangoHost}
424+
*
425+
* @deprecated
318426
* @param host
319427
* the host name
320428
*/
429+
@Deprecated
321430
public void setHost(String host) {
322-
this.host = host;
431+
arangoHosts.get(0).setHost(host);
432+
}
433+
434+
/**
435+
* Set the host name and port of the database
436+
*
437+
* @param arangoHost
438+
* the host name and port
439+
*/
440+
public void setArangoHost(ArangoHost arangoHost) {
441+
ArangoHost host = arangoHosts.get(0);
442+
host.setHost(arangoHost.getHost());
443+
host.setPort(arangoHost.getPort());
444+
}
445+
446+
/**
447+
* Set the host name and port of the fallback database
448+
*
449+
* @param arangoHost
450+
* the host name and port
451+
*/
452+
public void addFallbackArangoHost(ArangoHost arangoHost) {
453+
arangoHosts.add(arangoHost);
323454
}
324455

325456
public void setConnectionTimeout(int connectionTimeout) {
@@ -407,4 +538,33 @@ public boolean isStaleConnectionCheck() {
407538
public void setStaleConnectionCheck(boolean staleConnectionCheck) {
408539
this.staleConnectionCheck = staleConnectionCheck;
409540
}
541+
542+
public int getConnectRetryCount() {
543+
return connectRetryCount;
544+
}
545+
546+
/**
547+
* Set number of connect retries (0 means infinite)
548+
*
549+
* @param connectRetryCount
550+
* number of connect retries
551+
*/
552+
public void setConnectRetryCount(int connectRetryCount) {
553+
this.connectRetryCount = connectRetryCount;
554+
}
555+
556+
public int getConnectRetryWait() {
557+
return connectRetryWait;
558+
}
559+
560+
/**
561+
* Set wait time for a connect retry
562+
*
563+
* @param connectRetryWait
564+
* milliseconds to wait
565+
*/
566+
public void setConnectRetryWait(int connectRetryWait) {
567+
this.connectRetryWait = connectRetryWait;
568+
}
569+
410570
}

0 commit comments

Comments
 (0)