diff --git a/docs/src/reference/docbook/reference/redis.xml b/docs/src/reference/docbook/reference/redis.xml
index 365da3407..dd07b9c17 100644
--- a/docs/src/reference/docbook/reference/redis.xml
+++ b/docs/src/reference/docbook/reference/redis.xml
@@ -222,6 +222,8 @@ public RedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory(sentinelConfig);
}
]]>
+ Sometimes direct interaction with the one of the Sentinels is required. Using RedisConnectionFactory.getSentinelConnection() or
+ RedisConnection.getSentinelCommands() gives you access to the first active Sentinel configured.
diff --git a/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java
new file mode 100644
index 000000000..c7e324ab7
--- /dev/null
+++ b/src/main/java/org/springframework/data/redis/connection/AbstractRedisConnection.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2014 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 org.springframework.data.redis.connection;
+
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.springframework.dao.DataAccessException;
+import org.springframework.dao.InvalidDataAccessApiUsageException;
+import org.springframework.dao.InvalidDataAccessResourceUsageException;
+import org.springframework.data.redis.RedisSystemException;
+
+/**
+ * @author Christoph Strobl
+ * @since 1.4
+ */
+public abstract class AbstractRedisConnection implements RedisConnection {
+
+ private RedisSentinelConfiguration sentinelConfiguration;
+ private ConcurrentHashMap connectionCache = new ConcurrentHashMap();
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.connection.RedisConnection#getSentinelCommands()
+ */
+ @Override
+ public RedisSentinelConnection getSentinelConnection() {
+
+ if (!hasRedisSentinelConfigured()) {
+ throw new InvalidDataAccessResourceUsageException("No sentinels configured.");
+ }
+
+ RedisNode node = selectActiveSentinel();
+ RedisSentinelConnection connection = connectionCache.get(node);
+ if (connection == null || !connection.isOpen()) {
+ connection = getSentinelConnection(node);
+ connectionCache.putIfAbsent(node, connection);
+ }
+ return connection;
+ }
+
+ public void setSentinelConfiguration(RedisSentinelConfiguration sentinelConfiguration) {
+ this.sentinelConfiguration = sentinelConfiguration;
+ }
+
+ public boolean hasRedisSentinelConfigured() {
+ return this.sentinelConfiguration != null;
+ }
+
+ private RedisNode selectActiveSentinel() {
+
+ for (RedisNode node : this.sentinelConfiguration.getSentinels()) {
+ if (isActive(node)) {
+ return node;
+ }
+ }
+
+ throw new InvalidDataAccessApiUsageException("Could not find any active sentinels");
+ }
+
+ /**
+ * Check if node is active by sending ping.
+ *
+ * @param node
+ * @return
+ */
+ protected boolean isActive(RedisNode node) {
+ return false;
+ }
+
+ /**
+ * Get {@link RedisSentinelCommands} connected to given node.
+ *
+ * @param sentinel
+ * @return
+ */
+ protected RedisSentinelConnection getSentinelConnection(RedisNode sentinel) {
+ throw new UnsupportedOperationException("Sentinel is not supported by this client.");
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.connection.RedisConnection#close()
+ */
+ @Override
+ public void close() throws DataAccessException {
+
+ if (!connectionCache.isEmpty()) {
+ for (RedisNode node : connectionCache.keySet()) {
+ RedisSentinelConnection connection = connectionCache.remove(node);
+ if (connection.isOpen()) {
+ try {
+ connection.close();
+ } catch (IOException e) {
+ throw new RedisSystemException("Failed to close sentinel connection", e);
+ }
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java
index 5c29f4773..cdbdcf611 100644
--- a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java
+++ b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java
@@ -2380,4 +2380,9 @@ public class DefaultStringRedisConnection implements StringRedisConnection {
new TupleConverter());
}
+ @Override
+ public RedisSentinelConnection getSentinelConnection() {
+ return delegate.getSentinelConnection();
+ }
+
}
diff --git a/src/main/java/org/springframework/data/redis/connection/RedisConnection.java b/src/main/java/org/springframework/data/redis/connection/RedisConnection.java
index a445696dd..2171ca5dd 100644
--- a/src/main/java/org/springframework/data/redis/connection/RedisConnection.java
+++ b/src/main/java/org/springframework/data/redis/connection/RedisConnection.java
@@ -92,4 +92,10 @@ public interface RedisConnection extends RedisCommands {
* @return the result of the executed commands.
*/
List