public abstract class ConnectionBase extends java.lang.Object implements Connection
Socket, and maintains the reference to
the handler.
Further, it provides the default NotSupportedException response for the
Connection's methods that the extending classes of various Connection.Modality
are expected to support. (They would simply implement the method that they support.)
Connection.Event, Connection.Factory, Connection.Flag, Connection.Listener, Connection.Modality, Connection.Property, Connection.Socket, Connection.State| Modifier and Type | Field and Description |
|---|---|
private java.io.InputStream |
instream |
private boolean |
isConnected |
private java.util.Set<Connection.Listener> |
listeners
Connector Listeners
|
private java.io.OutputStream |
outstream |
private java.net.Socket |
socket
socket reference -- a new instance obtained in
newSocketConnect() |
private java.net.InetSocketAddress |
socketAddress
address of the socket connection
|
protected ConnectionSpec |
spec
Connection specs used to create this
Connection |
(package private) java.lang.ThreadLocal<Protocol> |
thrdProtocol
Thread specific protocol handler -- optimize fencing
Protocol specific matters are delegated to an instance of
Protocol |
| Modifier | Constructor and Description |
|---|---|
protected |
ConnectionBase(ConnectionSpec spec)
Will create and initialize a socket per the connection spec.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addListener(Connection.Listener connListener)
Optional
|
protected void |
connect() |
protected void |
disconnect() |
protected java.io.InputStream |
getInputStream() |
protected java.io.OutputStream |
getOutputStream() |
protected Protocol |
getProtocolHandler() |
ConnectionSpec |
getSpec()
The
ConnectionSpec of a Connection must be invariant during its life-cycle. |
protected void |
initializeAsyncConnection() |
protected void |
initializeComponents()
Extension point: child classes may override for additional components:
|
protected void |
initializeOnConnect() |
protected void |
initializeSocketStreams() |
protected void |
initializeSyncConnection() |
protected boolean |
isConnected() |
protected java.io.InputStream |
newInputStream(java.io.InputStream socketInputStream)
Extension point: override to return stream per requirement.
|
protected java.io.OutputStream |
newOutputStream(java.io.OutputStream socketOutputStream)
Extension point: override to return stream per requirement.
|
protected Protocol |
newProtocolHandler()
Extension point: child classes may override to return specific
Protocol implementations per their requirements. |
private void |
newSocketConnect()
Instantiates a new
Socket, sets its properties and flags using the spec
and finally connects to the socketAddress. |
protected void |
notifyConnected()
Extension point -- callback on this method when
ConnectionBase has connected to server. |
protected void |
notifyDisconnected()
Extension point -- callback on this method when
ConnectionBase has disconnected from server. |
protected void |
notifyFaulted(java.lang.String info) |
protected void |
notifyListeners(Connection.Event e) |
protected void |
notifyShuttingDown() |
protected void |
onConnectionFault(java.lang.String fault,
boolean raiseEx)
Will throw a
ClientRuntimeException if raiseEx is true |
java.util.concurrent.Future<Response> |
queueRequest(Command cmd,
byte[]... args)
A non-blocking call to service the specified request at some point in the future.
|
protected void |
reconnect()
Attempt reconnect.
|
boolean |
removeListener(Connection.Listener connListener)
Optional
|
Response |
serviceRequest(Command cmd,
byte[]... args)
A blocking call to service the specified request.
|
protected void |
shutdown() |
private void |
socketClose() |
java.lang.String |
toString() |
java.lang.ThreadLocal<Protocol> thrdProtocol
Protocolprotected final ConnectionSpec spec
Connectionprivate boolean isConnected
private java.net.Socket socket
newSocketConnect()private java.io.InputStream instream
private java.io.OutputStream outstream
private final java.util.Set<Connection.Listener> listeners
private final java.net.InetSocketAddress socketAddress
protected ConnectionBase(ConnectionSpec spec) throws ClientRuntimeException
spec - connectImmediately - will connect the socket immediately if trueClientRuntimeException - if connection attempt to specified host is not possible and
connect immediate was requested.public ConnectionSpec getSpec()
ConnectionConnectionSpec of a Connection must be invariant during its life-cycle.getSpec in interface ConnectionConnectionSpec for this Connection.public Response serviceRequest(Command cmd, byte[]... args) throws RedisException, ClientRuntimeException, ProviderException
ConnectionProtocol interface
and can (and should) be addressed at the implementation level (for example when creating
handler instances using a specification set, including max wait for synchronous response.)
Connection.Modality.Asynchronous handlers must always throw a ClientRuntimeException
for this method which violates the contract for Connection.Modality.Asynchronous handlers.
serviceRequest in interface ConnectionRedisExceptionClientRuntimeExceptionProviderExceptionpublic java.util.concurrent.Future<Response> queueRequest(Command cmd, byte[]... args) throws ClientRuntimeException, ProviderException
ConnectionFuture object of parametric type Response
When the request is serviced, call to Future.get() will return the request response.
Connection.Modality.Synchronous handlers must always throw a ClientRuntimeException
for this method which violates the contract for Connection.Modality.Synchronous handlers.
If request resulted in a redis error (RedisException), the exception will be set as the cause of
the corresponding ExecutionException of the Future object returned.
queueRequest in interface ConnectionFuture Response.ClientRuntimeExceptionProviderExceptionFuture,
ExecutionExceptionpublic final boolean addListener(Connection.Listener connListener)
addListener in interface ConnectionconnListener - public final boolean removeListener(Connection.Listener connListener)
removeListener in interface ConnectionconnListener - protected void initializeComponents()
In the extended class:
protected void initializeComponents() {
super.initializeComponents();
// my components here ...
//
}
protected void notifyConnected()
ConnectionBase has connected to server.
It is important to note that the extension must call super.notifyConnected if reliable service (using
heartbeats) is required!.protected void notifyDisconnected()
ConnectionBase has disconnected from server.
It is important to note that the extension must call super.notifyDisconnected if reliable service (using
heartbeats) is required!.protected void notifyFaulted(java.lang.String info)
protected void notifyShuttingDown()
protected Protocol newProtocolHandler()
Protocol implementations per their requirements.protected java.io.InputStream newInputStream(java.io.InputStream socketInputStream)
FastBufferedInputStream by default,
with buffer size matching the SO_RCVBUF property of the Connection's ConnectionSpecsocketInputStream - protected java.io.OutputStream newOutputStream(java.io.OutputStream socketOutputStream)
socketOutputStream - protected final void notifyListeners(Connection.Event e)
protected final boolean isConnected()
protected final void reconnect()
java.lang.IllegalStateException - if not (logically) connected.protected final void onConnectionFault(java.lang.String fault,
boolean raiseEx)
throws ClientRuntimeException
ClientRuntimeException if raiseEx is truejava.lang.IllegalStateExceptionClientRuntimeExceptionprotected final void connect()
throws java.lang.IllegalStateException,
ClientRuntimeException
java.io.IOExceptionjava.lang.IllegalStateExceptionClientRuntimeExceptionprotected final void disconnect()
throws java.lang.IllegalStateException
java.lang.IllegalStateExceptionprotected final void shutdown()
throws java.lang.IllegalStateException
java.lang.IllegalStateExceptionprivate final void newSocketConnect()
throws java.io.IOException
Socket, sets its properties and flags using the spec
and finally connects to the socketAddress.
Note that if the platform default send and receive buffers are larger than that specified, this method will not use the (smaller) values defined in the spec.
Further note that method will not check connection state.
java.io.IOException - thrown by the socket object.private final void socketClose()
protected final void initializeSocketStreams()
throws java.lang.IllegalArgumentException,
java.io.IOException
java.lang.IllegalStateException - if socket is nulljava.io.IOException - thrown by socket instance stream accessorsjava.lang.IllegalArgumentExceptionprotected final void initializeOnConnect()
throws ProviderException,
ClientRuntimeException,
RedisException
protected final void initializeSyncConnection()
throws ProviderException,
ClientRuntimeException,
RedisException
protected final void initializeAsyncConnection()
throws ProviderException,
ClientRuntimeException,
RedisException
public java.lang.String toString()
toString in class java.lang.Objectprotected final Protocol getProtocolHandler()
protected final java.io.OutputStream getOutputStream()
protected final java.io.InputStream getInputStream()