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

maven 依赖包

spring-data-redis实现redis发布订阅_第1张图片

	<properties>
		<spring.version>5.1.0.RELEASEspring.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>junitgroupId>
			<artifactId>junitartifactId>
			<version>3.8.1version>
			<scope>testscope>
		dependency>

		<dependency>
			<groupId>redis.clientsgroupId>
			<artifactId>jedisartifactId>
			<version>2.9.0version>
		dependency>

		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-contextartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-coreartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-beansartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aopartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aspectsartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframework.datagroupId>
			<artifactId>spring-data-redisartifactId>
			<version>2.1.0.RELEASEversion>
		dependency>
	dependencies>

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);
}

你可能感兴趣的:(redis)