All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.RedissonReactive Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/**
 * Copyright (c) 2013-2020 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.redisson.api.*;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import org.redisson.config.ConfigSupport;
import org.redisson.connection.ConnectionManager;
import org.redisson.eviction.EvictionScheduler;
import org.redisson.reactive.*;
import org.redisson.remote.ResponseEntry;

/**
 * Main infrastructure class allows to get access
 * to all Redisson objects on top of Redis server.
 *
 * @author Nikita Koksharov
 *
 */
public class RedissonReactive implements RedissonReactiveClient {

    protected final WriteBehindService writeBehindService;
    protected final EvictionScheduler evictionScheduler;
    protected final CommandReactiveService commandExecutor;
    protected final ConnectionManager connectionManager;
    protected final Config config;
    
    protected final ConcurrentMap responses = new ConcurrentHashMap<>();

    protected RedissonReactive(Config config) {
        this.config = config;
        Config configCopy = new Config(config);

        connectionManager = ConfigSupport.createConnectionManager(configCopy);
        commandExecutor = new CommandReactiveService(connectionManager);
        evictionScheduler = new EvictionScheduler(commandExecutor);
        writeBehindService = new WriteBehindService(commandExecutor);
    }
    
    public EvictionScheduler getEvictionScheduler() {
        return evictionScheduler;
    }
    
    public ConnectionManager getConnectionManager() {
        return connectionManager;
    }
    
    public CommandReactiveService getCommandExecutor() {
        return commandExecutor;
    }
    
