redis.clients.jedis.JedisShardedPubSubBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jedis_preview Show documentation
Show all versions of jedis_preview Show documentation
Jedis is a blazingly small and sane Redis java client.
The newest version!
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;
public abstract class JedisShardedPubSubBase {
private int subscribedChannels = 0;
private volatile Connection client;
public void onSMessage(T channel, T message) {
}
public void onSSubscribe(T channel, int subscribedChannels) {
}
public void onSUnsubscribe(T channel, int subscribedChannels) {
}
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 sunsubscribe() {
sendAndFlushCommand(Command.SUNSUBSCRIBE);
}
public final void sunsubscribe(T... channels) {
sendAndFlushCommand(Command.SUNSUBSCRIBE, channels);
}
public final void ssubscribe(T... channels) {
sendAndFlushCommand(Command.SSUBSCRIBE, channels);
}
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 {
ssubscribe(channels);
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