forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultServer.java
More file actions
123 lines (105 loc) · 5.02 KB
/
Copy pathDefaultServer.java
File metadata and controls
123 lines (105 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) 2008-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static com.mongodb.ServerConnectionState.Connecting;
import static com.mongodb.ServerConnectionState.Unconnected;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.bson.util.Assertions.isTrue;
import static org.bson.util.Assertions.notNull;
class DefaultServer implements ClusterableServer {
private final ScheduledExecutorService scheduledExecutorService;
private final ServerAddress serverAddress;
private final ServerStateNotifier stateNotifier;
private final ScheduledFuture<?> scheduledFuture;
private final PooledConnectionProvider connectionProvider;
private final Map<ChangeListener<ServerDescription>, Boolean> changeListeners =
new ConcurrentHashMap<ChangeListener<ServerDescription>, Boolean>();
private final ServerSettings settings;
private final ChangeListener<ServerDescription> serverStateListener;
private volatile ServerDescription description;
private volatile boolean isClosed;
public DefaultServer(final ServerAddress serverAddress,
final ServerSettings settings,
final PooledConnectionProvider connectionProvider,
final ScheduledExecutorService scheduledExecutorService,
Mongo mongo) {
this.settings = notNull("settings", settings);
this.scheduledExecutorService = notNull("scheduledExecutorService", scheduledExecutorService);
this.serverAddress = notNull("serverAddress", serverAddress);
this.description = ServerDescription.builder().state(Connecting).address(serverAddress).build();
serverStateListener = new DefaultServerStateListener();
this.stateNotifier = new ServerStateNotifier(serverAddress, serverStateListener,
settings.getHeartbeatSocketSettings(), mongo);
this.scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(stateNotifier, 0,
settings.getHeartbeatFrequency(MILLISECONDS),
MILLISECONDS);
this.connectionProvider = connectionProvider;
}
@Override
public ServerDescription getDescription() {
isTrue("open", !isClosed());
return description;
}
@Override
public Connection getConnection(final long maxWaitTime, final TimeUnit timeUnit) {
return connectionProvider.get(maxWaitTime, timeUnit);
}
@Override
public void addChangeListener(final ChangeListener<ServerDescription> changeListener) {
isTrue("open", !isClosed());
changeListeners.put(changeListener, true);
}
@Override
public void invalidate() {
isTrue("open", !isClosed());
serverStateListener.stateChanged(new ChangeEvent<ServerDescription>(description, ServerDescription.builder()
.state(Connecting)
.address(serverAddress).build()));
scheduledExecutorService.submit(stateNotifier);
connectionProvider.invalidate();
}
@Override
public void close() {
if (!isClosed()) {
scheduledFuture.cancel(true);
stateNotifier.close();
connectionProvider.close();
isClosed = true;
}
}
@Override
public boolean isClosed() {
return isClosed;
}
private final class DefaultServerStateListener implements ChangeListener<ServerDescription> {
@Override
public void stateChanged(final ChangeEvent<ServerDescription> event) {
description = event.getNewValue();
for (ChangeListener<ServerDescription> listener : changeListeners.keySet()) {
listener.stateChanged(event);
}
if (event.getNewValue().getState() == Unconnected) {
scheduledExecutorService.schedule(stateNotifier, settings.getHeartbeatConnectRetryFrequency(MILLISECONDS),
MILLISECONDS);
}
}
}
}