redis.clients.jedis.JedisPubSubBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jedis Show documentation
Show all versions of jedis Show documentation
Jedis is a blazingly small and sane Redis java client.
package redis.clients.jedis;
import static redis.clients.jedis.Protocol.ResponseKeyword.*;
import java.util.Arrays;
import java.util.List;
import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.SafeEncoder;
public abstract class JedisPubSubBase {
private int subscribedChannels = 0;
private volatile Connection client;
public void onMessage(T channel, T message) {
}
public void onPMessage(T pattern, T channel, T message) {
}
public void onSubscribe(T channel, int subscribedChannels) {
}
public void onUnsubscribe(T channel, int subscribedChannels) {
}
public void onPUnsubscribe(T pattern, int subscribedChannels) {
}
public void onPSubscribe(T pattern, int subscribedChannels) {
}
public void onPong(T pattern) {
}
private void sendAndFlushCommand(Command command, T... args) {
if (client == null) {
throw new JedisException(getClass() + " is not connected to a Connection.");
}
CommandArguments cargs = new CommandArguments(command).addObjects(args);
client.sendCommand(cargs);
client.flush();
}
public final void unsubscribe() {
sendAndFlushCommand(Command.UNSUBSCRIBE);
}
public final void unsubscribe(T... channels) {
sendAndFlushCommand(Command.UNSUBSCRIBE, channels);
}
public final void subscribe(T... channels) {
sendAndFlushCommand(Command.SUBSCRIBE, channels);
}
public final void psubscribe(T... patterns) {
sendAndFlushCommand(Command.PSUBSCRIBE, patterns);
}
public final void punsubscribe() {
sendAndFlushCommand(Command.PUNSUBSCRIBE);
}
public final void punsubscribe(T... patterns) {
sendAndFlushCommand(Command.PUNSUBSCRIBE, patterns);
}
public final void ping() {
sendAndFlushCommand(Command.PING);
}
public final void ping(T argument) {
sendAndFlushCommand(Command.PING, argument);
}
public final boolean isSubscribed() {
return subscribedChannels > 0;
}
public final int getSubscribedChannels() {
return subscribedChannels;
}
public final void proceed(Connection client, T... channels) {
this.client = client;
this.client.setTimeoutInfinite();
try {
subscribe(channels);
process();
} finally {
this.client.rollbackTimeout();
}
}
public final void proceedWithPatterns(Connection client, T... patterns) {
this.client = client;
this.client.setTimeoutInfinite();
try {
psubscribe(patterns);
process();
} finally {
this.client.rollbackTimeout();
}
}
protected abstract T encode(byte[] raw);
// private void process(Client client) {
private void process() {
do {
Object reply = client.getUnflushedObject();
if (reply instanceof List) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy