com.github.lontime.extredisson.provider.AbstractRedissonProvider Maven / Gradle / Ivy
package com.github.lontime.extredisson.provider;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import com.github.lontime.base.commonj.components.AbstractComponentLifecycle;
import com.github.lontime.base.commonj.components.ComponentShutdownPriority;
import com.github.lontime.base.commonj.constants.Consts;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.base.commonj.utils.CommonManager;
import com.github.lontime.base.commonj.utils.LoggerHelper;
import com.github.lontime.base.commonj.utils.StringHelper;
import com.github.lontime.extredisson.configuration.ConnectionOption;
import com.github.lontime.extredisson.configuration.OptionResolver;
import com.github.lontime.extredisson.service.ServiceContainer;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
/**
* AbstractRedissonProvider.
*
* @author lontime
* @since 1.0
*/
public abstract class AbstractRedissonProvider extends AbstractComponentLifecycle implements RedissonProvider {
protected final ServiceContainer container;
public AbstractRedissonProvider() {
this.container = new ServiceContainer();
}
@Override
public void initialize() {
OptionResolver.getInstance().getConnections().stream().forEach(this::loadIfAbsent);
this.container.initialize();
}
@Override
public void afterRunning() {
this.container.start();
}
@Override
public void stop() {
this.container.stop();
}
@Override
public void destroy() {
this.container.destroy();
shutdownClient();
}
@Override
public void close() {
reentrantStopAndRemoveHooklet();
}
@Override
public int getShutdownPriority() {
return ComponentShutdownPriority.LIB_LOWEST.getValue();
}
@Override
public String resolve(String name) {
return resolveName(name);
}
/**
* slotKey.
* @return String
*/
abstract public List getKeys();
/**
* loadIfAbsent.
* @param options options
*/
abstract protected void loadIfAbsent(ConnectionOption options);
/**
* shutdownClient.
*/
abstract protected void shutdownClient();
protected RedissonClient load(ConnectionOption options) {
return Redisson.create(config(options));
}
protected String resolveName(String name) {
final String actualName = StringHelper.hasText(name) ? name : Consts.DEFAULT_OBJECT_NAME;
final int index = actualName.lastIndexOf(':');
if (index > -1) {
return actualName.substring(0, index);
}
return actualName;
}
private Config config(ConnectionOption options) {
final Config config = new Config();
Optional.ofNullable(options.getCodec()).map(s -> s.toCodec()).ifPresent(config::setCodec);
Optional.ofNullable(options.getThreads()).ifPresent(config::setThreads);
Optional.ofNullable(options.getNettyThreads()).ifPresent(config::setNettyThreads);
Optional.ofNullable(options.getTransportMode()).ifPresent(config::setTransportMode);
Optional.ofNullable(options.getExecutor())
.map(s -> CommonManager.getInstance().getExecutorService(s))
.ifPresent(config::setExecutor);
Optional.ofNullable(options.getLockWatchdogTimeout()).ifPresent(config::setLockWatchdogTimeout);
Optional.ofNullable(options.getKeepPubSubOrder()).ifPresent(config::setKeepPubSubOrder);
if (options.getCluster() != null) {
final ConnectionOption.Cluster clusterOptions = options.getCluster();
final ClusterServersConfig clusterConfig = config.useClusterServers();
Optional.ofNullable(clusterOptions.getNodeAddresses()).ifPresent(clusterConfig::setNodeAddresses);
Optional.ofNullable(clusterOptions.getScanInterval()).ifPresent(clusterConfig::setScanInterval);
Optional.ofNullable(clusterOptions.getCheckSlotsCoverage()).ifPresent(clusterConfig::setCheckSlotsCoverage);
//------------
Optional.ofNullable(options.getClientName()).ifPresent(clusterConfig::setClientName);
Optional.ofNullable(options.getUsername()).ifPresent(clusterConfig::setUsername);
Optional.ofNullable(options.getPassword()).ifPresent(clusterConfig::setPassword);
//TIME
Optional.ofNullable(options.getPingConnectionInterval()).ifPresent(clusterConfig::setPingConnectionInterval);
Optional.ofNullable(options.getKeepAlive()).ifPresent(clusterConfig::setKeepAlive);
Optional.ofNullable(options.getTcpNoDelay()).ifPresent(clusterConfig::setTcpNoDelay);
Optional.ofNullable(options.getIdleConnectionTimeout()).ifPresent(clusterConfig::setIdleConnectionTimeout);
Optional.ofNullable(options.getConnectTimeout()).ifPresent(clusterConfig::setConnectTimeout);
Optional.ofNullable(options.getTimeout()).ifPresent(clusterConfig::setTimeout);
Optional.ofNullable(options.getRetryAttempts()).ifPresent(clusterConfig::setRetryAttempts);
Optional.ofNullable(options.getRetryInterval()).ifPresent(clusterConfig::setRetryInterval);
Optional.ofNullable(options.getDnsMonitoringInterval()).ifPresent(clusterConfig::setDnsMonitoringInterval);
//Subscription
Optional.ofNullable(options.getSubscriptionMode()).ifPresent(clusterConfig::setSubscriptionMode);
Optional.ofNullable(options.getSubscriptionsPerConnection()).ifPresent(clusterConfig::setSubscriptionsPerConnection);
Optional.ofNullable(options.getSubscriptionConnectionMinimumIdleSize()).ifPresent(clusterConfig::setSubscriptionConnectionMinimumIdleSize);
Optional.ofNullable(options.getSubscriptionConnectionPoolSize()).ifPresent(clusterConfig::setSubscriptionConnectionPoolSize);
//Multi
Optional.ofNullable(options.getLoadBalancer()).ifPresent(clusterConfig::setLoadBalancer);
Optional.ofNullable(options.getReadMode()).ifPresent
(clusterConfig::setReadMode);
Optional.ofNullable(options.getFailedSlaveReconnectionInterval()).ifPresent(clusterConfig::setFailedSlaveReconnectionInterval);
Optional.ofNullable(options.getFailedSlaveCheckInterval()).ifPresent(clusterConfig::setFailedSlaveCheckInterval);
Optional.ofNullable(options.getSlaveConnectionPoolSize()).ifPresent(clusterConfig::setSlaveConnectionPoolSize);
Optional.ofNullable(options.getSlaveConnectionMinimumIdleSize()).ifPresent(clusterConfig::setSlaveConnectionMinimumIdleSize);
Optional.ofNullable(options.getMasterConnectionMinimumIdleSize()).ifPresent(clusterConfig::setMasterConnectionMinimumIdleSize);
Optional.ofNullable(options.getMasterConnectionPoolSize()).ifPresent(clusterConfig::setMasterConnectionPoolSize);
//SSL
Optional.ofNullable(options.getSslEnableEndpointIdentification()).ifPresent(clusterConfig::setSslEnableEndpointIdentification);
Optional.ofNullable(options.getSslProtocols())
.map(p -> p.toArray(new String[0]))
.ifPresent(clusterConfig::setSslProtocols);
Optional.ofNullable(options.getSslProvider()).ifPresent(clusterConfig::setSslProvider);
Optional.ofNullable(options.getSslTruststore()).ifPresent(clusterConfig::setSslTruststore);
Optional.ofNullable(options.getSslTruststorePassword()).ifPresent(clusterConfig::setSslTruststorePassword);
Optional.ofNullable(options.getSslKeystore()).ifPresent(clusterConfig::setSslKeystore);
Optional.ofNullable(options.getSslKeystorePassword()).ifPresent(clusterConfig::setSslKeystorePassword);
} else if (options.getSentinel() != null) {
final ConnectionOption.Sentinel sentinelOptions = options.getSentinel();
final SentinelServersConfig sentinelConfig = config.useSentinelServers();
Optional.ofNullable(sentinelOptions.getMasterName()).ifPresent(sentinelConfig::setMasterName);
Optional.ofNullable(sentinelOptions.getSentinelAddress()).ifPresent(sentinelConfig::setSentinelAddresses);
Optional.ofNullable(sentinelOptions.getSentinelPassword()).ifPresent(sentinelConfig::setSentinelPassword);
Optional.ofNullable(sentinelOptions.getScanInterval()).ifPresent(sentinelConfig::setScanInterval);
Optional.ofNullable(sentinelOptions.getCheckSentinelsList()).ifPresent(sentinelConfig::setCheckSentinelsList);
Optional.ofNullable(sentinelOptions.getCheckSlaveStatusWithSyncing()).ifPresent(sentinelConfig::setCheckSlaveStatusWithSyncing);
Optional.ofNullable(sentinelOptions.getSentinelsDiscovery()).ifPresent(sentinelConfig::setSentinelsDiscovery);
//------------
Optional.ofNullable(options.getClientName()).ifPresent(sentinelConfig::setClientName);
Optional.ofNullable(options.getUsername()).ifPresent(sentinelConfig::setUsername);
Optional.ofNullable(options.getPassword()).ifPresent(sentinelConfig::setPassword);
Optional.ofNullable(options.getDatabase()).ifPresent(sentinelConfig::setDatabase);
//TIME
Optional.ofNullable(options.getPingConnectionInterval()).ifPresent(sentinelConfig::setPingConnectionInterval);
Optional.ofNullable(options.getKeepAlive()).ifPresent(sentinelConfig::setKeepAlive);
Optional.ofNullable(options.getTcpNoDelay()).ifPresent(sentinelConfig::setTcpNoDelay);
Optional.ofNullable(options.getIdleConnectionTimeout()).ifPresent(sentinelConfig::setIdleConnectionTimeout);
Optional.ofNullable(options.getConnectTimeout()).ifPresent(sentinelConfig::setConnectTimeout);
Optional.ofNullable(options.getTimeout()).ifPresent(sentinelConfig::setTimeout);
Optional.ofNullable(options.getRetryAttempts()).ifPresent(sentinelConfig::setRetryAttempts);
Optional.ofNullable(options.getRetryInterval()).ifPresent(sentinelConfig::setRetryInterval);
Optional.ofNullable(options.getDnsMonitoringInterval()).ifPresent(sentinelConfig::setDnsMonitoringInterval);
//Subscription
Optional.ofNullable(options.getSubscriptionMode()).ifPresent(sentinelConfig::setSubscriptionMode);
Optional.ofNullable(options.getSubscriptionsPerConnection()).ifPresent(sentinelConfig::setSubscriptionsPerConnection);
Optional.ofNullable(options.getSubscriptionConnectionMinimumIdleSize()).ifPresent(sentinelConfig::setSubscriptionConnectionMinimumIdleSize);
Optional.ofNullable(options.getSubscriptionConnectionPoolSize()).ifPresent(sentinelConfig::setSubscriptionConnectionPoolSize);
//Multi
Optional.ofNullable(options.getLoadBalancer()).ifPresent(sentinelConfig::setLoadBalancer);
Optional.ofNullable(options.getReadMode()).ifPresent
(sentinelConfig::setReadMode);
Optional.ofNullable(options.getFailedSlaveReconnectionInterval()).ifPresent(sentinelConfig::setFailedSlaveReconnectionInterval);
Optional.ofNullable(options.getFailedSlaveCheckInterval()).ifPresent(sentinelConfig::setFailedSlaveCheckInterval);
Optional.ofNullable(options.getSlaveConnectionPoolSize()).ifPresent(sentinelConfig::setSlaveConnectionPoolSize);
Optional.ofNullable(options.getSlaveConnectionMinimumIdleSize()).ifPresent(sentinelConfig::setSlaveConnectionMinimumIdleSize);
Optional.ofNullable(options.getMasterConnectionMinimumIdleSize()).ifPresent(sentinelConfig::setMasterConnectionMinimumIdleSize);
Optional.ofNullable(options.getMasterConnectionPoolSize()).ifPresent(sentinelConfig::setMasterConnectionPoolSize);
//SSL
Optional.ofNullable(options.getSslEnableEndpointIdentification()).ifPresent(sentinelConfig::setSslEnableEndpointIdentification);
Optional.ofNullable(options.getSslProtocols())
.map(p -> p.toArray(new String[0]))
.ifPresent(sentinelConfig::setSslProtocols);
Optional.ofNullable(options.getSslProvider()).ifPresent(sentinelConfig::setSslProvider);
Optional.ofNullable(options.getSslTruststore()).ifPresent(sentinelConfig::setSslTruststore);
Optional.ofNullable(options.getSslTruststorePassword()).ifPresent(sentinelConfig::setSslTruststorePassword);
Optional.ofNullable(options.getSslKeystore()).ifPresent(sentinelConfig::setSslKeystore);
Optional.ofNullable(options.getSslKeystorePassword()).ifPresent(sentinelConfig::setSslKeystorePassword);
} else if (options.getMasterSlave() != null) {
final ConnectionOption.MasterSlave masterSlaveOptions = options.getMasterSlave();
final MasterSlaveServersConfig masterSlaveConfig = config.useMasterSlaveServers();
Optional.ofNullable(masterSlaveOptions.getMasterAddress()).ifPresent(masterSlaveConfig::setMasterAddress);
Optional.ofNullable(masterSlaveOptions.getSlaveAddress()).ifPresent(masterSlaveConfig::setSlaveAddresses);
//------------
Optional.ofNullable(options.getClientName()).ifPresent(masterSlaveConfig::setClientName);
Optional.ofNullable(options.getUsername()).ifPresent(masterSlaveConfig::setUsername);
Optional.ofNullable(options.getPassword()).ifPresent(masterSlaveConfig::setPassword);
Optional.ofNullable(options.getDatabase()).ifPresent(masterSlaveConfig::setDatabase);
//TIME
Optional.ofNullable(options.getPingConnectionInterval()).ifPresent(masterSlaveConfig::setPingConnectionInterval);
Optional.ofNullable(options.getKeepAlive()).ifPresent(masterSlaveConfig::setKeepAlive);
Optional.ofNullable(options.getTcpNoDelay()).ifPresent(masterSlaveConfig::setTcpNoDelay);
Optional.ofNullable(options.getIdleConnectionTimeout()).ifPresent(masterSlaveConfig::setIdleConnectionTimeout);
Optional.ofNullable(options.getConnectTimeout()).ifPresent(masterSlaveConfig::setConnectTimeout);
Optional.ofNullable(options.getTimeout()).ifPresent(masterSlaveConfig::setTimeout);
Optional.ofNullable(options.getRetryAttempts()).ifPresent(masterSlaveConfig::setRetryAttempts);
Optional.ofNullable(options.getRetryInterval()).ifPresent(masterSlaveConfig::setRetryInterval);
Optional.ofNullable(options.getDnsMonitoringInterval()).ifPresent(masterSlaveConfig::setDnsMonitoringInterval);
//Subscription
Optional.ofNullable(options.getSubscriptionMode()).ifPresent(masterSlaveConfig::setSubscriptionMode);
Optional.ofNullable(options.getSubscriptionsPerConnection()).ifPresent(masterSlaveConfig::setSubscriptionsPerConnection);
Optional.ofNullable(options.getSubscriptionConnectionMinimumIdleSize()).ifPresent(masterSlaveConfig::setSubscriptionConnectionMinimumIdleSize);
Optional.ofNullable(options.getSubscriptionConnectionPoolSize()).ifPresent(masterSlaveConfig::setSubscriptionConnectionPoolSize);
//Multi
Optional.ofNullable(options.getLoadBalancer()).ifPresent(masterSlaveConfig::setLoadBalancer);
Optional.ofNullable(options.getReadMode()).ifPresent
(masterSlaveConfig::setReadMode);
Optional.ofNullable(options.getFailedSlaveReconnectionInterval()).ifPresent(masterSlaveConfig::setFailedSlaveReconnectionInterval);
Optional.ofNullable(options.getFailedSlaveCheckInterval()).ifPresent(masterSlaveConfig::setFailedSlaveCheckInterval);
Optional.ofNullable(options.getSlaveConnectionPoolSize()).ifPresent(masterSlaveConfig::setSlaveConnectionPoolSize);
Optional.ofNullable(options.getSlaveConnectionMinimumIdleSize()).ifPresent(masterSlaveConfig::setSlaveConnectionMinimumIdleSize);
Optional.ofNullable(options.getMasterConnectionMinimumIdleSize()).ifPresent(masterSlaveConfig::setMasterConnectionMinimumIdleSize);
Optional.ofNullable(options.getMasterConnectionPoolSize()).ifPresent(masterSlaveConfig::setMasterConnectionPoolSize);
//SSL
Optional.ofNullable(options.getSslEnableEndpointIdentification()).ifPresent(masterSlaveConfig::setSslEnableEndpointIdentification);
Optional.ofNullable(options.getSslProtocols())
.map(p -> p.toArray(new String[0]))
.ifPresent(masterSlaveConfig::setSslProtocols);
Optional.ofNullable(options.getSslProvider()).ifPresent(masterSlaveConfig::setSslProvider);
Optional.ofNullable(options.getSslTruststore()).ifPresent(masterSlaveConfig::setSslTruststore);
Optional.ofNullable(options.getSslTruststorePassword()).ifPresent(masterSlaveConfig::setSslTruststorePassword);
Optional.ofNullable(options.getSslKeystore()).ifPresent(masterSlaveConfig::setSslKeystore);
Optional.ofNullable(options.getSslKeystorePassword()).ifPresent(masterSlaveConfig::setSslKeystorePassword);
} else if (options.getAddress() != null) {
// final ConnectionOption.Single singleOptions = options.getSingle();
final SingleServerConfig singleConfig = config.useSingleServer();
Optional.ofNullable(options.getAddress()).ifPresent(singleConfig::setAddress);
Optional.ofNullable(options.getConnectionPoolSize()).ifPresent(singleConfig::setConnectionPoolSize);
Optional.ofNullable(options.getConnectionMinimumIdleSize()).ifPresent(singleConfig::setConnectionMinimumIdleSize);
//------------
Optional.ofNullable(options.getClientName()).ifPresent(singleConfig::setClientName);
Optional.ofNullable(options.getUsername()).ifPresent(singleConfig::setUsername);
Optional.ofNullable(options.getPassword()).ifPresent(singleConfig::setPassword);
Optional.ofNullable(options.getDatabase()).ifPresent(singleConfig::setDatabase);
//TIME
Optional.ofNullable(options.getPingConnectionInterval()).ifPresent(singleConfig::setPingConnectionInterval);
Optional.ofNullable(options.getKeepAlive()).ifPresent(singleConfig::setKeepAlive);
Optional.ofNullable(options.getTcpNoDelay()).ifPresent(singleConfig::setTcpNoDelay);
Optional.ofNullable(options.getIdleConnectionTimeout()).ifPresent(singleConfig::setIdleConnectionTimeout);
Optional.ofNullable(options.getConnectTimeout()).ifPresent(singleConfig::setConnectTimeout);
Optional.ofNullable(options.getTimeout()).ifPresent(singleConfig::setTimeout);
Optional.ofNullable(options.getRetryAttempts()).ifPresent(singleConfig::setRetryAttempts);
Optional.ofNullable(options.getRetryInterval()).ifPresent(singleConfig::setRetryInterval);
Optional.ofNullable(options.getDnsMonitoringInterval()).ifPresent(singleConfig::setDnsMonitoringInterval);
//Subscription
Optional.ofNullable(options.getSubscriptionsPerConnection()).ifPresent(singleConfig::setSubscriptionsPerConnection);
Optional.ofNullable(options.getSubscriptionConnectionMinimumIdleSize()).ifPresent(singleConfig::setSubscriptionConnectionMinimumIdleSize);
Optional.ofNullable(options.getSubscriptionConnectionPoolSize()).ifPresent(singleConfig::setSubscriptionConnectionPoolSize);
//SSL
Optional.ofNullable(options.getSslEnableEndpointIdentification()).ifPresent(singleConfig::setSslEnableEndpointIdentification);
Optional.ofNullable(options.getSslProtocols())
.map(p -> p.toArray(new String[0]))
.ifPresent(singleConfig::setSslProtocols);
Optional.ofNullable(options.getSslProvider()).ifPresent(singleConfig::setSslProvider);
Optional.ofNullable(options.getSslTruststore()).ifPresent(singleConfig::setSslTruststore);
Optional.ofNullable(options.getSslTruststorePassword()).ifPresent(singleConfig::setSslTruststorePassword);
Optional.ofNullable(options.getSslKeystore()).ifPresent(singleConfig::setSslKeystore);
Optional.ofNullable(options.getSslKeystorePassword()).ifPresent(singleConfig::setSslKeystorePassword);
}
return config;
}
}