Search in sources :

Example 1 with CommandReader

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);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CommandReader(com.ctrip.xpipe.redis.core.store.CommandReader) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException) ReferenceFileRegion(com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)

Aggregations

ReferenceFileRegion (com.ctrip.xpipe.netty.filechannel.ReferenceFileRegion)1 CommandReader (com.ctrip.xpipe.redis.core.store.CommandReader)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 IOException (java.io.IOException)1