<?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: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">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--生存时间(单位/秒) 自动过期-->
<!-- <property name="expire" value="${redis.expire}" /> -->
<!--最大空闲数:空闲链接数大于maxIdle时,将进行回收 -->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--最大连接数:能够同时建立的“最大链接个数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!--最大等待时间:单位:毫秒 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--使用连接时,检测连接是否成功 -->
<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:database="${redis.dbIndex}" p:password="${redis.auth}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
</beans>
java
package com.cc.login.service.impl;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.Any;
import com.oztf.common.netty.NettyChannel;
import com.oztf.common.protobuf.BaseProto;
import com.oztf.common.protobuf.CommonProto;
import com.oztf.common.protobuf.UserProto;
import com.oztf.user.model.UserFormalModel;
/**
*
* 消息队列 消费者
*/
@Component
public class ConsumerMessageListener implements MessageListener {
private static final String USER_CHANNEL = "userChannel";
private static final String COMMON_CHANNEL = "commonChannel";
private static final InternalLogger LOG = InternalLoggerFactory
.getInstance(ConsumerMessageListener.class);
@Autowired
private StringRedisTemplate redisTemplate;
public void onMessage(Message msg, byte[] arg1) {
byte[] body = msg.getBody();
byte[] channel = msg.getChannel();
BaseProto.Base.Builder rs = BaseProto.Base.newBuilder();
rs.setCode(1015);
rs.setCommand(1202);
String topic = (String) redisTemplate.getStringSerializer().deserialize(channel);
UserFormalModel formalModel = null;
String str = null;
if (topic.equals(USER_CHANNEL)) {
// 推送用户信息
str = (String) redisTemplate.getStringSerializer().deserialize(body);
ObjectMapper mapper = new ObjectMapper();
try {
formalModel = mapper.readValue(str, UserFormalModel.class);
} catch (Exception e) {
LOG.error("消息列队转换失败。");
}
UserProto.UserInfoRs.Builder userBuilder = UserProto.UserInfoRs.newBuilder();
userBuilder.setUserId(formalModel.getUserId());
userBuilder.setLevel(formalModel.getLevel());
userBuilder.setGold(formalModel.getGold());
userBuilder.setBank(formalModel.getBank());
if (null != formalModel) {
rs.setExtany(Any.pack(userBuilder.build()));
NettyChannel.getInstance().pushMessage(rs.build(), formalModel.getUserId());
LOG.info("指定消息推送:" + formalModel.getUserId());
}
} else if (topic.equals(COMMON_CHANNEL)) {
LOG.info("公共消息推送:" + new Date());
// 推送公共信息
CommonProto.CommonMessage.Builder conBuilder = CommonProto.CommonMessage.newBuilder();
str = (String) redisTemplate.getStringSerializer().deserialize(body);
conBuilder.setMsg(str);
rs.setExtany(Any.pack(conBuilder.build()));
NettyChannel.getInstance().pushMessage(rs.build());
}
}
/**
* 消息监听适配器
*
* @param receiver
* @return
*/
@Bean
public MessageListenerAdapter getMessageListenerAdapter(ConsumerMessageListener receiver) {
return new MessageListenerAdapter(receiver);
}
/**
* 创建消息监听容器
*
* @param redisConnectionFactory
* @param messageListenerAdapter
* @return
*/
@Bean
public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic("*Channel"));
return redisMessageListenerContainer;
}
}
生产者
@Autowired
private StringRedisTemplate redisTemplate;
public void send(final String channel, final String msg) {
redisTemplate.convertAndSend(channel, msg);
}