Search in sources :

Example 11 with NodeSource

use of org.redisson.connection.NodeSource in project redisson by redisson.

the class CommandAsyncService method readAllAsync.

@Override
public <T, R> RFuture<Collection<R>> readAllAsync(RedisCommand<T> command, Object... params) {
    final RPromise<Collection<R>> mainPromise = connectionManager.newPromise();
    final Set<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
    final List<R> results = new ArrayList<R>();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    FutureListener<R> listener = new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            if (!future.isSuccess()) {
                mainPromise.tryFailure(future.cause());
                return;
            }
            R result = future.getNow();
            if (result instanceof Collection) {
                synchronized (results) {
                    results.addAll((Collection) result);
                }
            } else {
                synchronized (results) {
                    results.add(result);
                }
            }
            if (counter.decrementAndGet() == 0 && !mainPromise.isDone()) {
                mainPromise.trySuccess(results);
            }
        }
    };
    for (MasterSlaveEntry entry : nodes) {
        RPromise<R> promise = connectionManager.newPromise();
        promise.addListener(listener);
        async(true, new NodeSource(entry), connectionManager.getCodec(), command, params, promise, 0);
    }
    return mainPromise;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ArrayList(java.util.ArrayList) NodeSource(org.redisson.connection.NodeSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Collection(java.util.Collection) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Example 12 with NodeSource

use of org.redisson.connection.NodeSource in project redisson by redisson.

the class CommandAsyncService method readAsync.

@Override
public <T, R> RFuture<R> readAsync(String key, Codec codec, RedisCommand<T> command, Object... params) {
    RPromise<R> mainPromise = connectionManager.newPromise();
    NodeSource source = getNodeSource(key);
    async(true, source, codec, command, params, mainPromise, 0);
    return mainPromise;
}
Also used : NodeSource(org.redisson.connection.NodeSource)

Example 13 with NodeSource

use of org.redisson.connection.NodeSource in project redisson by redisson.

the class CommandAsyncService method allAsync.

private <T, R> RFuture<R> allAsync(boolean readOnlyMode, RedisCommand<T> command, final SlotCallback<T, R> callback, Object... params) {
    final RPromise<R> mainPromise = connectionManager.newPromise();
    final Set<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    FutureListener<T> listener = new FutureListener<T>() {

        @Override
        public void operationComplete(Future<T> future) throws Exception {
            if (!future.isSuccess()) {
                mainPromise.tryFailure(future.cause());
                return;
            }
            if (callback != null) {
                callback.onSlotResult(future.getNow());
            }
            if (counter.decrementAndGet() == 0) {
                if (callback != null) {
                    mainPromise.trySuccess(callback.onFinish());
                } else {
                    mainPromise.trySuccess(null);
                }
            }
        }
    };
    for (MasterSlaveEntry entry : nodes) {
        RPromise<T> promise = connectionManager.newPromise();
        promise.addListener(listener);
        async(readOnlyMode, new NodeSource(entry), connectionManager.getCodec(), command, params, promise, 0);
    }
    return mainPromise;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) NodeSource(org.redisson.connection.NodeSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Example 14 with NodeSource

use of org.redisson.connection.NodeSource in project redisson by redisson.

the class CommandAsyncService method writeAsync.

@Override
public <T, R> RFuture<R> writeAsync(Integer slot, Codec codec, RedisCommand<T> command, Object... params) {
    RPromise<R> mainPromise = connectionManager.newPromise();
    async(false, new NodeSource(slot), codec, command, params, mainPromise, 0);
    return mainPromise;
}
Also used : NodeSource(org.redisson.connection.NodeSource)

Example 15 with NodeSource

use of org.redisson.connection.NodeSource in project redisson by redisson.

the class CommandAsyncService method readAsync.

@Override
public <T, R> RFuture<R> readAsync(InetSocketAddress client, MasterSlaveEntry entry, Codec codec, RedisCommand<T> command, Object... params) {
    RPromise<R> mainPromise = connectionManager.newPromise();
    async(true, new NodeSource(entry, client), codec, command, params, mainPromise, 0);
    return mainPromise;
}
Also used : NodeSource(org.redisson.connection.NodeSource)

Aggregations

NodeSource (org.redisson.connection.NodeSource)17 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 RedisAskException (org.redisson.client.RedisAskException)5 RedisLoadingException (org.redisson.client.RedisLoadingException)5 RedisMovedException (org.redisson.client.RedisMovedException)5 RedisTimeoutException (org.redisson.client.RedisTimeoutException)5 RedisTryAgainException (org.redisson.client.RedisTryAgainException)5 WriteRedisConnectionException (org.redisson.client.WriteRedisConnectionException)5 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 Future (io.netty.util.concurrent.Future)3 FutureListener (io.netty.util.concurrent.FutureListener)3 ArrayList (java.util.ArrayList)3 RFuture (org.redisson.api.RFuture)3 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 RedissonShutdownException (org.redisson.RedissonShutdownException)2 RedisException (org.redisson.client.RedisException)2