Hi gyus,
I would like to susbribe on the (master sentinel) in order to receive events and message when master dies such as:
- connection down
- connection up
- switch to new master
i have the following code but when i stop master, i am not receiving events or message could someone show me what is wrong in my code:
TOPOLOGY:
+----+
+----+
+----+
| M1 |
| R2 |
| R3 |
| S1 |--
| S2 |
-
-| S3 |
+----+
+----+
+----+
CODE:
// Lettuce
static public RedisClient redisClient;
static public RedisClient sentinelClient;
static public RedisAsyncCommands<String, String> asyncCommands = null;
static public StatefulRedisMasterSlaveConnection<String, String> redisConnection;
// Create Redis-URI
RedisURI redisUri = RedisURI.builder().withSentinel("192.168.158.200", 26379)
.withSentinel("192.168.158.201", 26379).withSentinel("192.168.158.202", 26379)
.withSentinelMasterId("mymaster").build();
// Create Sentinel-Client
sentinelClient = RedisClient.create(redisUri);
// Create Redis-Client
redisClient = RedisClient.create();
redisClient.setOptions(ClientOptions.builder().autoReconnect(true)
// .timeoutOptions(new TimeoutOptions())
/**
* DEFAULT: Accept commands when auto-reconnect is enabled, reject commands when
* auto-reconnect is disabled. ACCEPT_COMMANDS: Accept commands in disconnected
* state. REJECT_COMMANDS: Reject commands in disconnected state.
*/
.disconnectedBehavior(DisconnectedBehavior.REJECT_COMMANDS).pingBeforeActivateConnection(true)
.build());
// Create Redis-Connection
redisConnection = MasterSlave.connect(redisClient, new Utf8StringCodec(), redisUri);
redisConnection.setReadFrom(ReadFrom.MASTER_PREFERRED);
asyncCommands = redisConnection.async();
//The listener which is not working!!!!!!
public static void addSentinelListennerChannel() {
if (sentinelClient == null)
return;
// Subscribe: Sentinel -> check failover
RedisPubSubAdapter<String, String> listener2 = new RedisPubSubAdapter<String, String>() {
@Override
public void message(String channel, String message) {
if (channel.equals("+switch-master")) {
SmppTestingSbb.log.info("sentinel MASTER CHANGED");
}
if (channel.equals("+sdown")) {
SmppTestingSbb.log.info("sentinel MASTER SDOWN");
}
if (channel.equals("+odown")) {
SmppTestingSbb.log.info("sentinel MASTER ODOWN");
}
}
};
StatefulRedisPubSubConnection<String, String> pubsubConn2 = sentinelClient.connectPubSub();
pubsubConn2.addListener(listener2);
RedisPubSubAsyncCommands<String, String> async2 = pubsubConn2.async();
async2.subscribe("+sdown");
async2.subscribe("+odown");
async2.subscribe("+switch-master");
}
Br.