public class SocketConnection extends Object implements Connection
Modifier and Type | Class and Description |
---|---|
protected static class |
SocketConnection.NoopCallback
NoopCallback provides a callback that does not look at the reply.
|
protected class |
SocketConnection.ReceiveRunnable
Runnable to receive messages.
|
protected class |
SocketConnection.SendRunnable
Runnable to push data out over the MongoDB connection.
|
Modifier and Type | Field and Description |
---|---|
static int |
HEADER_LENGTH
The length of the message header in bytes.
|
protected static Logger |
LOG
The logger for the
SocketConnection . |
protected Executor |
myExecutor
The executor for the responses.
|
protected AtomicBoolean |
myOpen
Holds if the connection is open.
|
protected PendingMessageQueue |
myPendingQueue
The queue of messages sent but waiting for a reply.
|
protected AtomicBoolean |
myShutdown
Set to true when the connection should be gracefully closed.
|
protected PendingMessageQueue |
myToSendQueue
The queue of messages to be sent.
|
static MongoDbException |
NO_REPLY
Exception that there was no reply for a message from MongoDB.
|
COMMAND_COLLECTION, OPEN_PROP_NAME
Constructor and Description |
---|
SocketConnection(ServerState server,
MongoClientConfiguration config)
Creates a new SocketConnection to a MongoDB server.
|
Modifier and Type | Method and Description |
---|---|
void |
addPending(List<PendingMessage> pending)
Adds the pending messages from the specified list.
|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a
PropertyChangeListener to this connection. |
void |
close() |
protected Message |
doReceive()
Receives a single message from the connection.
|
protected void |
doSend(int messageId,
Message message)
Sends a single message to the connection.
|
void |
drainPending(List<PendingMessage> pending)
Removes any to be sent pending messages into the specified list.
|
void |
flush()
/**
|
int |
getPendingCount()
Returns the number of messages that are pending responses from the
server.
|
String |
getServerName()
Returns the name of a server the connection is currently connected to.
|
boolean |
isIdle()
Determines if the connection is idle.
|
boolean |
isOpen()
Determines if the connection is open.
|
protected void |
raiseError(Throwable exception,
Callback<Reply> replyCallback)
Updates to raise an error on the callback, if any.
|
void |
raiseErrors(MongoDbException exception,
boolean notifyToBeSent)
Notifies the call backs for the pending and optionally the to be sent
messages that there has been an external, unrecoverable error.
|
protected int |
readIntSuppressTimeoutOnNonFirstByte()
Reads a little-endian 4 byte signed integer from the stream.
|
void |
removePropertyChangeListener(PropertyChangeListener listener)
Removes a
PropertyChangeListener from this connection. |
protected void |
reply(Reply reply,
Callback<Reply> replyCallback)
Updates to set the reply for the callback, if any.
|
String |
send(Message message,
Callback<Reply> replyCallback)
Sends a message on the connection.
|
String |
send(Message message1,
Message message2,
Callback<Reply> replyCallback)
Sends a message on the connection.
|
void |
shutdown()
Notifies the connection that once all outstanding requests have been sent
and all replies received the Connection should be closed.
|
void |
start()
Starts the connections read and write threads.
|
void |
stop()
Stops the socket connection by calling
shutdown() . |
String |
toString() |
protected void |
updateSocketWithOptions(MongoClientConfiguration config)
Updates the socket with the configuration's socket options.
|
void |
waitForClosed(int timeout,
TimeUnit timeoutUnits)
Waits for the connection to become idle.
|
protected void |
waitForPending(int count,
long millis)
Waits for the requested number of messages to become pending.
|
public static final int HEADER_LENGTH
public static final MongoDbException NO_REPLY
protected static final Logger LOG
SocketConnection
.protected final Executor myExecutor
protected final AtomicBoolean myOpen
protected final PendingMessageQueue myPendingQueue
protected final AtomicBoolean myShutdown
protected final PendingMessageQueue myToSendQueue
public SocketConnection(ServerState server, MongoClientConfiguration config) throws SocketException, IOException
server
- The MongoDB server to connect to.config
- The configuration for the Connection to the MongoDB server.SocketException
- On a failure connecting to the MongoDB server.IOException
- On a failure to read or write data to the MongoDB server.public void addPending(List<PendingMessage> pending) throws InterruptedException
addPending
in interface Connection
pending
- The list to populate the pending list with.InterruptedException
- On the thread being interrupted while copying the messages.public void addPropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
to this connection. Events are
fired as the state of the connection changes.
Overridden to add the listener to this connection.
addPropertyChangeListener
in interface Connection
listener
- The listener for the change events.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void drainPending(List<PendingMessage> pending)
drainPending
in interface Connection
pending
- The list to populate with the pending messages.public void flush() throws IOException
flush
in interface Flushable
IOException
public int getPendingCount()
getPendingCount
in interface Connection
public String getServerName()
Overridden to returns the server's name.
getServerName
in interface Connection
public boolean isIdle()
True if the send and pending queues are empty.
isIdle
in interface Connection
public boolean isOpen()
True if the send and receive threads are running.
isOpen
in interface Connection
public void raiseErrors(MongoDbException exception, boolean notifyToBeSent)
Notifies the appropriate messages of the error.
raiseErrors
in interface Connection
exception
- The error condition.notifyToBeSent
- If true then the to be sent message's callback are also
notified, otherwise just the pending messages are notified.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 String send(Message message, Callback<Reply> replyCallback) throws MongoDbException
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 String send(Message message1, Message message2, Callback<Reply> replyCallback) throws MongoDbException
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 shutdown()
Overridden to mark the socket as shutting down and tickles the sender to make sure that happens as soon as possible.
shutdown
in interface Connection
public void start()
public void stop()
shutdown()
.public String toString()
Overridden to return the socket information.
public void waitForClosed(int timeout, TimeUnit timeoutUnits)
Waits for the connections pending queues to empty.
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 Message doReceive() throws MongoDbException
Message
received.MongoDbException
- On an error receiving the message.protected void doSend(int messageId, Message message) throws IOException
messageId
- The id to use for the message.message
- The message to send.IOException
- On a failure sending the message.protected void raiseError(Throwable exception, Callback<Reply> replyCallback)
exception
- The thrown exception.replyCallback
- The callback for the reply to the message.protected int readIntSuppressTimeoutOnNonFirstByte() throws EOFException, IOException
EOFException
- On insufficient data for the integer.IOException
- On a failure reading the integer.protected void reply(Reply reply, Callback<Reply> replyCallback)
reply
- The reply.replyCallback
- The callback for the reply to the message.protected void updateSocketWithOptions(MongoClientConfiguration config) throws SocketException
config
- The configuration to apply.SocketException
- On a failure setting the socket options.protected void waitForPending(int count, long millis)
count
- The number of pending messages expected.millis
- The number of milliseconds to wait.Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.