package com.dashitech.redis.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

/**
 * Redis发布和订阅
 * Created by CX on 2018/8/20 0020.
 */
public class RedisPubSubCmd {
    private static Log log = LogFactory.getLog(RedisPubSubCmd.class);

    public static void main(String[] args) {
        JedisPoolFactory.getDefaultJedis();

    }

    public static boolean subscribe(JedisPubSub jedisPubSub, String topic) {
        final Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.subscribe(jedisPubSub, topic);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            jedis.close();
        }
        return true;
    }


    public static void publish(String topic, String message) {
        Jedis jedis = JedisPoolFactory.getJedis();
        try {
            jedis.publish(topic, message);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            jedis.close();
        }
    }


}
