1、springmvc-redis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="MaxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" 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="poolConfig">
</bean>
<!-- redis客户端模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!-- 注入连接工厂 -->
<property name="connectionFactory" ref="connectionFactory" />
<!-- 配置key序列化类 -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!-- 配置value序列化类 -->
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<!-- 定义监听容器 -->
<bean id="redisMessageListenerContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 任务执行器 -->
<property name="taskExecutor">
<bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="10"/>
</bean>
</property>
<!-- 消息监听器 -->
<property name="messageListeners">
<map>
<entry key-ref="redisSubscribeDaoImpl">
<list>
<!-- 监听通道匹配 -->
<bean class="org.springframework.data.redis.listener.PatternTopic">
<constructor-arg value="channel*" />
</bean>
</list>
</entry>
</map>
</property>
</bean>
</beans>
2、生产者:
package com.yllt.site.redis.impl;
import com.yllt.site.redis.IRedisPublishDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
/**
*
* @author asus
* @date 2019/8/17
*/
@Repository
public class RedisPublishDaoImpl implements IRedisPublishDao {
private static Logger logger = LoggerFactory.getLogger(RedisPublishDaoImpl.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
static String[] channels = null;
static {
if(channels == null){
channels = new String[]{"channel1", "channel2", "channel3", "channel4", "channel5"};
}
}
@Override
public void publishMessage(String message) {
logger.info("消息发布开始..." + message);
redisTemplate.convertAndSend("channel1", message);
}
}
3、消费者:
package com.yllt.site.redis.impl;
import com.alibaba.fastjson.JSONObject;
import com.yllt.common.util.CollectionUtil;
import com.yllt.site.domain.Camera;
import com.yllt.site.domain.Trajectory;
import com.yllt.site.service.ICameraService;
import com.yllt.site.service.ITrajectoryService;
import com.yllt.site.util.LatLngUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 消息订阅者
* @author asus
* @date 2019/8/17
*/
@Service("redisSubscribeDaoImpl")
public class RedisSubscribeDaoImpl implements MessageListener {
private static Logger logger = LoggerFactory.getLogger(RedisSubscribeDaoImpl.class);
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Override
public void onMessage(Message message, byte[] pattern) {
logger.info("消息订阅开始...");
byte[] body = message.getBody();
String msgBody = (String) redisTemplate.getValueSerializer().deserialize(body);
logger.info("接收到消息内容:{}" + msgBody);
}
}
4、测试:
String json = JSONObject.toJSONString(xxx);
redisPublishDao.publishMessage(json);