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 AtomicBoolean |
myOpen
Holds if the connection is open.
|
protected BlockingQueue<PendingMessage> |
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 BlockingQueue<PendingMessage> |
myToSendQueue
The queue of messages to be sent.
|
COMMAND_COLLECTION, OPEN_PROP_NAME
Constructor and Description |
---|
SocketConnection(ServerState server,
MongoDbConfiguration 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(PendingMessage 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.
|
boolean |
isIdle()
Determines if the connection is idle.
|
boolean |
isOpen()
Determines if the connection is open.
|
protected int |
nextId()
Gets the next id avoiding negative values.
|
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. |
String |
send(Callback<Reply> reply,
Message... messages)
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() |
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
protected static final Logger LOG
SocketConnection
.protected final AtomicBoolean myOpen
protected final BlockingQueue<PendingMessage> myPendingQueue
protected final AtomicBoolean myShutdown
protected final BlockingQueue<PendingMessage> myToSendQueue
public SocketConnection(ServerState server, MongoDbConfiguration 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)
addPending
in interface Connection
pending
- The list to populate the pending list with.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 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(Callback<Reply> reply, Message... messages) throws MongoDbException
send
in interface Connection
reply
- The callback to notify of responses to the messages. May be
null
.messages
- The messages to send on the connection. The messages will be
sent one after the other and are guaranteed to be contiguous
and have sequential message ids.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(PendingMessage message) throws IOException
message
- The pending message to send.IOException
- On a failure sending the message.protected int nextId()
protected int readIntSuppressTimeoutOnNonFirstByte() throws EOFException, IOException
EOFException
- On insufficient data for the integer.IOException
- On a failure reading the integer.protected void waitForPending(int count, long millis)
count
- The number of pending messages expected.millis
- The number of milliseconds to wait.Copyright © 2011-2012 Allanbank Consulting, Inc.. All Rights Reserved.