1515 */
1616package com .datastax .driver .core ;
1717
18+ import java .io .Closeable ;
1819import java .net .InetAddress ;
1920import java .net .UnknownHostException ;
2021import java .util .*;
5556 * the nodes currently in the cluster as well as new nodes joining the cluster
5657 * subsequently.
5758 */
58- public class Cluster {
59+ public class Cluster implements Closeable {
5960
6061 private static final Logger logger = LoggerFactory .getLogger (Cluster .class );
6162
@@ -322,24 +323,40 @@ public Cluster unregister(LatencyTracker tracker) {
322323
323324 /**
324325 * Initiates a shutdown of this cluster instance.
325- *
326+ * <p>
326327 * This method is asynchronous and return a future on the completion
327328 * of the shutdown process. As soon a the cluster is shutdown, no
328329 * new request will be accepted, but already submitted queries are
329- * allowed to complete. Shutdown closes all connections from all
330+ * allowed to complete. This method closes all connections from all
330331 * sessions and reclaims all ressources used by this Cluster
331332 * instance.
332333 * <p>
333334 * If for some reason you wish to expedite this process, the
334- * {@link ShutdownFuture #force} can be called on the result future.
335+ * {@link CloseFuture #force} can be called on the result future.
335336 * <p>
336- * This method has no particular effect if the cluster was already shut
337- * down (in which case the returned future will return immediately).
337+ * This method has no particular effect if the cluster was already closed
338+ * (in which case the returned future will return immediately).
338339 *
339- * @return a future on the completion of the shtudown process.
340+ * @return a future on the completion of the shutdown process.
341+ */
342+ public CloseFuture closeAsync () {
343+ return manager .close ();
344+ }
345+
346+ /**
347+ * Initiates a shutdown of this cluster instance and blocks until
348+ * that shutdown completes.
349+ * <p>
350+ * This method is a shortcut for {@code closeAsync().get()}.
340351 */
341- public ShutdownFuture shutdown () {
342- return manager .shutdown ();
352+ public void close () {
353+ try {
354+ closeAsync ().get ();
355+ } catch (ExecutionException e ) {
356+ throw DefaultResultSetFuture .extractCauseFromExecutionException (e );
357+ } catch (InterruptedException e ) {
358+ Thread .currentThread ().interrupt ();
359+ }
343360 }
344361
345362 /**
@@ -842,7 +859,7 @@ class Manager implements Host.StateListener, Connection.DefaultResponseHandler {
842859 // An executor for tasks that migth block some time, like creating new connection, but are generally not too critical.
843860 final ListeningExecutorService blockingTasksExecutor ;
844861
845- final AtomicReference <ShutdownFuture > shutdownFuture = new AtomicReference <ShutdownFuture >();
862+ final AtomicReference <CloseFuture > closeFuture = new AtomicReference <CloseFuture >();
846863
847864 // All the queries that have been prepared (we keep them so we can re-prepared them when a node fail or a
848865 // new one join the cluster).
@@ -899,7 +916,7 @@ private void init() {
899916 try {
900917 controlConnection .connect ();
901918 } catch (NoHostAvailableException e ) {
902- shutdown ();
919+ close ();
903920 throw e ;
904921 }
905922 }
@@ -930,13 +947,13 @@ void reportLatency(Host host, long latencyNanos) {
930947 }
931948 }
932949
933- boolean isShutdown () {
934- return shutdownFuture .get () != null ;
950+ boolean isClosed () {
951+ return closeFuture .get () != null ;
935952 }
936953
937- private ShutdownFuture shutdown () {
954+ private CloseFuture close () {
938955
939- ShutdownFuture future = shutdownFuture .get ();
956+ CloseFuture future = closeFuture .get ();
940957 if (future != null )
941958 return future ;
942959
@@ -953,24 +970,24 @@ private ShutdownFuture shutdown() {
953970 metrics .shutdown ();
954971
955972 // Then we shutdown all connections
956- List <ShutdownFuture > futures = new ArrayList <ShutdownFuture >(sessions .size () + 1 );
957- futures .add (controlConnection .shutdown ());
973+ List <CloseFuture > futures = new ArrayList <CloseFuture >(sessions .size () + 1 );
974+ futures .add (controlConnection .closeAsync ());
958975 for (Session session : sessions )
959- futures .add (session .shutdown ());
976+ futures .add (session .closeAsync ());
960977
961- future = new ClusterShutdownFuture (futures );
978+ future = new ClusterCloseFuture (futures );
962979
963980 // The rest will happen asynchonrously, when all connections are successfully closed
964- return shutdownFuture .compareAndSet (null , future )
981+ return closeFuture .compareAndSet (null , future )
965982 ? future
966- : shutdownFuture .get (); // We raced, it's ok, return the future that was actually set
983+ : closeFuture .get (); // We raced, it's ok, return the future that was actually set
967984 }
968985
969986 @ Override
970987 public void onUp (final Host host ) {
971988 logger .trace ("Host {} is UP" , host );
972989
973- if (isShutdown ())
990+ if (isClosed ())
974991 return ;
975992
976993 if (host .isUp ())
@@ -1043,7 +1060,7 @@ public void onDown(final Host host) {
10431060 public void onDown (final Host host , final boolean isHostAddition ) {
10441061 logger .trace ("Host {} is DOWN" , host );
10451062
1046- if (isShutdown ())
1063+ if (isClosed ())
10471064 return ;
10481065
10491066 // Note: we don't want to skip that method if !host.isUp() because we set isUp
@@ -1104,7 +1121,7 @@ protected boolean onUnknownException(Exception e, long nextDelayMs) {
11041121 public void onAdd (final Host host ) {
11051122 logger .trace ("Adding new host {}" , host );
11061123
1107- if (isShutdown ())
1124+ if (isClosed ())
11081125 return ;
11091126
11101127 try {
@@ -1154,7 +1171,7 @@ public void onFailure(Throwable t) {
11541171
11551172 @ Override
11561173 public void onRemove (Host host ) {
1157- if (isShutdown ())
1174+ if (isClosed ())
11581175 return ;
11591176
11601177 host .setDown ();
@@ -1267,7 +1284,7 @@ private void prepareAllQueries(Host host) throws InterruptedException {
12671284 }
12681285 }
12691286 } finally {
1270- connection .close ();
1287+ connection .closeAsync ();
12711288 }
12721289 } catch (ConnectionException e ) {
12731290 // Ignore, not a big deal
@@ -1419,14 +1436,14 @@ private int delayForEvent(ProtocolEvent event) {
14191436 return 0 ;
14201437 }
14211438
1422- private class ClusterShutdownFuture extends ShutdownFuture .Forwarding {
1439+ private class ClusterCloseFuture extends CloseFuture .Forwarding {
14231440
1424- ClusterShutdownFuture (List <ShutdownFuture > futures ) {
1441+ ClusterCloseFuture (List <CloseFuture > futures ) {
14251442 super (futures );
14261443 }
14271444
14281445 @ Override
1429- public ShutdownFuture force () {
1446+ public CloseFuture force () {
14301447 reconnectionExecutor .shutdownNow ();
14311448 scheduledTasksExecutor .shutdownNow ();
14321449 executor .shutdownNow ();
0 commit comments