public abstract class ConnectionBase extends 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 |
---|---|
protected ConnectionSpec |
spec
Connection specs used to create this
Connection |
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 InputStream |
getInputStream() |
protected 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 InputStream |
newInputStream(InputStream socketInputStream)
Extension point: override to return stream per requirement.
|
protected OutputStream |
newOutputStream(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. |
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(String info) |
protected void |
notifyListeners(Connection.Event e) |
protected void |
notifyShuttingDown() |
protected void |
onConnectionFault(String fault,
boolean raiseEx)
Will throw a
ClientRuntimeException if raiseEx is true |
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() |
String |
toString() |
protected final ConnectionSpec spec
Connection
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()
Connection
ConnectionSpec
of a Connection must be invariant during its life-cycle.getSpec
in interface Connection
ConnectionSpec
for this Connection.public Response serviceRequest(Command cmd, byte[]... args) throws RedisException, ClientRuntimeException, ProviderException
Connection
Protocol
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 Connection
RedisException
ClientRuntimeException
ProviderException
public Future<Response> queueRequest(Command cmd, byte[]... args) throws ClientRuntimeException, ProviderException
Connection
Future
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 Connection
Future
Response
.ClientRuntimeException
ProviderException
Future
,
ExecutionException
public final boolean addListener(Connection.Listener connListener)
addListener
in interface Connection
connListener
- public final boolean removeListener(Connection.Listener connListener)
removeListener
in interface Connection
connListener
- 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(String info)
protected void notifyShuttingDown()
protected Protocol newProtocolHandler()
Protocol
implementations per their requirements.protected InputStream newInputStream(InputStream socketInputStream)
FastBufferedInputStream
by default,
with buffer size matching the SO_RCVBUF property of the Connection
's ConnectionSpec
socketInputStream
- protected OutputStream newOutputStream(OutputStream socketOutputStream)
socketOutputStream
- protected final void notifyListeners(Connection.Event e)
protected final boolean isConnected()
protected final void reconnect()
IllegalStateException
- if not (logically) connected.protected final void onConnectionFault(String fault, boolean raiseEx) throws ClientRuntimeException
ClientRuntimeException
if raiseEx is trueprotected final void connect() throws IllegalStateException, ClientRuntimeException
protected final void disconnect() throws IllegalStateException
IllegalStateException
protected final void shutdown() throws IllegalStateException
IllegalStateException
protected final void initializeSocketStreams() throws IllegalArgumentException, IOException
IllegalStateException
- if socket is nullIOException
- thrown by socket instance stream accessorsIllegalArgumentException
protected final void initializeOnConnect() throws ProviderException, ClientRuntimeException, RedisException
protected final void initializeSyncConnection() throws ProviderException, ClientRuntimeException, RedisException
protected final void initializeAsyncConnection() throws ProviderException, ClientRuntimeException, RedisException
protected final Protocol getProtocolHandler()
protected final OutputStream getOutputStream()
protected final InputStream getInputStream()
Copyright © 2009–2015. All rights reserved.