Redisでのサブスクリプションとパブリケーション


package cn.yc.com.JedisPubSub;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import parquet.org.codehaus.jackson.JsonNode;
import parquet.org.codehaus.jackson.map.ObjectMapper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
//  
public class Program {

    public static final String CHANNEL_NAME = "msg:text:html";

    private static Logger logger = LoggerFactory.getLogger(Program.class);

    public static void main(String[] args) throws Exception {

        JedisPoolConfig poolConfig = new JedisPoolConfig();

        JedisPool jedisPool = new JedisPool(poolConfig, "IP", 6379, 0);

        final Jedis subscriberJedis = jedisPool.getResource();

        final Subscriber subscriber = new Subscriber();

        new Thread(new Runnable() {

            public void run() {
                try {
                    logger.info("Subscribing to \"commonChannel\". This thread will be blocked.");
                    subscriberJedis.subscribe(subscriber, CHANNEL_NAME);
                    logger.info("Subscription ended.");
                } catch (Exception e) {
                    logger.error("Subscribing failed.", e);
                }
            }
        }).start();

        Jedis publisherJedis = jedisPool.getResource();

        new Publisher(publisherJedis, CHANNEL_NAME).start();

        subscriber.unsubscribe();
        jedisPool.returnResource(subscriberJedis);
        jedisPool.returnResource(publisherJedis);

    }

}
package cn.yc.com.JedisPubSub;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Publisher {

    private static final Logger logger = LoggerFactory.getLogger(Publisher.class);

    private final Jedis publisherJedis;

    private final String channel;

    public Publisher(Jedis publisherJedis, String channel) {
        this.publisherJedis = publisherJedis;
        this.channel = channel;
    }

    public void start() {
        logger.info("Type your message (quit for terminate)");
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            while (true) {
                String line = reader.readLine();

                if (!"quit".equals(line)) {
                    publisherJedis.publish(channel, line);
                } else {
                    break;
                }
            }

        } catch (IOException e) {
            logger.error("IO failure while reading input, e");
        }
    }
}
package cn.yc.com.JedisPubSub;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.JedisPubSub;

public class Subscriber extends JedisPubSub {

    private static Logger logger = LoggerFactory.getLogger(Subscriber.class);

    //             
    public void onMessage(String channel, String message) {  
        logger.info("            : " + channel + "=" + message);  

    }  

    //                    
    public void onPMessage(String pattern, String channel, String message) {  
        logger.info("                   :" + pattern + "=" + channel + "=" + message);  
    }  

    //            
    public void onSubscribe(String channel, int subscribedChannels) {  
        logger.info("           : " + channel + "=" + subscribedChannels);  
    }  

    //           
    public void onUnsubscribe(String channel, int subscribedChannels) {  
        logger.info("          : " + channel + "=" + subscribedChannels);  
    }  

    //                   
    public void onPSubscribe(String pattern, int subscribedChannels) {  
        logger.info("                  : " + pattern + "=" + subscribedChannels);  
    }  

    //                  
    public void onPUnsubscribe(String pattern, int subscribedChannels) {  
        logger.info("                  : " + pattern + "=" + subscribedChannels);  
    }  

}