
com.lambdaworks.redis.cluster.topology.Connections Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lettuce Show documentation
Show all versions of lettuce Show documentation
Advanced and thread-safe Java Redis client for synchronous, asynchronous, and
reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs
and much more.
The newest version!
/*
* Copyright 2011-2016 the original author or authors.
*
* 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 com.lambdaworks.redis.cluster.topology;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.api.StatefulRedisConnection;
import com.lambdaworks.redis.output.StatusOutput;
import com.lambdaworks.redis.protocol.Command;
import com.lambdaworks.redis.protocol.CommandArgs;
import com.lambdaworks.redis.protocol.CommandKeyword;
import com.lambdaworks.redis.protocol.CommandType;
/**
* @author Mark Paluch
*/
class Connections {
private Map> connections = new TreeMap<>(
TopologyComparators.RedisURIComparator.INSTANCE);
public Connections() {
}
private Connections(Map> connections) {
this.connections = connections;
}
/**
* Add a connection for a {@link RedisURI}
*
* @param redisURI
* @param connection
*/
public void addConnection(RedisURI redisURI, StatefulRedisConnection connection) {
connections.put(redisURI, connection);
}
/*
* Initiate {@code CLUSTER NODES} on all connections and return the {@link Requests}.
*
* @return the {@link Requests}.
*/
public Requests requestTopology() {
Requests requests = new Requests();
for (Map.Entry> entry : connections.entrySet()) {
CommandArgs args = new CommandArgs<>(ClusterTopologyRefresh.CODEC).add(CommandKeyword.NODES);
Command command = new Command<>(CommandType.CLUSTER,
new StatusOutput<>(ClusterTopologyRefresh.CODEC), args);
TimedAsyncCommand timedCommand = new TimedAsyncCommand<>(command);
entry.getValue().dispatch(timedCommand);
requests.addRequest(entry.getKey(), timedCommand);
}
return requests;
}
/*
* Initiate {@code CLIENT LIST} on all connections and return the {@link Requests}.
*
* @return the {@link Requests}.
*/
public Requests requestClients() {
Requests requests = new Requests();
for (Map.Entry> entry : connections.entrySet()) {
CommandArgs args = new CommandArgs<>(ClusterTopologyRefresh.CODEC).add(CommandKeyword.LIST);
Command command = new Command<>(CommandType.CLIENT,
new StatusOutput<>(ClusterTopologyRefresh.CODEC), args);
TimedAsyncCommand timedCommand = new TimedAsyncCommand<>(command);
entry.getValue().dispatch(timedCommand);
requests.addRequest(entry.getKey(), timedCommand);
}
return requests;
}
/**
* Close all connections.
*/
public void close() {
for (StatefulRedisConnection connection : connections.values()) {
connection.close();
}
}
/**
*
* @return a set of {@link RedisURI} for which {@link Connections} has a connection.
*/
public Set connectedNodes() {
return connections.keySet();
}
public Connections mergeWith(Connections discoveredConnections) {
Map> result = new TreeMap<>(
TopologyComparators.RedisURIComparator.INSTANCE);
result.putAll(this.connections);
result.putAll(discoveredConnections.connections);
return new Connections(result);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy