Package io.lettuce.core.protocol
Class DefaultEndpoint
- java.lang.Object
-
- io.lettuce.core.protocol.DefaultEndpoint
-
- All Implemented Interfaces:
AsyncCloseable
,AsyncCloseable
,Endpoint
,PushHandler
,RedisChannelWriter
,Closeable
,AutoCloseable
- Direct Known Subclasses:
PubSubEndpoint
public class DefaultEndpoint extends Object implements RedisChannelWriter, Endpoint, PushHandler
DefaultEndpoint
implementation.- Author:
- Mark Paluch
-
-
Constructor Summary
Constructors Constructor Description DefaultEndpoint(ClientOptions clientOptions, ClientResources clientResources)
Create a newDefaultEndpoint
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(PushListener listener)
Add a newlistener
.void
close()
Close the connection.CompletableFuture<Void>
closeAsync()
Asynchronously close theRedisChannelWriter
.void
disconnect()
Disconnect the channel.protected <T> T
doExclusive(Supplier<T> supplier)
Execute aSupplier
callback guarded by an exclusive lock.protected List<RedisCommand<?,?,?>>
drainCommands()
void
flushCommands()
Flush pending commands.ClientResources
getClientResources()
String
getId()
protected ProtocolVersion
getProtocolVersion()
List<PushListener>
getPushListeners()
Returns a collection ofPushListener
.void
initialState()
Reset the command-handler to the initial not-connected state.boolean
isClosed()
protected String
logPrefix()
void
notifyChannelActive(Channel channel)
Notify about channel activation.void
notifyChannelInactive(Channel channel)
Notify about channel deactivation.void
notifyDrainQueuedCommands(io.lettuce.core.protocol.HasQueuedCommands queuedCommands)
Signal the endpoint to drain queued commands from the queue holder.void
notifyException(Throwable t)
Notify about an exception occured in channel/command processingvoid
registerConnectionWatchdog(ConnectionWatchdog connectionWatchdog)
Associate aConnectionWatchdog
with theEndpoint
.void
removeListener(PushListener listener)
Remove an existinglistener
.void
reset()
Reset the writer state.void
setAutoFlushCommands(boolean autoFlush)
Disable or enable auto-flush behavior.void
setConnectionFacade(ConnectionFacade connectionFacade)
Set the corresponding connection facade in order to notify it about channel active/inactive state.<K,V,T>
RedisCommand<K,V,T>write(RedisCommand<K,V,T> command)
Write a command on the channel.<K,V>
Collection<RedisCommand<K,V,?>>write(Collection<? extends RedisCommand<K,V,?>> commands)
Write multiple commands on the channel.protected <C extends RedisCommand<?,?,T>,T>
voidwriteToBuffer(C command)
-
-
-
Field Detail
-
channel
protected volatile Channel channel
-
-
Constructor Detail
-
DefaultEndpoint
public DefaultEndpoint(ClientOptions clientOptions, ClientResources clientResources)
Create a newDefaultEndpoint
.- Parameters:
clientOptions
- client options for this connection, must not benull
.clientResources
- client resources for this connection, must not benull
.
-
-
Method Detail
-
setConnectionFacade
public void setConnectionFacade(ConnectionFacade connectionFacade)
Description copied from interface:RedisChannelWriter
Set the corresponding connection facade in order to notify it about channel active/inactive state.- Specified by:
setConnectionFacade
in interfaceRedisChannelWriter
- Parameters:
connectionFacade
- the connection facade (external connection object)
-
getClientResources
public ClientResources getClientResources()
- Specified by:
getClientResources
in interfaceRedisChannelWriter
- Returns:
- the
ClientResources
.
-
setAutoFlushCommands
public void setAutoFlushCommands(boolean autoFlush)
Description copied from interface:RedisChannelWriter
Disable or enable auto-flush behavior. Default istrue
. If autoFlushCommands is disabled, multiple commands can be issued without writing them actually to the transport. Commands are buffered until aRedisChannelWriter.flushCommands()
is issued. After callingRedisChannelWriter.flushCommands()
commands are sent to the transport and executed by Redis.- Specified by:
setAutoFlushCommands
in interfaceRedisChannelWriter
- Parameters:
autoFlush
- state of autoFlush.
-
addListener
public void addListener(PushListener listener)
Description copied from interface:PushHandler
Add a newlistener
.- Specified by:
addListener
in interfacePushHandler
- Parameters:
listener
- the listener, must not benull
.
-
removeListener
public void removeListener(PushListener listener)
Description copied from interface:PushHandler
Remove an existinglistener
.- Specified by:
removeListener
in interfacePushHandler
- Parameters:
listener
- the listener, must not benull
.
-
getPushListeners
public List<PushListener> getPushListeners()
Description copied from interface:PushHandler
Returns a collection ofPushListener
.- Specified by:
getPushListeners
in interfacePushHandler
- Returns:
- the collection of listeners.
-
write
public <K,V,T> RedisCommand<K,V,T> write(RedisCommand<K,V,T> command)
Description copied from interface:RedisChannelWriter
Write a command on the channel. The command may be changed/wrapped during write and the written instance is returned after the call.- Specified by:
write
in interfaceRedisChannelWriter
T
- result type- Parameters:
command
- the Redis command.- Returns:
- the written Redis command.
-
write
public <K,V> Collection<RedisCommand<K,V,?>> write(Collection<? extends RedisCommand<K,V,?>> commands)
Description copied from interface:RedisChannelWriter
Write multiple commands on the channel. The commands may be changed/wrapped during write and the written instance is returned after the call.- Specified by:
write
in interfaceRedisChannelWriter
- Type Parameters:
K
- key typeV
- value type- Parameters:
commands
- the Redis commands.- Returns:
- the written redis command
-
writeToBuffer
protected <C extends RedisCommand<?,?,T>,T> void writeToBuffer(C command)
-
notifyChannelActive
public void notifyChannelActive(Channel channel)
Description copied from interface:Endpoint
Notify about channel activation.- Specified by:
notifyChannelActive
in interfaceEndpoint
- Parameters:
channel
- the channel
-
notifyChannelInactive
public void notifyChannelInactive(Channel channel)
Description copied from interface:Endpoint
Notify about channel deactivation.- Specified by:
notifyChannelInactive
in interfaceEndpoint
- Parameters:
channel
- the channel
-
notifyException
public void notifyException(Throwable t)
Description copied from interface:Endpoint
Notify about an exception occured in channel/command processing- Specified by:
notifyException
in interfaceEndpoint
- Parameters:
t
- the Exception
-
registerConnectionWatchdog
public void registerConnectionWatchdog(ConnectionWatchdog connectionWatchdog)
Description copied from interface:Endpoint
Associate aConnectionWatchdog
with theEndpoint
.- Specified by:
registerConnectionWatchdog
in interfaceEndpoint
- Parameters:
connectionWatchdog
- the connection watchdog.
-
flushCommands
public void flushCommands()
Description copied from interface:RedisChannelWriter
Flush pending commands. This commands forces a flush on the channel and can be used to buffer ("pipeline") commands to achieve batching. No-op if channel is not connected.- Specified by:
flushCommands
in interfaceRedisChannelWriter
-
close
public void close()
Close the connection.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceRedisChannelWriter
-
closeAsync
public CompletableFuture<Void> closeAsync()
Description copied from interface:RedisChannelWriter
Asynchronously close theRedisChannelWriter
.- Specified by:
closeAsync
in interfaceAsyncCloseable
- Specified by:
closeAsync
in interfaceAsyncCloseable
- Specified by:
closeAsync
in interfaceRedisChannelWriter
- Returns:
- future for result synchronization.
-
disconnect
public void disconnect()
Disconnect the channel.
-
reset
public void reset()
Reset the writer state. Queued commands will be canceled and the internal state will be reset. This is useful when the internal state machine gets out of sync with the connection.- Specified by:
reset
in interfaceRedisChannelWriter
-
initialState
public void initialState()
Reset the command-handler to the initial not-connected state.- Specified by:
initialState
in interfaceEndpoint
-
notifyDrainQueuedCommands
public void notifyDrainQueuedCommands(io.lettuce.core.protocol.HasQueuedCommands queuedCommands)
Description copied from interface:Endpoint
Signal the endpoint to drain queued commands from the queue holder.- Specified by:
notifyDrainQueuedCommands
in interfaceEndpoint
- Parameters:
queuedCommands
- the queue holder.
-
isClosed
public boolean isClosed()
-
doExclusive
protected <T> T doExclusive(Supplier<T> supplier)
Execute aSupplier
callback guarded by an exclusive lock.- Type Parameters:
T
-- Parameters:
supplier
-- Returns:
-
drainCommands
protected List<RedisCommand<?,?,?>> drainCommands()
-
logPrefix
protected String logPrefix()
-
getProtocolVersion
protected ProtocolVersion getProtocolVersion()
-
-