jedis调用redis之pubsub

package com.alex.xiyoubang;

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

public class JedisPubSubDemo {

    public static JedisPool pool;

    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(1);
        config.setMaxTotal(20);
        config.setMaxWaitMillis(6000);
        pool = new JedisPool(config, "192.168.1.171", 6379);
    }

    public static void main(String[] args) {
        Jedis redisClient1 = pool.getResource();
        Jedis redisClient2 = pool.getResource();
        MyListener listener = new MyListener();
        //发布消息渠道
        Publisher pub = new Publisher();
        //发布一个频道
        pub.publish(redisClient2); 
        //订阅消息
        Subscriber sub = new Subscriber();
        // 订阅一个频道
        sub.psub(redisClient1, listener);
    }
}

//发布消息渠道
class Publisher {
    public void publish(final Jedis redisClient) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.currentThread();
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("发布:com.alex.xiyoubang");
                //发布消息
                redisClient.publish("com.alex.xiyoubang", "悟空");
                redisClient.publish("com.alex.xiyoubang", "八戒");
            }
        }).start();
    }
}

//接收消息渠道
class Subscriber {
    public void psub(final Jedis redisClient, final MyListener listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                System.out.println("订阅:com.alex.xiyoubang");
                // 订阅得到信息在lister的onMessage(...)方法中进行处理
                // 订阅多个频道
                redisClient.psubscribe(listener, new String[]{"com.alex.xiyoubang"});// 使用模式匹配的方式设置频道
            }
        }).start();
    }
}

class MyListener extends JedisPubSub {

    // 取得订阅的消息后的处理
    public void onMessage(String channel, String message) {
        System.out.println(channel + "=" + message);
    }

    // 初始化订阅时候的处理
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(channel + "=" + subscribedChannels);
    }

    // 取消订阅时候的处理
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println(channel + "=" + subscribedChannels);
    }

    // 初始化按表达式的方式订阅时候的处理
    public void onPSubscribe(String pattern, int subscribedChannels) {
        System.out.println(pattern + "=" + subscribedChannels);
    }

    // 取消按表达式的方式订阅时候的处理
    public void onPUnsubscribe(String pattern, int subscribedChannels) {
        System.out.println(pattern + "=" + subscribedChannels);
    }


    // 取得按表达式的方式订阅的消息后的处理
    public void onPMessage(String pattern, String channel, String message) {
        System.out.println(pattern + "=" + channel + "=" + message);
    }
}

你可能感兴趣的:(redis,jedis,Class,subscribe,publish)