Search in sources :

Example 1 with QueueCommandHolder

use of org.redisson.client.protocol.QueueCommandHolder in project redisson by redisson.

the class CommandsQueue method sendData.

private void sendData(Channel ch) {
    QueueCommandHolder command = queue.peek();
    if (command != null && command.trySend()) {
        QueueCommand data = command.getCommand();
        List<CommandData<Object, Object>> pubSubOps = data.getPubSubOperations();
        if (!pubSubOps.isEmpty()) {
            for (CommandData<Object, Object> cd : pubSubOps) {
                for (Object channel : cd.getParams()) {
                    ch.pipeline().get(CommandDecoder.class).addPubSubCommand(channel.toString(), cd);
                }
            }
        } else {
            ch.attr(CURRENT_COMMAND).set(data);
        }
        command.getChannelPromise().addListener(listener);
        ch.writeAndFlush(data, command.getChannelPromise());
    }
}
Also used : QueueCommandHolder(org.redisson.client.protocol.QueueCommandHolder) QueueCommand(org.redisson.client.protocol.QueueCommand) CommandData(org.redisson.client.protocol.CommandData)

Example 2 with QueueCommandHolder

use of org.redisson.client.protocol.QueueCommandHolder in project redisson by redisson.

the class CommandsQueue method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof QueueCommand) {
        QueueCommand data = (QueueCommand) msg;
        QueueCommandHolder holder = new QueueCommandHolder(data, promise);
        Queue<QueueCommandHolder> queue = ctx.channel().attr(COMMANDS_QUEUE).get();
        while (true) {
            if (lock.compareAndSet(false, true)) {
                try {
                    queue.add(holder);
                    ctx.writeAndFlush(data, holder.getChannelPromise());
                } finally {
                    lock.set(false);
                }
                break;
            }
        }
    } else {
        super.write(ctx, msg, promise);
    }
}
Also used : QueueCommandHolder(org.redisson.client.protocol.QueueCommandHolder) QueueCommand(org.redisson.client.protocol.QueueCommand)

Example 3 with QueueCommandHolder

use of org.redisson.client.protocol.QueueCommandHolder in project redisson by redisson.

the class CommandsQueue method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    Queue<QueueCommandHolder> queue = ctx.channel().attr(COMMANDS_QUEUE).get();
    Iterator<QueueCommandHolder> iterator = queue.iterator();
    while (iterator.hasNext()) {
        QueueCommandHolder command = iterator.next();
        if (command.getCommand().isBlockingCommand()) {
            continue;
        }
        iterator.remove();
        command.getChannelPromise().tryFailure(new WriteRedisConnectionException("Channel has been closed! Can't write command: " + LogHelper.toString(command.getCommand()) + " to channel: " + ctx.channel()));
    }
    super.channelInactive(ctx);
}
Also used : WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) QueueCommandHolder(org.redisson.client.protocol.QueueCommandHolder)

Example 4 with QueueCommandHolder

use of org.redisson.client.protocol.QueueCommandHolder in project redisson by redisson.

the class CommandsQueuePubSub method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof QueueCommand) {
        QueueCommand data = (QueueCommand) msg;
        QueueCommandHolder holder = queue.peek();
        if (holder != null && holder.getCommand() == data) {
            super.write(ctx, msg, promise);
        } else {
            queue.add(new QueueCommandHolder(data, promise));
            sendData(ctx.channel());
        }
    } else {
        super.write(ctx, msg, promise);
    }
}
Also used : QueueCommandHolder(org.redisson.client.protocol.QueueCommandHolder) QueueCommand(org.redisson.client.protocol.QueueCommand)

Example 5 with QueueCommandHolder

use of org.redisson.client.protocol.QueueCommandHolder in project redisson by redisson.

the class CommandsQueuePubSub method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    while (true) {
        QueueCommandHolder command = queue.poll();
        if (command == null) {
            break;
        }
        command.getChannelPromise().tryFailure(new WriteRedisConnectionException("Channel has been closed! Can't write command: " + LogHelper.toString(command.getCommand()) + " to channel: " + ctx.channel()));
    }
    super.channelInactive(ctx);
}
Also used : WriteRedisConnectionException(org.redisson.client.WriteRedisConnectionException) QueueCommandHolder(org.redisson.client.protocol.QueueCommandHolder)

Aggregations

QueueCommandHolder (org.redisson.client.protocol.QueueCommandHolder)7 QueueCommand (org.redisson.client.protocol.QueueCommand)5 CommandData (org.redisson.client.protocol.CommandData)3 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)2