spring调用
pom.xml配置 核心依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- slf4j日志统一管理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- spring中的redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
消息监听器RedisMessageListener
package redis.listener;
import java.nio.charset.Charset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
public class RedisMessageListener implements MessageListener {
private static final Logger logger = LoggerFactory.getLogger(RedisMessageListener.class);
RedisTemplate redisTemplate;
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public void onMessage(Message message, byte[] pattern) {
// 获取消息
byte[] body = message.getBody();
String receiveMsg = (String) redisTemplate.getValueSerializer().deserialize(body);
logger.debug("body:{}", receiveMsg);
// channel
byte[] channel = message.getChannel();
String channelMsg = (String) redisTemplate.getStringSerializer().deserialize(channel);
logger.debug("channel:{}", channelMsg);
// 渠道pattern
String string = new String(pattern, Charset.defaultCharset());
logger.debug("渠道pattern:{}", string);
}
}
publish-subscribe-redis.xml spring配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-lazy-init="false">
<!-- 占位符配置文件 -->
<!-- <context:property-placeholder location="classpath:redis.properties" /> -->
<!-- redis 连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50" />
<property name="maxTotal" value="100" />
<property name="maxWaitMillis" value="20000" />
</bean>
<!-- Spring-redis连接池工厂配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" />
<property name="timeout" value="2000" />
<property name="poolConfig" ref="poolConfig" />
</bean>
<!-- 序列化 String类型 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!-- 序列化 对象 -->
<!-- <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
<!-- redisTemplate 定义 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
</bean>
<!-- 自定义监听类 -->
<bean id="redisMessageListener" class="redis.listener.RedisMessageListener">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
<bean id="redisMessageListenerContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="taskExecutor">
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="3"/>
</bean>
</property>
<property name="messageListeners">
<map>
<entry key-ref="redisMessageListener">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="topic-123"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
测试代码1
package redis.core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
public class PushTopicTest {
private static final Logger logger = LoggerFactory.getLogger(PushTopicTest.class);
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("redis-conf/string/publish-subscribe-redis.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
redisTemplate.convertAndSend("topic-123", "hhiii,mmmp中国了解了");
}
}