K
- The key used to track the various connections.public abstract class AbstractProxyMultipleConnection<K> extends Object implements Connection
Modifier and Type | Class and Description |
---|---|
protected class |
AbstractProxyMultipleConnection.ClusterAndConnectionListener
ClusterListener provides a listener for changes in the cluster.
|
Modifier and Type | Field and Description |
---|---|
protected Cluster |
myCluster
The state of the cluster for finding secondary connections.
|
protected MongoClientConfiguration |
myConfig
The MongoDB client configuration.
|
protected PropertyChangeSupport |
myEventSupport
Support for emitting property change events.
|
protected ProxiedConnectionFactory |
myFactory
The connection factory for opening secondary connections.
|
protected AtomicReference<Connection> |
myLastUsedConnection
The most recently used connection.
|
protected PropertyChangeListener |
myListener
The listener for changes in the cluster and connections.
|
protected K |
myMainKey
The primary instance this connection is connected to.
|
protected AtomicBoolean |
myOpen
Set to false when the connection is closed.
|
protected AtomicBoolean |
myShutdown
Set to true when the connection should be gracefully closed.
|
COMMAND_COLLECTION, OPEN_PROP_NAME
Constructor and Description |
---|
AbstractProxyMultipleConnection(Connection proxiedConnection,
K server,
Cluster cluster,
ProxiedConnectionFactory factory,
MongoClientConfiguration config)
Creates a new
AbstractProxyMultipleConnection . |
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a
PropertyChangeListener to this connection. |
protected Connection |
cacheConnection(K server,
Connection conn)
Caches the connection to the server if there is not already a connection
in the cache.
|
void |
close()
Closes the underlying connection.
|
protected abstract Connection |
connect(K server)
Attempts to create a connection to the server, catching any exceptions
thrown.
|
protected Connection |
connection(K server)
Returns the cached connection for the specified key.
|
protected MongoDbException |
createReconnectFailure(Message message1,
Message message2)
Creates a exception for a reconnect failure.
|
protected void |
doSend(Connection conn,
Message message1,
Message message2,
ReplyCallback reply)
Sends the message on the connection.
|
protected abstract List<K> |
findPotentialKeys(Message message1,
Message message2)
Locates the set of servers that can be used to send the specified
messages.
|
void |
flush() |
protected abstract String |
getConnectionType()
Returns the type of connection (for logs, etc.).
|
int |
getPendingCount()
Returns the number of messages that are pending responses from the
server.
|
protected void |
handleConnectionClosed(Connection connection)
Tries to reconnect previously open
Connection s. |
boolean |
isAvailable()
Returns true if the connection is open and not shutting down, false
otherwise.
|
boolean |
isIdle()
Determines if the connection is idle.
|
boolean |
isOpen()
Determines if the connection is open.
|
boolean |
isShuttingDown()
Returns true if the connection is being gracefully closed, false
otherwise.
|
void |
raiseErrors(MongoDbException exception)
Notifies the call backs for the pending and optionally the to be sent
messages that there has been an external, unrecoverable error.
|
protected abstract ConnectionInfo<K> |
reconnectMain()
Creates a connection back to the main server for this connection.
|
protected void |
removeCachedConnection(Object key,
Connection connection)
Remove the connection from the cache.
|
void |
removePropertyChangeListener(PropertyChangeListener listener)
Removes a
PropertyChangeListener from this connection. |
void |
send(Message message1,
Message message2,
ReplyCallback replyCallback)
Sends a message on the connection.
|
void |
send(Message message,
ReplyCallback replyCallback)
Sends a message on the connection.
|
void |
shutdown(boolean force)
Notifies the connection that once all outstanding requests have been sent
and all replies received the Connection should be closed.
|
String |
toString() |
protected boolean |
trySend(List<K> servers,
Message message1,
Message message2,
ReplyCallback reply)
Tries to send the messages to the first server with either an open
connection or that we can open a connection to.
|
protected void |
updateMain(ConnectionInfo<K> newConn)
Update the state with the new primary server.
|
void |
waitForClosed(int timeout,
TimeUnit timeoutUnits)
Waits for the connection to become idle.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getServerName
protected final Cluster myCluster
protected final MongoClientConfiguration myConfig
protected final PropertyChangeSupport myEventSupport
protected final ProxiedConnectionFactory myFactory
protected final AtomicReference<Connection> myLastUsedConnection
protected final PropertyChangeListener myListener
protected volatile K myMainKey
protected final AtomicBoolean myOpen
protected final AtomicBoolean myShutdown
public AbstractProxyMultipleConnection(Connection proxiedConnection, K server, Cluster cluster, ProxiedConnectionFactory factory, MongoClientConfiguration config)
AbstractProxyMultipleConnection
.proxiedConnection
- The connection being proxied.server
- The primary server this connection is connected to.cluster
- The state of the cluster for finding secondary connections.factory
- The connection factory for opening secondary connections.config
- The MongoDB client configuration.public void addPropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
to this connection. Events are
fired as the state of the connection changes.
Overridden to add this listener to this connection's event source.
addPropertyChangeListener
in interface Connection
listener
- The listener for the change events.public void close()
close
in interface Closeable
close
in interface AutoCloseable
Closeable.close()
public void flush() throws IOException
Forwards the call to the proxied Connection
.
flush
in interface Flushable
IOException
Flushable.flush()
public int getPendingCount()
Overridden to return the pending count for the last connection used to send a message.
getPendingCount
in interface Connection
public boolean isAvailable()
True if the connection is open and not shutting down.
isAvailable
in interface Connection
public boolean isIdle()
Overridden to return if the last used connection is idle.
isIdle
in interface Connection
public boolean isOpen()
Overridden to return if this connection has any open connections.
isOpen
in interface Connection
public boolean isShuttingDown()
Overridden to return if the last used connection is shutting down.
isShuttingDown
in interface Connection
public void raiseErrors(MongoDbException exception)
Overridden to raise the errors with all of the underlying connections.
raiseErrors
in interface Connection
exception
- The error condition.public void removePropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
from this connection.
Overridden to remove the listener from this connection.
removePropertyChangeListener
in interface Connection
listener
- The listener for the change events.public void send(Message message1, Message message2, ReplyCallback replyCallback) throws MongoDbException
Locates all of the potential servers that can receive all of the messages. Tries to then send the messages to a server with a connection already open or failing that tries to open a connection to open of the servers.
send
in interface Connection
message1
- The first message to send on the connection.message2
- The second message to send on the connection.replyCallback
- The callback to notify of responses to the message2
.
May be null
.MongoDbException
- On an error sending the message.public void send(Message message, ReplyCallback replyCallback) throws MongoDbException
Locates all of the potential servers that can receive all of the messages. Tries to then send the messages to a server with a connection already open or failing that tries to open a connection to open of the servers.
send
in interface Connection
message
- The message to send on the connection.replyCallback
- The callback to notify of responses to the messages. May be
null
.MongoDbException
- On an error sending the message.public void shutdown(boolean force)
Overridden to shutdown all of the underlying connections.
shutdown
in interface Connection
force
- If true then the connection can be immediately closed as the
caller knows there are no outstanding requests to the server.public String toString()
Overridden to return the socket information.
public void waitForClosed(int timeout, TimeUnit timeoutUnits)
Overridden to wait for all of the underlying connections to close.
waitForClosed
in interface Connection
timeout
- The amount of time to wait for the connection to become idle.timeoutUnits
- The units for the amount of time to wait for the connection to
become idle.protected Connection cacheConnection(K server, Connection conn)
server
- The server connected to.conn
- The connection to cache, if possible.protected abstract Connection connect(K server)
cached
.server
- The server to connect to.protected Connection connection(K server)
null
.server
- The server connected to.protected MongoDbException createReconnectFailure(Message message1, Message message2)
message1
- The first message to send.message2
- The second message to send.protected void doSend(Connection conn, Message message1, Message message2, ReplyCallback reply)
conn
- The connection to send on.message1
- The first message to send.message2
- The second message to send, may be null
.reply
- The reply Callback
.protected abstract List<K> findPotentialKeys(Message message1, Message message2) throws MongoDbException
message1
- The first message to send.message2
- The second message to send. May be null
.MongoDbException
- On a failure to locate a server that all messages can be sent
to.protected abstract String getConnectionType()
protected void handleConnectionClosed(Connection connection)
Connection
s. If a connection
was being closed then cleans up the remaining state.connection
- The connection that was closed.protected abstract ConnectionInfo<K> reconnectMain()
protected void removeCachedConnection(Object key, Connection connection)
key
- The key to remove the connection for.connection
- The connection to remove (if known).protected boolean trySend(List<K> servers, Message message1, Message message2, ReplyCallback reply)
servers
- The servers the messages can be sent to.message1
- The first message to send.message2
- The second message to send. May be null
.reply
- The callback for the replies.protected void updateMain(ConnectionInfo<K> newConn)
newConn
- The new primary server.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.