Spring +redis 发布订阅模式填坑记录

一、包

注意版本关系,这里为了不动spring的版本,将spring-data-redis的版本降到1.6.4了,网上推荐jedis2.9.0对应spring-data-redis 1.8.1,但是同时也要考虑spring的版本,否则后续redisTemplate初始化有问题。

二、配置文件

命名空间,注意redis相关的,另外配置文件万不可在网页上复制,各种奇怪问题!!!

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:redis="http://www.springframework.org/schema/redis"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd" default-lazy-init="true">

序列化与spring模板

<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
 <bean id="jsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
 
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
              <property name="connectionFactory" ref="redisConnectionFactory" />
               <property name="keySerializer" ref="stringRedisSerializer" />
              <property name="valueSerializer" ref="jsonRedisSerializer" />
              <property name="hashKeySerializer" ref="stringRedisSerializer" />
              <property name="hashValueSerializer" ref="jsonRedisSerializer" /> 
   </bean> 
 <bean id="redisPuUtil" class="com.**.**.core.util.RedisPuUtil">
 <property name="redisTemplate" ref="redisTemplate" />
 </bean>
   

消息发布工具类

mport org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.serializer.RedisSerializer;
/**
 * redis发布工具类
 * @author ts
 * @version 2019-07-16
 */
public class RedisPuUtil {
	
	 private RedisTemplate<String, Object> redisTemplate = null;

	    
	    public void sendMessage(String channel,  RedisSerializer  message) {
	        redisTemplate.convertAndSend(channel, message);
	    }


	    public RedisTemplate getRedisTemplate() {
	        return redisTemplate;
	    }

	    public void setRedisTemplate(RedisTemplate redisTemplate) {
	        this.redisTemplate = redisTemplate;
	    }
}

订阅者与订阅注册


    <bean id="RedisSendMailListener" class="com.**.**.modules.mail.RedisSendMailListener">
        <property name="jsonRedisSerializer" ref="jsonRedisSerializer" />
    </bean>
   
    <redis:listener-container>
         <redis:listener ref="RedisSendMailListener" serializer="jsonRedisSerializer" method="handleMessage" topic="java" />
    </redis:listener-container>

 发布

RedisPuUtil.sendMessage("java",sendMail);

注意:sendMail实体类必须继承序列化类或实现接口

订阅实现:

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.serializer.RedisSerializer;

public class RedisSendMailListener implements MessageListener {

	@Autowired
	protected SendMail sendMail;
	
	public RedisSerializer<Object> jsonRedisSerializer;
	
	
	public RedisSerializer<Object> getJsonRedisSerializer() {
		return jsonRedisSerializer;
	}


	public void setJsonRedisSerializer(RedisSerializer<Object> jsonRedisSerializer) {
		this.jsonRedisSerializer = jsonRedisSerializer;
	}


	@Override
	public void onMessage(Message message, byte[] arg1) {
		    SendMail object = (SendMail)jsonRedisSerializer.deserialize(message.getBody()); 
		    System.out.println(object.toString());
		
	}
 
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值