spring-redis实现订阅发布

导入包:
       
            org.springframework.data
            spring-data-redis
            1.0.3.RELEASE
       

生产端代码: 接口省略

package jredis;

import java.io.Serializable;
import org.springframework.data.redis.core.RedisTemplate ;


public class RedisDAOImpl implements RedisDAO{
    private RedisTemplate redisTemplate = null;

    public RedisDAOImpl() {

    }


    @Override
    public void sendMessage(String channel, Serializable message) {
        redisTemplate.convertAndSend(channel, message);
    }


    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
}
客户端代码:用于监听redis
package jredis;

import com.sun.tools.javac.util.List;
import com.sun.xml.internal.xsom.impl.scd.Iterators;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.io.Serializable;
import java.util.Arrays;

public class MessageDelegateListenerImpl implements MessageDelegateListener {

    @Override
    public void handleMessage(Serializable message) {
        //什么都不做,只输出
        if(message == null){
            System.out.println("null");
        } else if(message.getClass().isArray()){
            System.out.println(Arrays.toString((Object[]) message));
        } else if(message instanceof List) {
            System.out.println(message);
        } else if(message instanceof Iterators.Map) {
            System.out.println(message);
        } else {
            System.out.println(ToStringBuilder.reflectionToString(message));
            System.out.println(message);
        }
    }
}

先启消费端,消费端测试类:

package jredis;

import org.junit.Before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;


public class TestRedisConsumer {
    private MessageDelegateListenerImpl messageDelegateListener=null;

    @Before
    public void setUp() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-consumer-test.xml");
        messageDelegateListener = (MessageDelegateListenerImpl) applicationContext.getBean("messageDelegateListener");
    }

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring-consumer-test.xml");
        System.out.println("消费者1");
        while (true) { //这里是一个死循环,目的就是让进程不退出,用于接收发布的消息
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

生产端测试类:

package jredis;

import service.weibo.impl.TencentOauthV1BackgroundServiceImpl;
import service.weibo.impl.TencentOauthV1ForegroundServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestRedisProduce {
    private RedisDAOImpl redisDAO=null;

    @Before
    public void setUp() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-service-test.xml");
        redisDAO = (RedisDAOImpl) applicationContext.getBean("redisDAO");
        }


    @Test
    public void testPublishMessage() throws Exception {
        String msg = "Hello, Redis!";
        redisDAO.sendMessage("java", msg); //发布字符串消息


       Integer[] values = new Integer[]{21341,123123,12323};
        redisDAO.sendMessage("java", values);  //发布一个数组消息
    }
}
生产端xml配置:





    
    

    
    

    
        
    

    

    
    

    
        
          
    

消费端XML配置:




    
    

    
    

    
        
    

    

    

    
        
        
    

    
        
        
            
            
                
                    
                           
                    
                
            
        
    
缺点:

因为spring-redis是基于客户端的,所以如果将客户端进行分布式部署,部署几个实例就有几个实例收到相同的消息。大多数情况我们并不需要如此。我们只需要其中的一个实例处理该条消息即可。这种情况,redis有没有提供处理办法呢?本人还没有找到,欢迎大家提供方案。

你可能感兴趣的:(我的原创,java)