Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2203

How to choose RedisCluster, RedisStandalone and Sentinel Connections based on configuration?

$
0
0

Here is a requirement where i wanted to use either RedisCluster or Redisstandalone or Sentinel connection based on a configuration file. Example: If i specify configType=rediscluster then will use StatefulRedisClusterConnection. If i specify configType=redisstandalone then it will be StatefulRedisConnection.

Following is the code which I have written for achieving it. Is there any better way to achieve than this:

RedisConnectionFactory.java

public class RedisConnectionFactory {    private String redisUri;    private String connectionType;    private StatefulRedisClusterConnection<String, String> connection;    RedisConnectionFactory(String redisUri, String connectionType) {      this.redisUri = redisUri;      this.connectionType = connectionType;    }    public <T> List<T> getConnection() {      List connectionConfig = new ArrayList();        switch (connectionType) {            case "REDIS_CLUSTER":              RedisClusterClient redisClusterClient = RedisClusterClient.create(redisUri);              connectionConfig.add((T) getStateFulRedisClusterConnection(redisClusterClient));              connectionConfig.add(redisClusterClient);              return connectionConfig;            case "REDIS_STANDALONE":              RedisClient redisClient = RedisClient.create(redisUri);              connectionConfig.add((T) getStateFulRedisConnection(redisClient));              connectionConfig.add(redisClient);              return connectionConfig;        }        return null;    }public StatefulRedisClusterConnection<String,String> getStateFulRedisClusterConnection(RedisClusterClient redisClient) {    connection = redisClient.connect();    System.out.println("Connected to Redis cluster client");    return connection;  }public StatefulRedisConnection<String,String> getStateFulRedisConnection(RedisClient redisClient) {    StatefulRedisConnection statefulRedisConnection = redisClient.connect();    System.out.println("Connected to Redis standalone client");    return statefulRedisConnection;  }}

RedisConnectionProvider.java

private static <T> T getRedisConnection(String connectionId) {        String connectionType = "REDIS_CLUSTER"; // this value comes from config.properties file just hardcoded        if(redisConnection == null) {            String uri =  getRedisUriForConnectionId(connectionId); //gets url string from config file            RedisConnectionFactory redisConnectionFactory = new RedisConnectionFactory(uri, connectionType);            List<Object> redisConnectionConfig = redisConnectionFactory.getConnection();            redisConnection = redisConnectionConfig.get(0);        }        return (T) redisConnection;    }

Main.java

class Main {  public static void main(String args[]){       Object redisConnection = RedisConnectionProvider.getConnection(connectionId);        /**         *   Here is the way i m typecasting based on object type         *   IS THERE ANY BETTER WAY TO DO THAN THIS???         */        if (redisConnection  instanceof StatefulRedisConnection) {            ((StatefulRedisConnection) redisConnection).sync().del(setup.job.getStream());            ((StatefulRedisConnection) redisConnection).sync().del(setup.job.getConfigSet());        } else if (redisConnection instanceof StatefulRedisClusterConnection) {            ((StatefulRedisClusterConnection) redisConnection).sync().del(setup.job.getStream());            ((StatefulRedisClusterConnection) redisConnection).sync().del(setup.job.getConfigSet());        }}}

HERE IS MY QUESTION:The problem is StatefulRedisConnection.sync() returns value of type RedisCommands which has del() methodStatefulRedisClusteConnection.sync() returns value of type RedisAdvancedClusterCommands which also has del() method.

How Do i assign to instance variable of generic type something like this:

RedisGenericCommandForAllConnection redisGenericCommands = ??????

so that i can perform redisGenericCommands.del() without worrying about the type of RedisConnection.

Please guide.


Viewing all articles
Browse latest Browse all 2203

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>