redis发布订阅RedisTemplate实现

  • 进行环境配置
    
      		org.springframework.boot
      		spring-boot-starter
      	
      	
      	
      		org.springframework.boot
      		spring-boot-starter-data-redis
      	
      	
      		org.springframework.boot
      		spring-boot-starter-test
      	
  • 配置redis
    package com.wd.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import redis.clients.jedis.JedisPoolConfig;
    
    @Configuration
    public class RedisConfig {
    	@Bean
    	public JedisPoolConfig jedisPoolConfig(){
    		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    		jedisPoolConfig.setMaxIdle(10);
    		jedisPoolConfig.setMaxTotal(20);
    		jedisPoolConfig.setMinIdle(5);
    		return jedisPoolConfig;
    	}
    	
    	@Bean
    	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig){
    			JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    			jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
    			jedisConnectionFactory.setHostName("192.168.157.135");
    			jedisConnectionFactory.setPort(6379);
    			jedisConnectionFactory.setPassword("redis");
    		   return jedisConnectionFactory;
    	}
    	
    	@Bean
    	public RedisTemplate redisTempLate(JedisConnectionFactory jedisConnectionFactory){
    		RedisTemplate redisTemplate = new RedisTemplate<>();
    		redisTemplate.setConnectionFactory(jedisConnectionFactory);
    		redisTemplate.setKeySerializer(new StringRedisSerializer());
    		redisTemplate.setValueSerializer(new StringRedisSerializer());
    		return redisTemplate;
    	}
    }
    
  • 配置消息监听
  • package com.wd.config;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.MessageListener;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.listener.ChannelTopic;
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
    import org.springframework.data.redis.listener.Topic;
    
    import com.wd.msgListener.RedisMsgListener;
    
    @Configuration
    public class MessageListenerConfig {
    
    	/*
    	 * @Bean public ChannelTopic channelTopic(){ ChannelTopic channelTopic = new
    	 * ChannelTopic("chat2"); return channelTopic; }
    	 */
    	/**
    	 * 订阅多个
    	 * @return
    	 */
    	@Bean 
    	public List channelTopicList(){
    		ChannelTopic channelTopic1 = new ChannelTopic("chat1");
    		ChannelTopic channelTopic2 = new ChannelTopic("chat2");
    		ChannelTopic channelTopic3 = new ChannelTopic("chat3");
    		ArrayList channelTopicList = new ArrayList<>();
    		channelTopicList.add(channelTopic1);
    		channelTopicList.add(channelTopic2);
    		channelTopicList.add(channelTopic3);
    		return channelTopicList;
    	}
    
    	@Bean
    	public RedisMessageListenerContainer redisMessageListenerContainer(JedisConnectionFactory jedisConnectionFactory,RedisMsgListener redisMsgListener,List channelTopicList){
    		RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
    		redisMessageListenerContainer.setConnectionFactory(jedisConnectionFactory);
    //		ArrayList list = new ArrayList();
    //		list.add(channelTopic);
    		//此处使用Map> listeners = new HashMap<>();
    		//总是报错
    		
    		Map> listeners = new HashMap<>();
    		listeners.put(redisMsgListener, channelTopicList);
    		redisMessageListenerContainer.setMessageListeners(listeners);
    		return redisMessageListenerContainer;
    	}
    	
    	/*@Bean
    	public RedisMessageListenerContainer redisMessageListenerContainer(JedisConnectionFactory jedisConnectionFactory,RedisMsgListener redisMsgListener,ChannelTopic channelTopic){
    		RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
    		redisMessageListenerContainer.setConnectionFactory(jedisConnectionFactory);
    		ArrayList list = new ArrayList();
    		list.add(channelTopic);
    		//此处使用Map> listeners = new HashMap<>();
    		//总是报错
    		
    		Map> listeners = new HashMap<>();
    		listeners.put(redisMsgListener, list);
    		redisMessageListenerContainer.setMessageListeners(listeners);
    		return redisMessageListenerContainer;
    	}*/
    }
    
    package com.wd.msgListener;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.connection.Message;
    import org.springframework.data.redis.connection.MessageListener;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.stereotype.Component;
    
    @Component
    public class RedisMsgListener implements MessageListener{
    	
    	@Autowired
    	private RedisTemplate redisTemplate;
    	
    	@Override
    	public void onMessage(Message message, byte[] pattern) {
    		byte[] body = message.getBody();
    		String msgBody = (String) redisTemplate.getValueSerializer().deserialize(body);
    		System.out.println(msgBody);
    		byte[] channel = message.getChannel();
    		String msgChannel = (String) redisTemplate.getValueSerializer().deserialize(channel);
    		System.out.println(msgChannel);
    		String msgPattern = new String(pattern);
    		System.out.println(msgPattern);
    	}
    
    }
    
  • 测试
    package com.wd;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class App {
    	public static void main(String[] args) {
    		SpringApplication.run(App.class, args);
    	}
    }
package com.wd.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.wd.App;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class RedisTest {
	@Autowired
	private RedisTemplate redisTemplate;

	@Test
	public void testSet() {
		String channel = "chat1";
		String msg1 = "hello 发布订阅1";
		redisTemplate.convertAndSend(channel, msg1);
		String channe2 = "chat2";
		String msg2 = "hello 发布订阅2";
		redisTemplate.convertAndSend(channe2, msg2);
		String channe3 = "chat3";
		String msg3 = "hello 发布订阅3";
		redisTemplate.convertAndSend(channe3, msg3);
		
	};	
}

 

你可能感兴趣的:(web开发基础技术学习,Redis发布订阅)