利用redis实现消息订阅和推送

2 篇文章 0 订阅
2 篇文章 0 订阅

redis的消息推送可以用在同一项目中,也可用在不同项目中,文章中我们以同一个项目为例:

首先我们需要一个maven 项目,

在pom文件中加入如下配置:


<!-- Redis 配置中心 -->
<profile.redis.ip>10.20.200.21</profile.redis.ip>
<profile.redis.port>6379</profile.redis.port>
<profile.redis.password>wangdai.com</profile.redis.password>
<profile.redis.timeout>15000</profile.redis.timeout>
<profile.redis.maxTotal>1000</profile.redis.maxTotal>
<profile.redis.usePool>true</profile.redis.usePool> <profile.redis.maxIdle>100</profile.redis.maxIdle>
<profile.redis.testOnBorrow>true</profile.redis.testOnBorrow>
<profile.redis.maxWaitMillis>2000</profile.redis.maxWaitMillis>

对应在conf中新建redis.properties文件:

redis.hostName=${profile.redis.ip}
redis.passWord=${profile.redis.password}
redis.port=${profile.redis.port}
redis.timeout=${profile.redis.timeout}
redis.usePool=${profile.redis.usePool}
redis.maxIdle=6
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000
redis.maxIdle=${profile.redis.maxIdle}
redis.maxTotal=${profile.redis.maxTotal}
redis.maxWaitMillis=${profile.redis.maxWaitMillis}
redis.testOnBorrow=${profile.redis.testOnBorrow}

在applicationContext.xml 中加入相关配置

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:conf/jdbc.properties</value>
				<value>classpath:conf/dubbo.properties</value>
				<value>classpath:conf/redis.properties</value>
			</list>
		</property>
	</bean>

新建一个application-redis.xml 的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
	xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:redis="http://www.springframework.org/schema/redis"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
						   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
						   http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>
	<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  

	<bean  id='jedisConnectionFactory' class='org.springframework.data.redis.connection.jedis.JedisConnectionFactory' >
	  <property name="hostName" value="${redis.hostName}"></property>
	  <property name="port" value="${redis.port}"></property>
	  <property name="usePool" value="${redis.usePool}"></property>
	  <property name="password" value="${redis.passWord}"/>
	  <property name="poolConfig" ref="jedisPoolConfig"></property> 
	</bean>
       
     <bean id='redisTemplate' class='org.springframework.data.redis.core.RedisTemplate'>  
         <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->    
        <property name="keySerializer">    
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>    
        <property name="hashKeySerializer">    
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />    
        </property>  
        <property name="valueSerializer">  
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>
     </bean>
	
 	
	 redis发布订阅配置
   <bean id="serialization" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
   
  消息监听适配器  delegate属性指定真正的目标处理器  
    <bean id="smsMessageListener"
        class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
        <property name="delegate" ref="tQMessageDelegateListener" />
        <property name="serializer" ref="serialization" />  
    </bean>
    <bean id="tQMessageDelegateListener" class="com.hz.hzdjr.controller.TQMessageDelegateListener"/>
    消息监听适配器对应的监听容器
     <redis:listener-container  connection-factory="jedisConnectionFactory">
        <redis:listener ref="smsMessageListener" method="handleMessage"
            topic="test" /> 
    </redis:listener-container>
	
	
	
	
</beans>

application-redis.xml 中配置消息发送者的信息 和 监听者的信息。

我们需要创建一个监听处理类:TQMessageDelegateListener

package com.hz.hzdjr.controller;

public class TQMessageDelegateListener  {

    public void  handleMessage(String message){
	
    	System.out.println("监听到的消息为:"+message);
	
	}
}

我们需要创建一个消息发送方的服务:

RedisService:

package com.hz.hzdjr.system.service;

public interface RedisService {
	/*
	 * 设置频道
	 */
	public static final String TQCHANNEL = "channel_message";
	public void sendMessage(String channel, String message);
}

RedisServiceImpl:

package com.hz.hzdjr.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.hz.hzdjr.system.service.RedisService;

@Service
public class RedisServiceImpl implements RedisService {
	@Autowired
	public RedisTemplate<String, Object> redisTemplate;
	@Override
	public void sendMessage(String channel, String message) {
	System.out.println("开始发布消息。。。");
	redisTemplate.convertAndSend(channel, message); 
	System.out.println("发布成功!");
	}
}

此时我们的准备工作就结束了。

我们只需要启动项目后调用redisService.sendMessage();方法发送消息,那么

handleMessage监听方法将会被执行。

注意:消息发送方的channel  必须和监听方的一样。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值