/driver-core/src/main/com/mongodb/binding/AsyncClusterBinding.java

http://github.com/mongodb/mongo-java-driver · Java · 117 lines · 74 code · 16 blank · 27 comment · 3 complexity · 24fdcc7036a3200be6d308c98c0e932f MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 MongoDB, 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.mongodb.binding;
  17. import com.mongodb.ReadPreference;
  18. import com.mongodb.async.SingleResultCallback;
  19. import com.mongodb.connection.AsyncConnection;
  20. import com.mongodb.connection.Cluster;
  21. import com.mongodb.connection.Server;
  22. import com.mongodb.connection.ServerDescription;
  23. import com.mongodb.selector.ReadPreferenceServerSelector;
  24. import com.mongodb.selector.ServerSelector;
  25. import com.mongodb.selector.WritableServerSelector;
  26. import static com.mongodb.assertions.Assertions.notNull;
  27. /**
  28. * A simple ReadWriteBinding implementation that supplies write connection sources bound to a possibly different primary each time, and a
  29. * read connection source bound to a possible different server each time.
  30. *
  31. * @since 3.0
  32. */
  33. public class AsyncClusterBinding extends AbstractReferenceCounted implements AsyncReadWriteBinding {
  34. private final Cluster cluster;
  35. private final ReadPreference readPreference;
  36. /**
  37. * Creates an instance.
  38. *
  39. * @param cluster a non-null Cluster which will be used to select a server to bind to
  40. * @param readPreference a non-null ReadPreference for read operations
  41. */
  42. public AsyncClusterBinding(final Cluster cluster, final ReadPreference readPreference) {
  43. this.cluster = notNull("cluster", cluster);
  44. this.readPreference = notNull("readPreference", readPreference);
  45. }
  46. @Override
  47. public AsyncReadWriteBinding retain() {
  48. super.retain();
  49. return this;
  50. }
  51. @Override
  52. public ReadPreference getReadPreference() {
  53. return readPreference;
  54. }
  55. @Override
  56. public void getReadConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
  57. getAsyncClusterBindingConnectionSource(new ReadPreferenceServerSelector(readPreference), callback);
  58. }
  59. @Override
  60. public void getWriteConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
  61. getAsyncClusterBindingConnectionSource(new WritableServerSelector(), callback);
  62. }
  63. private void getAsyncClusterBindingConnectionSource(final ServerSelector serverSelector,
  64. final SingleResultCallback<AsyncConnectionSource> callback) {
  65. cluster.selectServerAsync(serverSelector, new SingleResultCallback<Server>() {
  66. @Override
  67. public void onResult(final Server result, final Throwable t) {
  68. if (t != null) {
  69. callback.onResult(null, t);
  70. } else {
  71. callback.onResult(new AsyncClusterBindingConnectionSource(result), null);
  72. }
  73. }
  74. });
  75. }
  76. private final class AsyncClusterBindingConnectionSource extends AbstractReferenceCounted implements AsyncConnectionSource {
  77. private final Server server;
  78. private AsyncClusterBindingConnectionSource(final Server server) {
  79. this.server = server;
  80. AsyncClusterBinding.this.retain();
  81. }
  82. @Override
  83. public ServerDescription getServerDescription() {
  84. return server.getDescription();
  85. }
  86. @Override
  87. public void getConnection(final SingleResultCallback<AsyncConnection> callback) {
  88. server.getConnectionAsync(callback);
  89. }
  90. public AsyncConnectionSource retain() {
  91. super.retain();
  92. AsyncClusterBinding.this.retain();
  93. return this;
  94. }
  95. @Override
  96. public void release() {
  97. super.release();
  98. AsyncClusterBinding.this.release();
  99. }
  100. }
  101. }