/driver-core/src/main/com/mongodb/connection/ServerSettings.java

https://github.com/foursquare/mongo-java-driver · Java · 232 lines · 113 code · 25 blank · 94 comment · 14 complexity · 4b89620e4c08b6165210028606c1b85a 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.connection;
  17. import com.mongodb.ConnectionString;
  18. import com.mongodb.annotations.Immutable;
  19. import com.mongodb.annotations.NotThreadSafe;
  20. import com.mongodb.event.ServerListener;
  21. import com.mongodb.event.ServerMonitorListener;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.concurrent.TimeUnit;
  26. import static com.mongodb.assertions.Assertions.notNull;
  27. /**
  28. * Settings relating to monitoring of each server.
  29. *
  30. * @since 3.0
  31. */
  32. @Immutable
  33. public class ServerSettings {
  34. private final long heartbeatFrequencyMS;
  35. private final long minHeartbeatFrequencyMS;
  36. private final List<ServerListener> serverListeners;
  37. private final List<ServerMonitorListener> serverMonitorListeners;
  38. /**
  39. * Creates a builder for ServerSettings.
  40. *
  41. * @return a new Builder for creating ServerSettings.
  42. */
  43. public static Builder builder() {
  44. return new Builder();
  45. }
  46. /**
  47. * A builder for the settings.
  48. */
  49. @NotThreadSafe
  50. public static class Builder {
  51. private long heartbeatFrequencyMS = 10000;
  52. private long minHeartbeatFrequencyMS = 500;
  53. private final List<ServerListener> serverListeners = new ArrayList<ServerListener>();
  54. private final List<ServerMonitorListener> serverMonitorListeners = new ArrayList<ServerMonitorListener>();
  55. /**
  56. * Sets the frequency that the cluster monitor attempts to reach each server. The default value is 10 seconds.
  57. *
  58. * @param heartbeatFrequency the heartbeat frequency
  59. * @param timeUnit the time unit
  60. * @return this
  61. */
  62. public Builder heartbeatFrequency(final long heartbeatFrequency, final TimeUnit timeUnit) {
  63. this.heartbeatFrequencyMS = TimeUnit.MILLISECONDS.convert(heartbeatFrequency, timeUnit);
  64. return this;
  65. }
  66. /**
  67. * Sets the minimum heartbeat frequency. In the event that the driver has to frequently re-check a server's availability, it will
  68. * wait at least this long since the previous check to avoid wasted effort. The default value is 500 milliseconds.
  69. *
  70. * @param minHeartbeatFrequency the minimum heartbeat frequency
  71. * @param timeUnit the time unit
  72. * @return this
  73. */
  74. public Builder minHeartbeatFrequency(final long minHeartbeatFrequency, final TimeUnit timeUnit) {
  75. this.minHeartbeatFrequencyMS = TimeUnit.MILLISECONDS.convert(minHeartbeatFrequency, timeUnit);
  76. return this;
  77. }
  78. /**
  79. * Add a server listener.
  80. *
  81. * @param serverListener the non-null server listener
  82. * @return this
  83. * @since 3.3
  84. */
  85. public Builder addServerListener(final ServerListener serverListener) {
  86. notNull("serverListener", serverListener);
  87. serverListeners.add(serverListener);
  88. return this;
  89. }
  90. /**
  91. * Adds a server monitor listener.
  92. *
  93. * @param serverMonitorListener the non-null server monitor listener
  94. * @return this
  95. * @since 3.3
  96. */
  97. public Builder addServerMonitorListener(final ServerMonitorListener serverMonitorListener) {
  98. notNull("serverMonitorListener", serverMonitorListener);
  99. serverMonitorListeners.add(serverMonitorListener);
  100. return this;
  101. }
  102. /**
  103. * Take the settings from the given ConnectionString and add them to the builder
  104. *
  105. * @param connectionString a URI containing details of how to connect to MongoDB
  106. * @return this
  107. * @since 3.3
  108. */
  109. public Builder applyConnectionString(final ConnectionString connectionString) {
  110. if (connectionString.getHeartbeatFrequency() != null) {
  111. heartbeatFrequencyMS = connectionString.getHeartbeatFrequency();
  112. }
  113. return this;
  114. }
  115. /**
  116. * Create a new ServerSettings from the settings applied to this builder.
  117. *
  118. * @return a ServerSettings with the given settings.
  119. */
  120. public ServerSettings build() {
  121. return new ServerSettings(this);
  122. }
  123. }
  124. /**
  125. * Gets the frequency that the cluster monitor attempts to reach each server. The default value is 10 seconds.
  126. *
  127. * @param timeUnit the time unit
  128. * @return the heartbeat frequency
  129. */
  130. public long getHeartbeatFrequency(final TimeUnit timeUnit) {
  131. return timeUnit.convert(heartbeatFrequencyMS, TimeUnit.MILLISECONDS);
  132. }
  133. /**
  134. * Gets the minimum heartbeat frequency. In the event that the driver has to frequently re-check a server's availability, it will wait
  135. * at least this long since the previous check to avoid wasted effort. The default value is 500 milliseconds.
  136. *
  137. * @param timeUnit the time unit
  138. * @return the heartbeat reconnect retry frequency
  139. */
  140. public long getMinHeartbeatFrequency(final TimeUnit timeUnit) {
  141. return timeUnit.convert(minHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
  142. }
  143. /**
  144. * Gets the server listeners. The default value is an empty list.
  145. *
  146. * @return the server listeners
  147. * @since 3.3
  148. */
  149. public List<ServerListener> getServerListeners() {
  150. return Collections.unmodifiableList(serverListeners);
  151. }
  152. /**
  153. * Gets the server monitor listeners. The default value is an empty list.
  154. *
  155. * @return the server monitor listeners
  156. * @since 3.3
  157. */
  158. public List<ServerMonitorListener> getServerMonitorListeners() {
  159. return Collections.unmodifiableList(serverMonitorListeners);
  160. }
  161. @Override
  162. public boolean equals(final Object o) {
  163. if (this == o) {
  164. return true;
  165. }
  166. if (o == null || getClass() != o.getClass()) {
  167. return false;
  168. }
  169. ServerSettings that = (ServerSettings) o;
  170. if (heartbeatFrequencyMS != that.heartbeatFrequencyMS) {
  171. return false;
  172. }
  173. if (minHeartbeatFrequencyMS != that.minHeartbeatFrequencyMS) {
  174. return false;
  175. }
  176. if (!serverListeners.equals(that.serverListeners)) {
  177. return false;
  178. }
  179. if (!serverMonitorListeners.equals(that.serverMonitorListeners)) {
  180. return false;
  181. }
  182. return true;
  183. }
  184. @Override
  185. public int hashCode() {
  186. int result = (int) (heartbeatFrequencyMS ^ (heartbeatFrequencyMS >>> 32));
  187. result = 31 * result + (int) (minHeartbeatFrequencyMS ^ (minHeartbeatFrequencyMS >>> 32));
  188. result = 31 * result + serverListeners.hashCode();
  189. result = 31 * result + serverMonitorListeners.hashCode();
  190. return result;
  191. }
  192. @Override
  193. public String toString() {
  194. return "ServerSettings{"
  195. + "heartbeatFrequencyMS=" + heartbeatFrequencyMS
  196. + ", minHeartbeatFrequencyMS=" + minHeartbeatFrequencyMS
  197. + ", serverListeners='" + serverListeners + '\''
  198. + ", serverMonitorListeners='" + serverMonitorListeners + '\''
  199. + '}';
  200. }
  201. ServerSettings(final Builder builder) {
  202. heartbeatFrequencyMS = builder.heartbeatFrequencyMS;
  203. minHeartbeatFrequencyMS = builder.minHeartbeatFrequencyMS;
  204. serverListeners = builder.serverListeners;
  205. serverMonitorListeners = builder.serverMonitorListeners;
  206. }
  207. }