org.redisson.Redisson Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
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
/**
* 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.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import org.redisson.api.*;
import org.redisson.api.redisnode.*;
import org.redisson.client.codec.Codec;
import org.redisson.command.CommandExecutor;
import org.redisson.config.Config;
import org.redisson.config.ConfigSupport;
import org.redisson.connection.ConnectionManager;
import org.redisson.eviction.EvictionScheduler;
import org.redisson.redisnode.RedissonClusterNodes;
import org.redisson.redisnode.RedissonMasterSlaveNodes;
import org.redisson.redisnode.RedissonSentinelMasterSlaveNodes;
import org.redisson.redisnode.RedissonSingleNode;
import org.redisson.remote.ResponseEntry;
import org.redisson.transaction.RedissonTransaction;
/**
* Main infrastructure class allows to get access
* to all Redisson objects on top of Redis server.
*
* @author Nikita Koksharov
*
*/
public class Redisson implements RedissonClient {
static {
RedissonReference.warmUp();
}
protected final QueueTransferService queueTransferService = new QueueTransferService();
protected final EvictionScheduler evictionScheduler;
protected final WriteBehindService writeBehindService;
protected final ConnectionManager connectionManager;
protected final ConcurrentMap, Class>> liveObjectClassCache = new ConcurrentHashMap<>();
protected final Config config;
protected final ConcurrentMap responses = new ConcurrentHashMap<>();
protected Redisson(Config config) {
this.config = config;
Config configCopy = new Config(config);
connectionManager = ConfigSupport.createConnectionManager(configCopy);
evictionScheduler = new EvictionScheduler(connectionManager.getCommandExecutor());
writeBehindService = new WriteBehindService(connectionManager.getCommandExecutor());
}
public EvictionScheduler getEvictionScheduler() {
return evictionScheduler;
}
public CommandExecutor getCommandExecutor() {
return connectionManager.getCommandExecutor();
}
public ConnectionManager getConnectionManager() {
return connectionManager;
}
/**
* Create sync/async Redisson instance with default config
*
* @return Redisson instance
*/
public static RedissonClient create() {
Config config = new Config();
config.useSingleServer()
.setTimeout(1000000)
.setAddress("redis://127.0.0.1:6379");
// config.useMasterSlaveConnection().setMasterAddress("127.0.0.1:6379").addSlaveAddress("127.0.0.1:6389").addSlaveAddress("127.0.0.1:6399");
// config.useSentinelConnection().setMasterName("mymaster").addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379");
// config.useClusterServers().addNodeAddress("127.0.0.1:7000");
return create(config);
}
/**
* Create sync/async Redisson instance with provided config
*
* @param config for Redisson
* @return Redisson instance
*/
public static RedissonClient create(Config config) {
Redisson redisson = new Redisson(config);
if (config.isReferenceEnabled()) {
redisson.enableRedissonReferenceSupport();
}
return redisson;
}
/**
* Create Reactive Redisson instance with default config
*
* @return Redisson instance
*/
public static RedissonRxClient createRx() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return createRx(config);
}
/**
* Create Reactive Redisson instance with provided config
*
* @param config for Redisson
* @return Redisson instance
*/
public static RedissonRxClient createRx(Config config) {
RedissonRx react = new RedissonRx(config);
if (config.isReferenceEnabled()) {
react.enableRedissonReferenceSupport();
}
return react;
}
/**
* Create Reactive Redisson instance with default config
*
* @return Redisson instance
*/
public static RedissonReactiveClient createReactive() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return createReactive(config);
}
/**
* Create Reactive Redisson instance with provided config
*
* @param config for Redisson
* @return Redisson instance
*/
public static RedissonReactiveClient createReactive(Config config) {
RedissonReactive react = new RedissonReactive(config);
if (config.isReferenceEnabled()) {
react.enableRedissonReferenceSupport();
}
return react;
}
@Override
public RTimeSeries getTimeSeries(String name) {
return new RedissonTimeSeries<>(evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public RTimeSeries getTimeSeries(String name, Codec codec) {
return new RedissonTimeSeries<>(codec, evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public RStream getStream(String name) {
return new RedissonStream(connectionManager.getCommandExecutor(), name);
}
@Override
public RStream getStream(String name, Codec codec) {
return new RedissonStream(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RBinaryStream getBinaryStream(String name) {
return new RedissonBinaryStream(connectionManager.getCommandExecutor(), name);
}
@Override
public RGeo getGeo(String name) {
return new RedissonGeo(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RGeo getGeo(String name, Codec codec) {
return new RedissonGeo(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBucket getBucket(String name) {
return new RedissonBucket(connectionManager.getCommandExecutor(), name);
}
@Override
public RRateLimiter getRateLimiter(String name) {
return new RedissonRateLimiter(connectionManager.getCommandExecutor(), name);
}
@Override
public RBucket getBucket(String name, Codec codec) {
return new RedissonBucket(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RBuckets getBuckets() {
return new RedissonBuckets(connectionManager.getCommandExecutor());
}
@Override
public RBuckets getBuckets(Codec codec) {
return new RedissonBuckets(codec, connectionManager.getCommandExecutor());
}
@Override
public RHyperLogLog getHyperLogLog(String name) {
return new RedissonHyperLogLog(connectionManager.getCommandExecutor(), name);
}
@Override
public RHyperLogLog getHyperLogLog(String name, Codec codec) {
return new RedissonHyperLogLog(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RList getList(String name) {
return new RedissonList(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RList getList(String name, Codec codec) {
return new RedissonList(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RListMultimap getListMultimap(String name) {
return new RedissonListMultimap(connectionManager.getCommandExecutor(), name);
}
@Override
public RListMultimap getListMultimap(String name, Codec codec) {
return new RedissonListMultimap(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RLocalCachedMap getLocalCachedMap(String name, LocalCachedMapOptions options) {
return new RedissonLocalCachedMap(connectionManager.getCommandExecutor(), name,
options, evictionScheduler, this, writeBehindService);
}
@Override
public RLocalCachedMap getLocalCachedMap(String name, Codec codec, LocalCachedMapOptions options) {
return new RedissonLocalCachedMap(codec, connectionManager.getCommandExecutor(), name,
options, evictionScheduler, this, writeBehindService);
}
@Override
public RMap getMap(String name) {
return new RedissonMap(connectionManager.getCommandExecutor(), name, this, null, null);
}
@Override
public RMap getMap(String name, MapOptions options) {
return new RedissonMap(connectionManager.getCommandExecutor(), name, this, options, writeBehindService);
}
@Override
public RSetMultimap getSetMultimap(String name) {
return new RedissonSetMultimap(connectionManager.getCommandExecutor(), name);
}
@Override
public RSetMultimapCache getSetMultimapCache(String name) {
return new RedissonSetMultimapCache(evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public RSetMultimapCache getSetMultimapCache(String name, Codec codec) {
return new RedissonSetMultimapCache(evictionScheduler, codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RListMultimapCache getListMultimapCache(String name) {
return new RedissonListMultimapCache(evictionScheduler, connectionManager.getCommandExecutor(), name);
}
@Override
public RListMultimapCache getListMultimapCache(String name, Codec codec) {
return new RedissonListMultimapCache(evictionScheduler, codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RSetMultimap getSetMultimap(String name, Codec codec) {
return new RedissonSetMultimap(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RSetCache getSetCache(String name) {
return new RedissonSetCache(evictionScheduler, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RSetCache getSetCache(String name, Codec codec) {
return new RedissonSetCache(codec, evictionScheduler, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RMapCache getMapCache(String name) {
return new RedissonMapCache(evictionScheduler, connectionManager.getCommandExecutor(), name, this, null, null);
}
@Override
public RMapCache getMapCache(String name, MapOptions options) {
return new RedissonMapCache(evictionScheduler, connectionManager.getCommandExecutor(), name, this, options, writeBehindService);
}
@Override
public RMapCache getMapCache(String name, Codec codec) {
return new RedissonMapCache(codec, evictionScheduler, connectionManager.getCommandExecutor(), name, this, null, null);
}
@Override
public RMapCache getMapCache(String name, Codec codec, MapOptions options) {
return new RedissonMapCache(codec, evictionScheduler, connectionManager.getCommandExecutor(), name, this, options, writeBehindService);
}
@Override
public RMap getMap(String name, Codec codec) {
return new RedissonMap(codec, connectionManager.getCommandExecutor(), name, this, null, null);
}
@Override
public RMap getMap(String name, Codec codec, MapOptions options) {
return new RedissonMap(codec, connectionManager.getCommandExecutor(), name, this, options, writeBehindService);
}
@Override
public RLock getLock(String name) {
return new RedissonLock(connectionManager.getCommandExecutor(), name);
}
@Override
public RLock getSpinLock(String name) {
return getSpinLock(name, LockOptions.defaults());
}
@Override
public RLock getSpinLock(String name, LockOptions.BackOff backOff) {
return new RedissonSpinLock(connectionManager.getCommandExecutor(), name, backOff);
}
@Override
public RLock getMultiLock(RLock... locks) {
return new RedissonMultiLock(locks);
}
@Override
public RLock getRedLock(RLock... locks) {
return new RedissonRedLock(locks);
}
@Override
public RLock getFairLock(String name) {
return new RedissonFairLock(connectionManager.getCommandExecutor(), name);
}
@Override
public RReadWriteLock getReadWriteLock(String name) {
return new RedissonReadWriteLock(connectionManager.getCommandExecutor(), name);
}
@Override
public RSet getSet(String name) {
return new RedissonSet(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RSet getSet(String name, Codec codec) {
return new RedissonSet(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RScript getScript() {
return new RedissonScript(connectionManager.getCommandExecutor());
}
@Override
public RScript getScript(Codec codec) {
return new RedissonScript(connectionManager.getCommandExecutor(), codec);
}
@Override
public RScheduledExecutorService getExecutorService(String name) {
return getExecutorService(name, connectionManager.getCodec());
}
@Override
public RScheduledExecutorService getExecutorService(String name, ExecutorOptions options) {
return getExecutorService(name, connectionManager.getCodec(), options);
}
@Override
public RScheduledExecutorService getExecutorService(String name, Codec codec) {
return getExecutorService(name, codec, ExecutorOptions.defaults());
}
@Override
public RScheduledExecutorService getExecutorService(String name, Codec codec, ExecutorOptions options) {
return new RedissonExecutorService(codec, connectionManager.getCommandExecutor(), this, name, queueTransferService, responses, options);
}
@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, connectionManager.getCommandExecutor(), executorId, responses);
}
@Override
public RSortedSet getSortedSet(String name) {
return new RedissonSortedSet(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RSortedSet getSortedSet(String name, Codec codec) {
return new RedissonSortedSet(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RScoredSortedSet getScoredSortedSet(String name) {
return new RedissonScoredSortedSet(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RScoredSortedSet getScoredSortedSet(String name, Codec codec) {
return new RedissonScoredSortedSet(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RLexSortedSet getLexSortedSet(String name) {
return new RedissonLexSortedSet(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RTopic getTopic(String name) {
return new RedissonTopic(connectionManager.getCommandExecutor(), name);
}
@Override
public RTopic getTopic(String name, Codec codec) {
return new RedissonTopic(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RReliableTopic getReliableTopic(String name) {
return new RedissonReliableTopic(connectionManager.getCommandExecutor(), name);
}
@Override
public RReliableTopic getReliableTopic(String name, Codec codec) {
return new RedissonReliableTopic(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RPatternTopic getPatternTopic(String pattern) {
return new RedissonPatternTopic(connectionManager.getCommandExecutor(), pattern);
}
@Override
public RPatternTopic getPatternTopic(String pattern, Codec codec) {
return new RedissonPatternTopic(codec, connectionManager.getCommandExecutor(), pattern);
}
@Override
public RDelayedQueue getDelayedQueue(RQueue destinationQueue) {
if (destinationQueue == null) {
throw new NullPointerException();
}
return new RedissonDelayedQueue(queueTransferService, destinationQueue.getCodec(), connectionManager.getCommandExecutor(), destinationQueue.getName());
}
@Override
public RQueue getQueue(String name) {
return new RedissonQueue(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RQueue getQueue(String name, Codec codec) {
return new RedissonQueue(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RTransferQueue getTransferQueue(String name) {
String remoteName = RedissonObject.suffixName(name, "remoteService");
RRemoteService service = getRemoteService(remoteName);
return new RedissonTransferQueue(connectionManager.getCommandExecutor(), name, service);
}
@Override
public RTransferQueue getTransferQueue(String name, Codec codec) {
String remoteName = RedissonObject.suffixName(name, "remoteService");
RRemoteService service = getRemoteService(remoteName);
return new RedissonTransferQueue(codec, connectionManager.getCommandExecutor(), name, service);
}
@Override
public RRingBuffer getRingBuffer(String name) {
return new RedissonRingBuffer(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RRingBuffer getRingBuffer(String name, Codec codec) {
return new RedissonRingBuffer(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBlockingQueue getBlockingQueue(String name) {
return new RedissonBlockingQueue(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBlockingQueue getBlockingQueue(String name, Codec codec) {
return new RedissonBlockingQueue(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBoundedBlockingQueue getBoundedBlockingQueue(String name) {
return new RedissonBoundedBlockingQueue(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBoundedBlockingQueue getBoundedBlockingQueue(String name, Codec codec) {
return new RedissonBoundedBlockingQueue(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RDeque getDeque(String name) {
return new RedissonDeque(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RDeque getDeque(String name, Codec codec) {
return new RedissonDeque(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBlockingDeque getBlockingDeque(String name) {
return new RedissonBlockingDeque(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RBlockingDeque getBlockingDeque(String name, Codec codec) {
return new RedissonBlockingDeque(codec, connectionManager.getCommandExecutor(), name, this);
};
@Override
public RAtomicLong getAtomicLong(String name) {
return new RedissonAtomicLong(connectionManager.getCommandExecutor(), name);
}
@Override
public RLongAdder getLongAdder(String name) {
return new RedissonLongAdder(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RDoubleAdder getDoubleAdder(String name) {
return new RedissonDoubleAdder(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RAtomicDouble getAtomicDouble(String name) {
return new RedissonAtomicDouble(connectionManager.getCommandExecutor(), name);
}
@Override
public RCountDownLatch getCountDownLatch(String name) {
return new RedissonCountDownLatch(connectionManager.getCommandExecutor(), name);
}
@Override
public RBitSet getBitSet(String name) {
return new RedissonBitSet(connectionManager.getCommandExecutor(), name);
}
@Override
public RSemaphore getSemaphore(String name) {
return new RedissonSemaphore(connectionManager.getCommandExecutor(), name);
}
@Override
public RPermitExpirableSemaphore getPermitExpirableSemaphore(String name) {
return new RedissonPermitExpirableSemaphore(connectionManager.getCommandExecutor(), name);
}
@Override
public RBloomFilter getBloomFilter(String name) {
return new RedissonBloomFilter(connectionManager.getCommandExecutor(), name);
}
@Override
public RBloomFilter getBloomFilter(String name, Codec codec) {
return new RedissonBloomFilter(codec, connectionManager.getCommandExecutor(), name);
}
@Override
public RIdGenerator getIdGenerator(String name) {
return new RedissonIdGenerator(connectionManager.getCommandExecutor(), name);
}
@Override
public RKeys getKeys() {
return new RedissonKeys(connectionManager.getCommandExecutor());
}
@Override
public RTransaction createTransaction(TransactionOptions options) {
return new RedissonTransaction(connectionManager.getCommandExecutor(), options);
}
@Override
public RBatch createBatch(BatchOptions options) {
RedissonBatch batch = new RedissonBatch(evictionScheduler, connectionManager, options);
if (config.isReferenceEnabled()) {
batch.enableRedissonReferenceSupport(this);
}
return batch;
}
@Override
public RBatch createBatch() {
return createBatch(BatchOptions.defaults());
}
@Override
public RLiveObjectService getLiveObjectService() {
return new RedissonLiveObjectService(liveObjectClassCache, connectionManager);
}
@Override
public void shutdown() {
connectionManager.shutdown();
}
@Override
public void shutdown(long quietPeriod, long timeout, TimeUnit unit) {
connectionManager.shutdown(quietPeriod, timeout, unit);
}
@Override
public Config getConfig() {
return config;
}
@Override
public T getRedisNodes(org.redisson.api.redisnode.RedisNodes nodes) {
if (nodes.getClazz() == RedisSingle.class) {
if (config.isSentinelConfig() || config.isClusterConfig()) {
throw new IllegalArgumentException("Can't be used in non Redis single configuration");
}
return (T) new RedissonSingleNode(connectionManager);
}
if (nodes.getClazz() == RedisCluster.class) {
if (!config.isClusterConfig()) {
throw new IllegalArgumentException("Can't be used in non Redis Cluster configuration");
}
return (T) new RedissonClusterNodes(connectionManager);
}
if (nodes.getClazz() == RedisSentinelMasterSlave.class) {
if (!config.isSentinelConfig()) {
throw new IllegalArgumentException("Can't be used in non Redis Sentinel configuration");
}
return (T) new RedissonSentinelMasterSlaveNodes(connectionManager);
}
if (nodes.getClazz() == RedisMasterSlave.class) {
if (config.isSentinelConfig() || config.isClusterConfig()) {
throw new IllegalArgumentException("Can't be used in non Redis Master Slave configuration");
}
return (T) new RedissonMasterSlaveNodes(connectionManager);
}
throw new IllegalArgumentException();
}
@Override
public NodesGroup getNodesGroup() {
return new RedisNodes(connectionManager);
}
@Override
public ClusterNodesGroup getClusterNodesGroup() {
if (!connectionManager.isClusterMode()) {
throw new IllegalStateException("Redisson is not in cluster mode!");
}
return new RedisClusterNodes(connectionManager);
}
@Override
public boolean isShutdown() {
return connectionManager.isShutdown();
}
@Override
public boolean isShuttingDown() {
return connectionManager.isShuttingDown();
}
protected void enableRedissonReferenceSupport() {
this.connectionManager.getCommandExecutor().enableRedissonReferenceSupport(this);
}
@Override
public RPriorityQueue getPriorityQueue(String name) {
return new RedissonPriorityQueue(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityQueue getPriorityQueue(String name, Codec codec) {
return new RedissonPriorityQueue(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityBlockingQueue getPriorityBlockingQueue(String name) {
return new RedissonPriorityBlockingQueue(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityBlockingQueue getPriorityBlockingQueue(String name, Codec codec) {
return new RedissonPriorityBlockingQueue(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityBlockingDeque getPriorityBlockingDeque(String name) {
return new RedissonPriorityBlockingDeque(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityBlockingDeque getPriorityBlockingDeque(String name, Codec codec) {
return new RedissonPriorityBlockingDeque(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityDeque getPriorityDeque(String name) {
return new RedissonPriorityDeque(connectionManager.getCommandExecutor(), name, this);
}
@Override
public RPriorityDeque getPriorityDeque(String name, Codec codec) {
return new RedissonPriorityDeque(codec, connectionManager.getCommandExecutor(), name, this);
}
@Override
public String getId() {
return connectionManager.getId();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy