I am publishing the message in the AWS Elasticache cluster.
And there is a subscriber who is listening to the channel. However, the subscriber runs in JBoss cluster.
So if there are three servers in a cluster then there are three listeners and they all will process the message.
How to avoid this situation and only process messages once?
I created a cluster code to publish, you can assume clusterclient works well, as I have tesetd it.
` public StatefulRedisClusterPubSubConnection<String, String> getClusterPubSubConnection() {
clusterClient = this.establishConnection();
//sample codecs can be used in future
//final RedisCodec<String, byte[]> codec = RedisCodec.of(new StringCodec(), new ByteArrayCodec());
//final RedisCodec<String, String> codec = RedisCodec.of(new StringCodec(), new StringCodec());
//return clusterClient.connectPubSub(codec);
return clusterClient.connectPubSub();
}`
publish code
` public void publishToXXXChannel(CacheConstants cacheConstants, String key, String checkoutTimeInSeconds) {
String redisKey = this.getDelimiter().add(cacheConstants.getCacheName()).add(key).toString();
StatefulRedisClusterPubSubConnection<String, String> connection= connectToRedisCluster.getClusterPubSubConnection();
RedisClusterPubSubCommands commands = connection.sync();
commands.publish("EncounterCheckoutTimeChannel",redisKey );
// The may be in seconds
commands.expire(redisKey ,Long.parseLong(checkoutTimeInSeconds));
}`
subscriber code
public void subscribeToXXXChannel StatefulRedisClusterPubSubConnection<String, String> connection = connectToRedisCluster.getClusterPubSubConnection(); connection.addListener(new XXXListener()); connection.sync().subscribe("XXXChannel"); }
I have created a custom listener, not posting the code here.