仅ERR(P)SUBSCRIBE /(P)UNSUBSCRIBE / PING / QUIT在此上下文中允许

时间:2016-08-01 18:44:28

标签: redis jedis

我在我的网络应用中使用jdish.publish,在我的桌面应用中使用jedis.subscribe。所以两者都是独立的应用程序。

我有这个pubsub类

public class RedisNewPostListener extends JedisPubSub {

    private final Jedis jedis;
    private final AppInstances appInstances;

    public RedisNewPostListener(AppInstances instances, Jedis jedis) {
        this.jedis = jedis;
        appInstances = instances;
    }

    @Override
    public void onMessage(String channel, String message) {
        String[] pos = message.split("##");
        double lat = Double.parseDouble(pos[0]);
        double lon = Double.parseDouble(pos[1]);

        List<GeoRadiusResponse> members = jedis.georadius("UsersByLocation", lon, lat, GEO_SEARCH_RANGE, GeoUnit.KM);

我称之为

RedisNewPostListener postListener = new RedisNewPostListener(instances, jedis);
jedis.subscribe(postListener, "NewPostArrived");

我收到此错误:

redis.clients.jedis.exceptions.JedisDataException: ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / PING / QUIT allowed in this context
    at redis.clients.jedis.Protocol.processError(Protocol.java:117)
    at redis.clients.jedis.Protocol.process(Protocol.java:151)
    at redis.clients.jedis.Protocol.read(Protocol.java:205)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:297)
    at redis.clients.jedis.Connection.getRawObjectMultiBulkReply(Connection.java:242)
    at redis.clients.jedis.Connection.getObjectMultiBulkReply(Connection.java:248)
    at redis.clients.jedis.Jedis.georadius(Jedis.java:3452)
    at com.app.redis.RedisNewPostListener.onMessage(RedisNewPostListener.java:39)
    at redis.clients.jedis.JedisPubSub.process(JedisPubSub.java:129)
    at redis.clients.jedis.JedisPubSub.proceed(JedisPubSub.java:102)
    at redis.clients.jedis.Jedis.subscribe(Jedis.java:2628)

1 个答案:

答案 0 :(得分:8)

似乎您使用相同的jedis客户端进行订阅和发布。您只需要创建另一个客户端,一个用于订阅,另一个用于发布