redis.clients.jedis.JedisPubSub Maven / Gradle / Ivy
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.Keyword.MESSAGE;
import static redis.clients.jedis.Protocol.Keyword.PMESSAGE;
import static redis.clients.jedis.Protocol.Keyword.PSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.PUNSUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.SUBSCRIBE;
import static redis.clients.jedis.Protocol.Keyword.UNSUBSCRIBE;
import java.util.Arrays;
import java.util.List;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.SafeEncoder;
public abstract class JedisPubSub {
private int subscribedChannels = 0;
private Client client;
public abstract void onMessage(String channel, String message);
public abstract void onPMessage(String pattern, String channel,
String message);
public abstract void onSubscribe(String channel, int subscribedChannels);
public abstract void onUnsubscribe(String channel, int subscribedChannels);
public abstract void onPUnsubscribe(String pattern, int subscribedChannels);
public abstract void onPSubscribe(String pattern, int subscribedChannels);
public void unsubscribe() {
if (client == null) {
throw new JedisConnectionException(
"JedisPubSub was not subscribed to a Jedis instance.");
}
client.unsubscribe();
client.flush();
}
public void unsubscribe(String... channels) {
client.unsubscribe(channels);
client.flush();
}
public void subscribe(String... channels) {
client.subscribe(channels);
client.flush();
}
public void psubscribe(String... patterns) {
client.psubscribe(patterns);
client.flush();
}
public void punsubscribe() {
client.punsubscribe();
client.flush();
}
public void punsubscribe(String... patterns) {
client.punsubscribe(patterns);
client.flush();
}
public boolean isSubscribed() {
return subscribedChannels > 0;
}
public void proceedWithPatterns(Client client, String... patterns) {
this.client = client;
client.psubscribe(patterns);
client.flush();
process(client);
}
public void proceed(Client client, String... channels) {
this.client = client;
client.subscribe(channels);
client.flush();
process(client);
}
private void process(Client client) {
do {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy