kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5e235af049f67c6c7cbfbc34f32ea152b25d08f3595478b1f687236c6ba3cae1 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-02-09 11:19 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 16:45 +01:00 |
| Parents | 732e04daae01 |
3 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +24 −0
| @@ -435,6 +435,8 @@ | ||
| 435 | 435 | */ |
| 436 | 436 | public Cluster register(Host.StateListener listener) { |
| 437 | 437 | checkNotClosed(manager); |
| 438 | + if (listener instanceof Host.LifecycleAwareStateListener) | |
| 439 | + ((Host.LifecycleAwareStateListener) listener).onRegister(this); | |
| 438 | 440 | manager.listeners.add(listener); |
| 439 | 441 | return this; |
| 440 | 442 | } |
| @@ -450,6 +452,8 @@ | ||
| 450 | 452 | */ |
| 451 | 453 | public Cluster unregister(Host.StateListener listener) { |
| 452 | 454 | checkNotClosed(manager); |
| 455 | + if (listener instanceof Host.LifecycleAwareStateListener) | |
| 456 | + ((Host.LifecycleAwareStateListener) listener).onUnregister(this); | |
| 453 | 457 | manager.listeners.remove(listener); |
| 454 | 458 | return this; |
| 455 | 459 | } |
| @@ -475,6 +479,8 @@ | ||
| 475 | 479 | */ |
| 476 | 480 | public Cluster register(LatencyTracker tracker) { |
| 477 | 481 | checkNotClosed(manager); |
| 482 | + if (tracker instanceof LifecycleAwareLatencyTracker) | |
| 483 | + ((LifecycleAwareLatencyTracker) tracker).onRegister(this); | |
| 478 | 484 | manager.trackers.add(tracker); |
| 479 | 485 | return this; |
| 480 | 486 | } |
| @@ -491,6 +497,8 @@ | ||
| 491 | 497 | */ |
| 492 | 498 | public Cluster unregister(LatencyTracker tracker) { |
| 493 | 499 | checkNotClosed(manager); |
| 500 | + if (tracker instanceof LifecycleAwareLatencyTracker) | |
| 501 | + ((LifecycleAwareLatencyTracker) tracker).onUnregister(this); | |
| 494 | 502 | manager.trackers.remove(tracker); |
| 495 | 503 | return this; |
| 496 | 504 | } |
| @@ -1471,6 +1479,14 @@ | ||
| 1471 | 1479 | // rack...) to initialize the load balancing policy |
| 1472 | 1480 | loadBalancingPolicy().init(Cluster.this, contactPointHosts); |
| 1473 | 1481 | speculativeRetryPolicy().init(Cluster.this); |
| 1482 | + for (LatencyTracker tracker : trackers) { | |
| 1483 | + if (tracker instanceof LifecycleAwareLatencyTracker) | |
| 1484 | + ((LifecycleAwareLatencyTracker) tracker).onRegister(Cluster.this); | |
| 1485 | + } | |
| 1486 | + for (Host.StateListener listener : listeners) { | |
| 1487 | + if (listener instanceof Host.LifecycleAwareStateListener) | |
| 1488 | + ((Host.LifecycleAwareStateListener) listener).onRegister(Cluster.this); | |
| 1489 | + } | |
| 1474 | 1490 | |
| 1475 | 1491 | for (Host host : removedContactPointHosts) { |
| 1476 | 1492 | loadBalancingPolicy().onRemove(host); |
| @@ -1622,6 +1638,14 @@ | ||
| 1622 | 1638 | if (translater instanceof CloseableAddressTranslater) |
| 1623 | 1639 | ((CloseableAddressTranslater) translater).close(); |
| 1624 | 1640 | |
| 1641 | + for (LatencyTracker tracker : trackers) { | |
| 1642 | + if (tracker instanceof LifecycleAwareLatencyTracker) | |
| 1643 | + ((LifecycleAwareLatencyTracker) tracker).onUnregister(Cluster.this); | |
| 1644 | + } | |
| 1645 | + for (Host.StateListener listener : listeners) { | |
| 1646 | + if (listener instanceof Host.LifecycleAwareStateListener) | |
| 1647 | + ((Host.LifecycleAwareStateListener) listener).onUnregister(Cluster.this); | |
| 1648 | + } | |
| 1625 | 1649 | for (SchemaChangeListener listener : schemaChangeListeners) { |
| 1626 | 1650 | listener.onUnregister(Cluster.this); |
| 1627 | 1651 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +25 −0
| @@ -330,4 +330,29 @@ | ||
| 330 | 330 | */ |
| 331 | 331 | public void onRemove(Host host); |
| 332 | 332 | } |
| 333 | + | |
| 334 | + /** | |
| 335 | + * A {@code StateListener} that tracks when it gets registered or unregistered with a cluster. | |
| 336 | + * <p/> | |
| 337 | + * This interface exists only for backward-compatibility reasons: starting with the 3.0 branch of the driver, its | |
| 338 | + * methods are on the parent interface directly. | |
| 339 | + */ | |
| 340 | + public interface LifecycleAwareStateListener extends StateListener { | |
| 341 | + /** | |
| 342 | + * Gets invoked when the listener is registered with a cluster, or at cluster startup if the | |
| 343 | + * listener was registered at initialization with | |
| 344 | + * {@link com.datastax.driver.core.Cluster.Initializer#register(StateListener)}. | |
| 345 | + * | |
| 346 | + * @param cluster the cluster that this listener is registered with. | |
| 347 | + */ | |
| 348 | + void onRegister(Cluster cluster); | |
| 349 | + | |
| 350 | + /** | |
| 351 | + * Gets invoked when the listener is unregistered from a cluster, or at cluster shutdown if | |
| 352 | + * the listener was not unregistered. | |
| 353 | + * | |
| 354 | + * @param cluster the cluster that this listener was registered with. | |
| 355 | + */ | |
| 356 | + void onUnregister(Cluster cluster); | |
| 357 | + } | |
| 333 | 358 | } |
added driver-core/src/main/java/com/datastax/driver/core/LifecycleAwareLatencyTracker.java +41 −0
| @@ -0,0 +1,41 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * A {@code LatencyTracker} that tracks when it gets registered or unregistered with a cluster. | |
| 20 | + * <p/> | |
| 21 | + * This interface exists only for backward-compatibility reasons: starting with the 3.0 branch of the driver, its | |
| 22 | + * methods are on the parent interface directly. | |
| 23 | + */ | |
| 24 | +public interface LifecycleAwareLatencyTracker extends LatencyTracker { | |
| 25 | + /** | |
| 26 | + * Gets invoked when the tracker is registered with a cluster, or at cluster startup if the | |
| 27 | + * tracker was registered at initialization with | |
| 28 | + * {@link com.datastax.driver.core.Cluster.Initializer#register(LatencyTracker)}. | |
| 29 | + * | |
| 30 | + * @param cluster the cluster that this tracker is registered with. | |
| 31 | + */ | |
| 32 | + void onRegister(Cluster cluster); | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Gets invoked when the tracker is unregistered from a cluster, or at cluster shutdown if | |
| 36 | + * the tracker was not unregistered. | |
| 37 | + * | |
| 38 | + * @param cluster the cluster that this tracker was registered with. | |
| 39 | + */ | |
| 40 | + void onUnregister(Cluster cluster); | |
| 41 | +} |