K
- Key type.V
- Value type.@ChannelHandler.Sharable public class CommandHandler<K,V> extends ChannelDuplexHandler implements RedisChannelWriter<K,V>
ChannelHandler
responsible for writing redis commands and reading responses from the server.Modifier and Type | Class and Description |
---|---|
static class |
CommandHandler.LifecycleState |
ChannelHandler.Sharable
Modifier and Type | Field and Description |
---|---|
protected ByteBuf |
buffer |
protected Channel |
channel |
protected ClientOptions |
clientOptions |
protected ClientResources |
clientResources |
protected Queue<RedisCommand<K,V,?>> |
commandBuffer |
protected long |
commandHandlerId |
protected Queue<RedisCommand<K,V,?>> |
disconnectedBuffer |
protected RedisStateMachine<K,V> |
rsm |
protected Deque<RedisCommand<K,V,?>> |
stack |
protected Object |
stateLock |
protected AtomicLong |
writers |
Constructor and Description |
---|
CommandHandler(ClientOptions clientOptions,
ClientResources clientResources)
Initialize a new instance that handles commands from the supplied queue.
|
Modifier and Type | Method and Description |
---|---|
protected void |
afterDecode(ChannelHandlerContext ctx,
RedisCommand<K,V,?> command)
Hook method called after command completion.
|
protected boolean |
canComplete(RedisCommand<?,?,?> command)
Decoding hook: Can the command be completed.
|
protected boolean |
canDecode(ByteBuf buffer)
Decoding hook: Can the buffer be decoded to a command.
|
void |
channelActive(ChannelHandlerContext ctx) |
void |
channelInactive(ChannelHandlerContext ctx) |
void |
channelRead(ChannelHandlerContext ctx,
Object msg) |
void |
channelRegistered(ChannelHandlerContext ctx) |
void |
channelUnregistered(ChannelHandlerContext ctx) |
void |
close()
Close the connection.
|
protected void |
complete(RedisCommand<?,?,?> command)
Decoding hook: Complete a command.
|
protected void |
decode(ChannelHandlerContext ctx,
ByteBuf buffer) |
protected void |
decrementWriters()
Decrement writers without any wait.
|
protected static <T> List<T> |
drainCommands(Queue<T> source) |
protected static <T> void |
drainCommands(Queue<T> source,
Collection<T> target) |
void |
exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) |
void |
flushCommands()
Flush pending commands.
|
protected CommandOutput<K,V,?> |
getCommandOutput(RedisCommand<K,V,?> command)
Decoding hook: Retrieve
CommandOutput for RedisCommand decoding. |
protected Deque<RedisCommand<K,V,?>> |
getStack() |
protected CommandHandler.LifecycleState |
getState() |
protected void |
incrementWriters()
Wait for stateLock and increment writers.
|
void |
initialState()
Reset the command-handler to the initial not-connected state.
|
boolean |
isClosed() |
protected void |
lockWritersExclusive()
Wait for stateLock and no writers.
|
protected String |
logPrefix() |
protected List<RedisCommand<K,V,?>> |
prepareReset() |
void |
reset()
Reset the writer state.
|
void |
setAutoFlushCommands(boolean autoFlush)
Disable or enable auto-flush behavior.
|
void |
setRedisChannelHandler(RedisChannelHandler<K,V> redisChannelHandler)
Set the corresponding connection instance in order to notify it about channel active/inactive state.
|
protected void |
setState(CommandHandler.LifecycleState lifecycleState) |
protected void |
unlockWritersExclusive()
Unlock writers.
|
void |
userEventTriggered(ChannelHandlerContext ctx,
Object evt) |
<T,C extends RedisCommand<K,V,T>> |
write(C command)
Write a command on the channel.
|
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise) |
protected <C extends RedisCommand<K,V,T>,T> |
writeToDisconnectedBuffer(C command) |
bind, close, connect, deregister, disconnect, flush, read
channelReadComplete, channelWritabilityChanged
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerAdded, handlerRemoved
protected final long commandHandlerId
protected final ClientOptions clientOptions
protected final ClientResources clientResources
protected final Queue<RedisCommand<K,V,?>> disconnectedBuffer
protected final Queue<RedisCommand<K,V,?>> commandBuffer
protected final AtomicLong writers
protected final Object stateLock
protected final Deque<RedisCommand<K,V,?>> stack
protected final ByteBuf buffer
protected final RedisStateMachine<K,V> rsm
protected volatile Channel channel
public CommandHandler(ClientOptions clientOptions, ClientResources clientResources)
clientOptions
- client options for this connection, must not be nullclientResources
- client resources for this connection, must not be nullprotected Deque<RedisCommand<K,V,?>> getStack()
public void setRedisChannelHandler(RedisChannelHandler<K,V> redisChannelHandler)
RedisChannelWriter
setRedisChannelHandler
in interface RedisChannelWriter<K,V>
redisChannelHandler
- the channel handler (external connection object)public void setAutoFlushCommands(boolean autoFlush)
RedisChannelWriter
RedisChannelWriter.flushCommands()
is
issued. After calling RedisChannelWriter.flushCommands()
commands are sent to the transport and executed by Redis.setAutoFlushCommands
in interface RedisChannelWriter<K,V>
autoFlush
- state of autoFlush.protected void setState(CommandHandler.LifecycleState lifecycleState)
protected CommandHandler.LifecycleState getState()
public boolean isClosed()
public void channelRegistered(ChannelHandlerContext ctx) throws Exception
channelRegistered
in interface ChannelInboundHandler
channelRegistered
in class ChannelInboundHandlerAdapter
Exception
ChannelInboundHandlerAdapter.channelRegistered(io.netty.channel.ChannelHandlerContext)
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception
channelUnregistered
in interface ChannelInboundHandler
channelUnregistered
in class ChannelInboundHandlerAdapter
Exception
ChannelInboundHandlerAdapter.channelUnregistered(io.netty.channel.ChannelHandlerContext)
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
channelRead
in interface ChannelInboundHandler
channelRead
in class ChannelInboundHandlerAdapter
Exception
ChannelInboundHandlerAdapter.channelRead(io.netty.channel.ChannelHandlerContext, java.lang.Object)
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer)
protected boolean canDecode(ByteBuf buffer)
buffer
- protected boolean canComplete(RedisCommand<?,?,?> command)
command
- protected void complete(RedisCommand<?,?,?> command)
command
- RedisCommand.complete()
protected void afterDecode(ChannelHandlerContext ctx, RedisCommand<K,V,?> command)
ctx
- command
- protected CommandOutput<K,V,?> getCommandOutput(RedisCommand<K,V,?> command)
CommandOutput
for RedisCommand
decoding.command
- RedisCommand.getOutput()
public <T,C extends RedisCommand<K,V,T>> C write(C command)
RedisChannelWriter
write
in interface RedisChannelWriter<K,V>
T
- result typeC
- command typecommand
- the Redis command.protected <C extends RedisCommand<K,V,T>,T> void writeToDisconnectedBuffer(C command)
protected void incrementWriters()
protected void decrementWriters()
protected void lockWritersExclusive()
synchronized
block to prevent interleaving with other
methods using writers. Sets writers to a negative value to create a lock for incrementWriters()
.protected void unlockWritersExclusive()
public void flushCommands()
RedisChannelWriter
flushCommands
in interface RedisChannelWriter<K,V>
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
write
in interface ChannelOutboundHandler
write
in class ChannelDuplexHandler
Exception
ChannelDuplexHandler.write(io.netty.channel.ChannelHandlerContext, java.lang.Object,
io.netty.channel.ChannelPromise)
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception
userEventTriggered
in interface ChannelInboundHandler
userEventTriggered
in class ChannelInboundHandlerAdapter
Exception
public void channelActive(ChannelHandlerContext ctx) throws Exception
channelActive
in interface ChannelInboundHandler
channelActive
in class ChannelInboundHandlerAdapter
Exception
public void channelInactive(ChannelHandlerContext ctx) throws Exception
channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
Exception
ChannelInboundHandlerAdapter.channelInactive(io.netty.channel.ChannelHandlerContext)
protected List<RedisCommand<K,V,?>> prepareReset()
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
exceptionCaught
in interface ChannelHandler
exceptionCaught
in interface ChannelInboundHandler
exceptionCaught
in class ChannelInboundHandlerAdapter
Exception
public void close()
close
in interface RedisChannelWriter<K,V>
close
in interface Closeable
close
in interface AutoCloseable
public void reset()
reset
in interface RedisChannelWriter<K,V>
public void initialState()
protected String logPrefix()
protected static <T> void drainCommands(Queue<T> source, Collection<T> target)
Copyright © 2018 lettuce.io. All rights reserved.