    @Override
    public  RStreamReactive getStream(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonStream(commandExecutor, name), RStreamReactive.class);
    }

    @Override
    public  RStreamReactive getStream(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonStream(codec, commandExecutor, name), RStreamReactive.class);
    }

    @Override
    public  RGeoReactive getGeo(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonGeo(commandExecutor, name, null), 
                new RedissonScoredSortedSetReactive(commandExecutor, name), RGeoReactive.class);
    }
    
    @Override
    public  RGeoReactive getGeo(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonGeo(codec, commandExecutor, name, null), 
                new RedissonScoredSortedSetReactive(codec, commandExecutor, name), RGeoReactive.class);
    }
    
    @Override
    public RLockReactive getFairLock(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonFairLock(commandExecutor, name), RLockReactive.class);
    }
    
    @Override
    public RRateLimiterReactive getRateLimiter(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonRateLimiter(commandExecutor, name), RRateLimiterReactive.class);
    }
    
    @Override
    public RBinaryStreamReactive getBinaryStream(String name) {
        RedissonBinaryStream stream = new RedissonBinaryStream(commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, stream,
                new RedissonBinaryStreamReactive(commandExecutor, stream), RBinaryStreamReactive.class);
    }

    @Override
    public RSemaphoreReactive getSemaphore(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonSemaphore(commandExecutor, name), RSemaphoreReactive.class);
    }

    @Override
    public RPermitExpirableSemaphoreReactive getPermitExpirableSemaphore(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonPermitExpirableSemaphore(commandExecutor, name), RPermitExpirableSemaphoreReactive.class);
    }

    @Override
    public RReadWriteLockReactive getReadWriteLock(String name) {
        return new RedissonReadWriteLockReactive(commandExecutor, name);
    }

    @Override
    public RLockReactive getLock(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonLock(commandExecutor, name), RLockReactive.class);
    }

    @Override
    public RLockReactive getSpinLock(String name) {
        return getSpinLock(name, LockOptions.defaults());
    }

    @Override
    public RLockReactive getSpinLock(String name, LockOptions.BackOff backOff) {
        RedissonSpinLock spinLock = new RedissonSpinLock(commandExecutor, name, backOff);
        return ReactiveProxyBuilder.create(commandExecutor, spinLock, RLockReactive.class);
    }

    @Override
    public RLockReactive getMultiLock(RLock... locks) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonMultiLock(locks), RLockReactive.class);
    }
    
    @Override
    public RLockReactive getRedLock(RLock... locks) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonRedLock(locks), RLockReactive.class);
    }

    @Override
    public RCountDownLatchReactive getCountDownLatch(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonCountDownLatch(commandExecutor, name), RCountDownLatchReactive.class);
    }

    @Override
    public  RMapCacheReactive getMapCache(String name, Codec codec) {
        RMapCache map = new RedissonMapCache(codec, evictionScheduler, commandExecutor, name, null, null, null);
        return ReactiveProxyBuilder.create(commandExecutor, map, 
                new RedissonMapCacheReactive(map, commandExecutor), RMapCacheReactive.class);
    }

    @Override
    public  RMapCacheReactive getMapCache(String name) {
        RMapCache map = new RedissonMapCache(evictionScheduler, commandExecutor, name, null, null, null);
        return ReactiveProxyBuilder.create(commandExecutor, map, 
                new RedissonMapCacheReactive(map, commandExecutor), RMapCacheReactive.class);
    }

    @Override
    public  RBucketReactive getBucket(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonBucket(commandExecutor, name), RBucketReactive.class);
    }

    @Override
    public  RBucketReactive getBucket(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonBucket(codec, commandExecutor, name), RBucketReactive.class);
    }

    @Override
    public RBucketsReactive getBuckets() {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonBuckets(commandExecutor), RBucketsReactive.class);
    }

    @Override
    public RBucketsReactive getBuckets(Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonBuckets(codec, commandExecutor), RBucketsReactive.class);
    }

    @Override
    public  List> findBuckets(String pattern) {
        RKeys redissonKeys = new RedissonKeys(commandExecutor);
        Iterable keys = redissonKeys.getKeysByPattern(pattern);

        List> buckets = new ArrayList>();
        for (Object key : keys) {
            if (key != null) {
                buckets.add(this.getBucket(key.toString()));
            }
        }
        return buckets;
    }

    @Override
    public  RHyperLogLogReactive getHyperLogLog(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonHyperLogLog(commandExecutor, name), RHyperLogLogReactive.class);
    }

    @Override
    public  RHyperLogLogReactive getHyperLogLog(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonHyperLogLog(codec, commandExecutor, name), RHyperLogLogReactive.class);
    }

    @Override
    public RIdGeneratorReactive getIdGenerator(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonIdGenerator(commandExecutor, name), RIdGeneratorReactive.class);
    }

    @Override
    public  RListReactive getList(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonList(commandExecutor, name, null), 
                new RedissonListReactive(commandExecutor, name), RListReactive.class);
    }

    @Override
    public  RListReactive getList(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonList(codec, commandExecutor, name, null), 
                new RedissonListReactive(codec, commandExecutor, name), RListReactive.class);
    }

    @Override
    public  RListMultimapReactive getListMultimap(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonListMultimap(commandExecutor, name), 
                new RedissonListMultimapReactive(commandExecutor, name), RListMultimapReactive.class);
    }

    @Override
    public  RListMultimapReactive getListMultimap(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonListMultimap(codec, commandExecutor, name), 
                new RedissonListMultimapReactive(codec, commandExecutor, name), RListMultimapReactive.class);
    }

    @Override
    public  RSetMultimapReactive getSetMultimap(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonSetMultimap(commandExecutor, name), 
                new RedissonSetMultimapReactive(commandExecutor, name, this), RSetMultimapReactive.class);
    }

    @Override
    public  RSetMultimapReactive getSetMultimap(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonSetMultimap(codec, commandExecutor, name), 
                new RedissonSetMultimapReactive(codec, commandExecutor, name, this), RSetMultimapReactive.class);
    }

    @Override
    public  RMapReactive getMap(String name) {
        RedissonMap map = new RedissonMap(commandExecutor, name, null, null, null);
        return ReactiveProxyBuilder.create(commandExecutor, map, 
                new RedissonMapReactive(map, commandExecutor), RMapReactive.class);
    }

    @Override
    public  RMapReactive getMap(String name, Codec codec) {
        RedissonMap map = new RedissonMap(codec, commandExecutor, name, null, null, null);
        return ReactiveProxyBuilder.create(commandExecutor, map, 
                new RedissonMapReactive(map, commandExecutor), RMapReactive.class);
    }

    @Override
    public  RSetReactive getSet(String name) {
        RedissonSet set = new RedissonSet(commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, set, 
                new RedissonSetReactive(set, this), RSetReactive.class);
    }

    @Override
    public  RSetReactive getSet(String name, Codec codec) {
        RedissonSet set = new RedissonSet(codec, commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, set, 
                new RedissonSetReactive(set, this), RSetReactive.class);
    }

    @Override
    public  RScoredSortedSetReactive getScoredSortedSet(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonScoredSortedSet(commandExecutor, name, null), 
                new RedissonScoredSortedSetReactive(commandExecutor, name), RScoredSortedSetReactive.class);
    }

    @Override
    public  RScoredSortedSetReactive getScoredSortedSet(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonScoredSortedSet(codec, commandExecutor, name, null), 
                new RedissonScoredSortedSetReactive(codec, commandExecutor, name), RScoredSortedSetReactive.class);
    }

    @Override
    public RLexSortedSetReactive getLexSortedSet(String name) {
        RedissonLexSortedSet set = new RedissonLexSortedSet(commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, set, 
                new RedissonLexSortedSetReactive(set), 
                RLexSortedSetReactive.class);
    }

    @Override
    public RTopicReactive getTopic(String name) {
        RedissonTopic topic = new RedissonTopic(commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, topic,
                new RedissonTopicReactive(topic), RTopicReactive.class);
    }

    @Override
    public RTopicReactive getTopic(String name, Codec codec) {
        RedissonTopic topic = new RedissonTopic(codec, commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, topic, 
                new RedissonTopicReactive(topic), RTopicReactive.class);
    }

    @Override
    public RReliableTopicReactive getReliableTopic(String name) {
        RedissonReliableTopic topic = new RedissonReliableTopic(commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, topic,
                new RedissonReliableTopicReactive(topic), RReliableTopicReactive.class);
    }

    @Override
    public RReliableTopicReactive getReliableTopic(String name, Codec codec) {
        RedissonReliableTopic topic = new RedissonReliableTopic(codec, commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, topic,
                new RedissonReliableTopicReactive(topic), RReliableTopicReactive.class);
    }

    @Override
    public RPatternTopicReactive getPatternTopic(String pattern) {
         return ReactiveProxyBuilder.create(commandExecutor, new RedissonPatternTopic(commandExecutor, pattern), RPatternTopicReactive.class);
    }

    @Override
    public RPatternTopicReactive getPatternTopic(String pattern, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonPatternTopic(codec, commandExecutor, pattern), RPatternTopicReactive.class);
    }

    @Override
    public  RQueueReactive getQueue(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonQueue(commandExecutor, name, null), 
                new RedissonListReactive(commandExecutor, name), RQueueReactive.class);
    }

    @Override
    public  RQueueReactive getQueue(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonQueue(codec, commandExecutor, name, null), 
                new RedissonListReactive(codec, commandExecutor, name), RQueueReactive.class);
    }
    
    @Override
    public  RRingBufferReactive getRingBuffer(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonRingBuffer(commandExecutor, name, null), RRingBufferReactive.class);
    }

    @Override
    public  RRingBufferReactive getRingBuffer(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonRingBuffer(codec, commandExecutor, name, null), RRingBufferReactive.class);
    }

    @Override
    public  RBlockingQueueReactive getBlockingQueue(String name) {
        RedissonBlockingQueue queue = new RedissonBlockingQueue(commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, queue, 
                new RedissonBlockingQueueReactive(queue), RBlockingQueueReactive.class);
    }

    @Override
    public  RBlockingQueueReactive getBlockingQueue(String name, Codec codec) {
        RedissonBlockingQueue queue = new RedissonBlockingQueue(codec, commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, queue, 
                new RedissonBlockingQueueReactive(queue), RBlockingQueueReactive.class);
    }

    @Override
    public  RDequeReactive getDeque(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonDeque(commandExecutor, name, null), 
                new RedissonListReactive(commandExecutor, name), RDequeReactive.class);
    }

    @Override
    public  RDequeReactive getDeque(String name, Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonDeque(codec, commandExecutor, name, null), 
                new RedissonListReactive(codec, commandExecutor, name), RDequeReactive.class);
    }

    @Override
    public  RTimeSeriesReactive getTimeSeries(String name) {
        RTimeSeries timeSeries = new RedissonTimeSeries(evictionScheduler, commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, timeSeries,
                new RedissonTimeSeriesReactive(timeSeries, this), RTimeSeriesReactive.class);
    }

    @Override
    public  RTimeSeriesReactive getTimeSeries(String name, Codec codec) {
        RTimeSeries timeSeries = new RedissonTimeSeries(codec, evictionScheduler, commandExecutor, name);
        return ReactiveProxyBuilder.create(commandExecutor, timeSeries,
                new RedissonTimeSeriesReactive(timeSeries, this), RTimeSeriesReactive.class);
    }

    @Override
    public  RSetCacheReactive getSetCache(String name) {
        RSetCache set = new RedissonSetCache(evictionScheduler, commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, set, 
                new RedissonSetCacheReactive(set, this), RSetCacheReactive.class);
    }

    @Override
    public  RSetCacheReactive getSetCache(String name, Codec codec) {
        RSetCache set = new RedissonSetCache(codec, evictionScheduler, commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, set, 
                new RedissonSetCacheReactive(set, this), RSetCacheReactive.class);
    }

    @Override
    public RAtomicLongReactive getAtomicLong(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonAtomicLong(commandExecutor, name), RAtomicLongReactive.class);
    }

    @Override
    public RAtomicDoubleReactive getAtomicDouble(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonAtomicDouble(commandExecutor, name), RAtomicDoubleReactive.class);
    }
    
    @Override
    public RRemoteService getRemoteService() {
        return getRemoteService("redisson_rs", connectionManager.getCodec());
    }

    @Override
    public RRemoteService getRemoteService(String name) {
        return getRemoteService(name, connectionManager.getCodec());
    }

    @Override
    public RRemoteService getRemoteService(Codec codec) {
        return getRemoteService("redisson_rs", codec);
    }

    @Override
    public RRemoteService getRemoteService(String name, Codec codec) {
        String executorId = connectionManager.getId();
        if (codec != connectionManager.getCodec()) {
            executorId = executorId + ":" + name;
        }
        return new RedissonRemoteService(codec, name, commandExecutor, executorId, responses);
    }

    @Override
    public RBitSetReactive getBitSet(String name) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonBitSet(commandExecutor, name), RBitSetReactive.class);
    }

    @Override
    public RScriptReactive getScript() {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonScript(commandExecutor), RScriptReactive.class);
    }
    
    @Override
    public RScriptReactive getScript(Codec codec) {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonScript(commandExecutor, codec), RScriptReactive.class);
    }

    @Override
    public RBatchReactive createBatch(BatchOptions options) {
        RedissonBatchReactive batch = new RedissonBatchReactive(evictionScheduler, connectionManager, commandExecutor, options);
        if (config.isReferenceEnabled()) {
            batch.enableRedissonReferenceSupport(this);
        }
        return batch;
    }

    @Override
    public RBatchReactive createBatch() {
        return createBatch(BatchOptions.defaults());
    }

    @Override
    public RKeysReactive getKeys() {
        return ReactiveProxyBuilder.create(commandExecutor, new RedissonKeys(commandExecutor), new RedissonKeysReactive(commandExecutor), RKeysReactive.class);
    }

    @Override
    public Config getConfig() {
        return config;
    }

    @Override
    public NodesGroup getNodesGroup() {
        return new RedisNodes(connectionManager);
    }

    @Override
    public NodesGroup getClusterNodesGroup() {
        if (!connectionManager.isClusterMode()) {
            throw new IllegalStateException("Redisson not in cluster mode!");
        }
        return new RedisNodes(connectionManager);
    }

    @Override
    public void shutdown() {
        connectionManager.shutdown();
    }

    @Override
    public boolean isShutdown() {
        return connectionManager.isShutdown();
    }

    @Override
    public boolean isShuttingDown() {
        return connectionManager.isShuttingDown();
    }

    protected void enableRedissonReferenceSupport() {
        this.commandExecutor.enableRedissonReferenceSupport(this);
    }

    @Override
    public  RMapCacheReactive getMapCache(String name, Codec codec, MapOptions options) {
        RMapCache map = new RedissonMapCache<>(codec, evictionScheduler, commandExecutor, name, null, options, writeBehindService);
        return ReactiveProxyBuilder.create(commandExecutor, map,
                new RedissonMapCacheReactive<>(map, commandExecutor), RMapCacheReactive.class);
    }


    @Override
    public  RMapCacheReactive getMapCache(String name, MapOptions options) {
        RMapCache map = new RedissonMapCache(evictionScheduler, commandExecutor, name, null, options, writeBehindService);
        return ReactiveProxyBuilder.create(commandExecutor, map,
                new RedissonMapCacheReactive<>(map, commandExecutor), RMapCacheReactive.class);
    }

    @Override
    public  RMapReactive getMap(String name, MapOptions options) {
        RMap map = new RedissonMap(commandExecutor, name, null, options, writeBehindService);
        return ReactiveProxyBuilder.create(commandExecutor, map, 
                new RedissonMapReactive(map, commandExecutor), RMapReactive.class);
    }

    @Override
    public  RMapReactive getMap(String name, Codec codec, MapOptions options) {
        RMap map = new RedissonMap<>(codec, commandExecutor, name, null, options, writeBehindService);
        return ReactiveProxyBuilder.create(commandExecutor, map,
                new RedissonMapReactive<>(map, commandExecutor), RMapReactive.class);
    }

    @Override
    public RTransactionReactive createTransaction(TransactionOptions options) {
        return new RedissonTransactionReactive(commandExecutor, options);
    }

    @Override
    public  RBlockingDequeReactive getBlockingDeque(String name) {
        RedissonBlockingDeque deque = new RedissonBlockingDeque(commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, deque, 
                new RedissonBlockingDequeReactive(deque), RBlockingDequeReactive.class);
    }

    @Override
    public  RBlockingDequeReactive getBlockingDeque(String name, Codec codec) {
        RedissonBlockingDeque deque = new RedissonBlockingDeque(codec, commandExecutor, name, null);
        return ReactiveProxyBuilder.create(commandExecutor, deque, 
                new RedissonBlockingDequeReactive(deque), RBlockingDequeReactive.class);
    }

    @Override
    public  RTransferQueueReactive getTransferQueue(String name) {
        String remoteName = RedissonObject.suffixName(name, "remoteService");
        RRemoteService service = getRemoteService(remoteName);
        RedissonTransferQueue queue = new RedissonTransferQueue(connectionManager.getCommandExecutor(), name, service);
        return ReactiveProxyBuilder.create(commandExecutor, queue,
                new RedissonTransferQueueReactive(queue), RTransferQueueReactive.class);
    }

    @Override
    public  RTransferQueueReactive getTransferQueue(String name, Codec codec) {
        String remoteName = RedissonObject.suffixName(name, "remoteService");
        RRemoteService service = getRemoteService(remoteName);
        RedissonTransferQueue queue = new RedissonTransferQueue(codec, connectionManager.getCommandExecutor(), name, service);
        return ReactiveProxyBuilder.create(commandExecutor, queue,
                new RedissonTransferQueueReactive(queue), RTransferQueueReactive.class);
    }

    @Override
    public String getId() {
        return commandExecutor.getConnectionManager().getId();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy