redis频道订阅

SpringBoot 项目配置

配置文件

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0

发布者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @className: TestSender
 * @copyright: HTD
 * @description: <一句话功能简述>
 * <功能详细描述>
 * @author: hubin
 * @date: 2018/12/10
 * @version: <version>
 */
@EnableScheduling
@Component
public class TestSender {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //向redis消息队列index通道发布消息
    @Scheduled(fixedRate = 3000)
    public void sendMessage(){
        stringRedisTemplate.convertAndSend("index",String.valueOf("hubin"+Math.random()));
    }
}

订阅者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @className: RedisReceiver
 * @copyright: HTD
 * @description: <一句话功能简述>
 * <功能详细描述>
 * @author: hubin
 * @date: 2018/12/10
 * @version: <version>
 */
@Service
public class RedisReceiver {

    public void receiveMessage(String message) {
        //这里是收到通道的消息之后执行的方法
        System.out.println("消息来了:"+message);
    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SpringMVC项目配置

配置文件

redis.host=localhost
redis.port=6379
redis.pass=
redis.database=0
redis.maxTotal=100
redis.maxIdle=50
redis.minIdle=2
redis.maxWaitMillis=3000
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000
redis.testOnBorrow=true
redis.testOnReturn=true
redis.testWhileIdle=true
redis.timeout=2000

spring.xml配置

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

<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:config.properties" />

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" />
	<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  p:database="${redis.database}"
		p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
	
	 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
		<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
		<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
		<property name="testWhileIdle" value="${redis.testWhileIdle}" />
    </bean>
	
	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
       <constructor-arg index="0" ref="jedisPoolConfig"/>
       <constructor-arg index="1" value="${redis.host}"/>
       <constructor-arg index="2" value="${redis.port}" type="int"/>
       <constructor-arg index="3" value="${redis.timeout}" type="int"/>
    </bean>
	
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory">
		<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.JdkSerializationRedisSerializer" />
		</property>
		<property name="hashValueSerializer">
			<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		</property>
	</bean>

	<bean id="stringTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="connectionFactory">
	</bean>


	<!-- 消息监听适配器  delegate属性指定真正的目标处理器-->
	<bean id="smsMessageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
		<property name="delegate" ref="redisMessageDelegateListener" />
	</bean>
	<bean id="redisMessageDelegateListener" class="com.htd.boss.subscriber.RedisDwrReceiver"/>
	<!-- 消息监听适配器对应的监听容器 -->
	<redis:listener-container connection-factory="connectionFactory">
		<redis:listener ref="smsMessageListener" method="handleMessage" topic="dwrChannel" />
	</redis:listener-container>


	<!-- 发布者-->
	<bean id="resdisDwrPublish" class="com.htd.boss.subscriber.ResdisDwrPublish">
	</bean>
</beans>

发布者ResdisDwrPublish.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @className: ResdisPublish
 * @copyright: HTD
 * @description:redis 发布者
 * dwr的web推送插件消息发布
 * @author: hubin
 * @date: 2018/12/10
 * @version: <version>
 */
public class ResdisDwrPublish {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /* *
     * @description:  发布信息
     * 向推送频道发送信息
     * @author hubin
     * @Param  [message]
     * @return void [返回类型说明]
     * @exception  [违例类型] [违例说明]
     * @version  [版本号, 2018/12/11]
     */
    public void sendMessage(String message) {
        redisTemplate.convertAndSend("dwrChannel", message);
    }





}

订阅者RedisDwrReceiver

package com.htd.boss.subscriber;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @className: RedisReceiver
 * @copyright: HTD
 * @description: redis 订阅者
 * dwr的web推送插件消息监听
 * @author: hubin
 * @date: 2018/12/10
 * @version: <version>
 */
public class RedisDwrReceiver  {


    /* *
     * @description: 监听信息
     * 订阅推送频道信息
     * @author hubin
     * @Param  [message]
     * @return void [返回类型说明]
     * @exception  [违例类型] [违例说明]
     * @version  [版本号, 2018/12/11]
     */
    public void  handleMessage(String message){

        System.out.println("监听到的消息为:"+message);

    }


}

猜你喜欢

转载自blog.csdn.net/xueyunzi1/article/details/84957398