Redis 訂閱 Java實現

##轉載請標明出處 http://coderknock.com ######源碼:http://git.oschina.net/sanchan/SparkJsonRedis ####消息publish端java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 拿客 www.coderknock.com
 * 微信公衆號 coderknock
 * 做者:三產
 */
public class TestRedis {
    public static void main(String[] args) {
        //獲取redis鏈接池
        JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
        try (Jedis jedis = pool.getResource()) {
            // jedis.set("foo", "bar");
            // String foobar = jedis.get("foo");
            // jedis.zadd("sose", 0, "car");
            // jedis.zadd("sose", 0, "bike");
            // Set<String> sose = jedis.zrange("sose", 0, -1);
            // System.out.println("");
            // 發佈
            String publishName = "testPublish";
            int i = 1;
            while (true) {
                try {
                    Thread.currentThread().sleep(1000);
                    i++;
                    System.out.println(jedis.publish(publishName, i + ""));
                    if (i == 999) {
                        break;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            pool.destroy();
        }
    }
}

#####消息訂閱端git

/**
 * 拿客 www.coderknock.com
 * 微信公衆號 coderknock
 * 做者:三產
 */

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 拿客 www.coderknock.com
 * 微信公衆號 coderknock
 * 做者:三產
 */
public class TestJedisPubSub {
    public static void main(String[] args) {
        //獲取redis鏈接池
        JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
        try (Jedis jedis = pool.getResource()) {
            jedis.subscribe(new PubListener(), "testPublish");
        }
        pool.destroy();
    }
}

#####訂閱端監聽器redis

import redis.clients.jedis.JedisPubSub;

/**
 * 拿客 www.coderknock.com
 * 微信公衆號 coderknock
 * 做者:三產
 */
public class PubListener extends JedisPubSub {
    // 取得訂閱的消息後的處理
    public void onMessage(String channel, String message) {
        System.out.println("取得訂閱的消息後的處理");
        System.out.println(channel + "=" + message);
    }

    // 初始化訂閱時候的處理
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("初始化訂閱時候的處理");
        System.out.println(channel + "=" + subscribedChannels);
    }

    // 取消訂閱時候的處理
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println("取消訂閱時候的處理");
        System.out.println(channel + "=" + subscribedChannels);
    }

    // 初始化按表達式的方式訂閱時候的處理
    public void onPSubscribe(String pattern, int subscribedChannels) {
        System.out.println("初始化按表達式的方式訂閱時候的處理");
        System.out.println(pattern + "=" + subscribedChannels);
    }

    // 取消按表達式的方式訂閱時候的處理
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
        System.out.println("取消按表達式的方式訂閱時候的處理");
        System.out.println(pattern + "=" + subscribedChannels);
    }

    // 取得按表達式的方式訂閱的消息後的處理
    public void onPMessage(String pattern, String channel, String message) {
        System.out.println("取得按表達式的方式訂閱的消息後的處理");
        System.out.println(pattern + "=" + channel + "=" + message);
    }
}

####Redis訂閱者與發佈者: Redis訂閱者與發佈者 ####訂閱監聽: 訂閱監聽微信

相關文章
相關標籤/搜索