spring-data-redis实现redis发布订阅

maven 依赖包

在这里插入图片描述

	<properties>
		<spring.version>5.1.0.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>

		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>
	</dependencies>

Spring 配置文件

	<!-- spring 注解配置 -->
	<context:annotation-config />
	<context:component-scan base-package="com" />
	<context:property-placeholder location="classpath:config.properties" />
	<aop:aspectj-autoproxy />

	<import resource="spring-redis.xml" />

Spring-redis.xml 配置文件

	<!-- 以后优化查找可配的参数 -->
	<bean id="jedistPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
	</bean>

	<bean id="redisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
		p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"
		p:pool-config-ref="jedistPoolConfig" />


	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="redisConnectionFactory" />
		<property name="defaultSerializer">
			<bean
				class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		</property>
	</bean>


	<bean id="jdkSerializer"
		class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

	<!-- 通用消息处理 -->
	<bean id="messageListenerTopicAll"
		class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
		<constructor-arg>
			<bean class="com.redis.ListenMessageDelegate" />
		</constructor-arg>
		<property name="serializer" ref="jdkSerializer" />
	</bean>


	<bean id="redisContainer"
		class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
		<property name="connectionFactory" ref="redisConnectionFactory" />
		<property name="taskExecutor">
			<bean
				class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
				<property name="poolSize" value="20"></property>
			</bean>
		</property>
		<property name="messageListeners">
			<map>
				<!-- 通用主题 -->
				<entry key-ref="messageListenerTopicAll">
					<list>
						<bean class="org.springframework.data.redis.listener.ChannelTopic">
							<constructor-arg value="topicAll" />
						</bean>
						<bean class="org.springframework.data.redis.listener.ChannelTopic">
							<constructor-arg value="topicLogic" />
						</bean>
					</list>
				</entry>
			</map>
		</property>
	</bean>

Redis 配置

redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

Delegate接口

public interface MessageDelegate {

	public void handleMessage(byte[] message);
}

Delegate实现

public class ListenMessageDelegate implements MessageDelegate {

	public void handleMessage(byte[] message) {
		//....
		System.out.println(new String(message));
	}

}

发布消息

/**
* 注意 message 必须是字节流
*/
public void convertAndSendRsResponse(String channel, Object message) {
	redisTemplate.convertAndSend(channel, message);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值