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 Deque<RedisCommand<K,V,?>> |
commandBuffer |
protected Queue<RedisCommand<K,V,?>> |
queue |
protected RedisStateMachine<K,V> |
rsm |
protected Deque<RedisCommand<K,V,?>> |
transportBuffer |
protected ReentrantLock |
writeLock |
Constructor and Description |
---|
CommandHandler(ClientOptions clientOptions,
ClientResources clientResources,
Queue<RedisCommand<K,V,?>> queue)
Initialize a new instance that handles commands from the supplied queue.
|
Modifier and Type | Method and Description |
---|---|
protected void |
activateCommandHandlerAndExecuteBufferedCommands(ChannelHandlerContext ctx) |
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 |
decode(ChannelHandlerContext ctx,
ByteBuf buffer) |
void |
exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) |
void |
flushCommands()
Flush pending commands.
|
protected CommandHandler.LifecycleState |
getState() |
void |
initialState()
Reset the command-handler to the initial not-connected state.
|
boolean |
isClosed() |
protected boolean |
isConnected() |
protected String |
logPrefix() |
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 |
setStateIfNotClosed(CommandHandler.LifecycleState lifecycleState) |
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise) |
<T> RedisCommand<K,V,T> |
write(RedisCommand<K,V,T> command)
Write a command on the channel.
|
bind, close, connect, deregister, disconnect, flush, read
channelReadComplete, channelWritabilityChanged, userEventTriggered
handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handlerAdded, handlerRemoved
protected final ClientOptions clientOptions
protected final ClientResources clientResources
protected final Queue<RedisCommand<K,V,?>> queue
protected final ReentrantLock writeLock
protected volatile Deque<RedisCommand<K,V,?>> commandBuffer
protected final Deque<RedisCommand<K,V,?>> transportBuffer
protected ByteBuf buffer
protected RedisStateMachine<K,V> rsm
protected Channel channel
public CommandHandler(ClientOptions clientOptions, ClientResources clientResources, Queue<RedisCommand<K,V,?>> queue)
clientOptions
- client options for this connection, must not be nullclientResources
- client resources for this connection, must not be nullqueue
- The command queue, must not be nullpublic 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
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) throws InterruptedException
InterruptedException
public <T> RedisCommand<K,V,T> write(RedisCommand<K,V,T> command)
RedisChannelWriter
write
in interface RedisChannelWriter<K,V>
T
- result typecommand
- the redis commandprotected boolean isConnected()
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 channelActive(ChannelHandlerContext ctx) throws Exception
channelActive
in interface ChannelInboundHandler
channelActive
in class ChannelInboundHandlerAdapter
Exception
protected void activateCommandHandlerAndExecuteBufferedCommands(ChannelHandlerContext ctx)
public void channelInactive(ChannelHandlerContext ctx) throws Exception
channelInactive
in interface ChannelInboundHandler
channelInactive
in class ChannelInboundHandlerAdapter
Exception
ChannelInboundHandlerAdapter.channelInactive(io.netty.channel.ChannelHandlerContext)
protected void setStateIfNotClosed(CommandHandler.LifecycleState lifecycleState)
protected void setState(CommandHandler.LifecycleState lifecycleState)
protected CommandHandler.LifecycleState getState()
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 boolean isClosed()
public void reset()
reset
in interface RedisChannelWriter<K,V>
public void initialState()
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 String logPrefix()
Copyright © 2016. All rights reserved.