1818
1919import java .io .IOException ;
2020import java .io .InputStream ;
21+ import java .util .ArrayList ;
22+ import java .util .List ;
2123import java .util .Properties ;
2224
2325import 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