java使用redis做发布订阅

配置文件:

package net.rdd.config;

import net.rdd.listener.RddMessageListener;
import net.rdd.mq.MessageReceiver;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Controller;

/**
 * Created by 东东 on 2018/11/18.
 */
@Configuration
public class RedisMqConfig {

    /**
     * redis消息监听器容器
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter,RddMessageListener rddMessageListener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅了test的通道
        //可以多个
        container.addMessageListener(listenerAdapter, new PatternTopic("test"));
//        container.addMessageListener(listenerAdapter, new PatternTopic("test"));
        container.addMessageListener(rddMessageListener, new PatternTopic("test"));

        return container;
    }

    /**
     * 消息监听器适配器
     * @param receiver
     * @return
     */
      @Bean
    MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
        //这个地方 是给messageListenerAdapter 传入一个消息接受的处理器,利用反射的方法调用“receiveMessage”
        //也有好几个重载方法,这边默认调用处理器的方法 叫handleMessage 可以自己到源码里面看
        //这里可以指定方法,也可以不指定方法,重写onMessage方法,如果指定了方法,则按指定的方法执行,如果没有则或执行onMessage方法.
        //这里跟监听redis key过期那差不多,参见我博客里面有
//        return new MessageListenerAdapter(receiver,"receiveMessage");

        return new MessageListenerAdapter(receiver);
    }

    @Bean
    MessageListenerAdapter rddlistenerAdapter(RddMessageListener receiver) {
        return new MessageListenerAdapter(receiver, "onMessage");
    }


}

监听器:

package net.rdd.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Component
public class RddMessageListener extends MessageListenerAdapter {
//public class RedisMessageListener implements MessageListener {

    private final Logger log = LoggerFactory.getLogger(getClass());

	private final RedisSerializer<String> stringSerializer = new StringRedisSerializer();
	
	@Override
	//key过期会执行这个方法
	public void onMessage(Message message, byte[] pattern) {

		String key = stringSerializer.deserialize(message.getBody());

		System.out.println("RddMessageListener"+key);

	}


}

具体发送,通过注入StringRedisTemplate,test是topic,可以设置多个:

   stringRedisTemplate.convertAndSend("test","");

参考:https://blog.csdn.net/u012995888/article/details/79377512
自己整理的源码:https://github.com/stackXu/SpringBootRedis

posted @ 2018-11-18 17:40  你就像甜甜的益达  阅读(159)  评论(0编辑  收藏  举报