use of com.ctrip.xpipe.redis.core.store.CommandReader in project x-pipe by ctripcorp.
the class DefaultCommandStore method addCommandsListener.
@Override
public void addCommandsListener(long offset, final CommandsListener listener) throws IOException {
makeSureOpen();
logger.info("[addCommandsListener][begin] from offset {}, {}", offset, listener);
CommandReader cmdReader = null;
try {
cmdReader = beginRead(offset);
} finally {
// ensure beforeCommand() is always called
listener.beforeCommand();
}
logger.info("[addCommandsListener] from offset {}, {}", offset, cmdReader);
try {
while (listener.isOpen() && !Thread.currentThread().isInterrupted()) {
final ReferenceFileRegion referenceFileRegion = cmdReader.read();
logger.debug("[addCommandsListener] {}", referenceFileRegion);
if (delayTraceLogger.isDebugEnabled()) {
delayTraceLogger.debug("[write][begin]{}, {}", listener, referenceFileRegion.getTotalPos());
}
commandStoreDelay.beginSend(listener, referenceFileRegion.getTotalPos());
ChannelFuture future = listener.onCommand(referenceFileRegion);
if (future != null) {
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
commandStoreDelay.flushSucceed(listener, referenceFileRegion.getTotalPos());
if (logger.isDebugEnabled()) {
delayTraceLogger.debug("[write][ end ]{}, {}", listener, referenceFileRegion.getTotalPos());
}
}
});
}
if (referenceFileRegion.count() <= 0) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
} catch (Throwable th) {
logger.error("[readCommands][exit]" + listener, th);
} finally {
cmdReader.close();
}
logger.info("[addCommandsListener][end] from offset {}, {}", offset, listener);
}
Aggregations