一、发布者代码
/**
* 发布消息
*
* @param channel
* @param message
* @date 2016年7月19日
*/
public static void sendMessage(String channel, Object message) {
String jsonMessage = "";
if (message.getClass().isAssignableFrom(String.class)) {
jsonMessage = (String) message;
} else {
jsonMessage = JsonUtil.objectToJsonStr(message);
}
stringRedisTemplate.convertAndSend(channel, jsonMessage);
}
二、消费者配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:redis="http://www.springframework.org/schema/redis" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/redis
http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">
<!-- 配置Jredis池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWait" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="poolConfig" ref="jedisPool" />
</bean>
<!-- Jredis客户端实例 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"></property>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"></property>
</bean>
<bean id="messageDelegateListenerImpl" class="com.iflashbuy.index.queue.MessageDelegateListenerImpl"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<redis:listener-container>
<redis:listener ref="messageDelegateListenerImpl"
serializer="stringRedisSerializer" method="handleMessage" topic="iflashbuy_product_syn_channel" />
</redis:listener-container>
</beans>
三、消费者消息
/**
* channel: iflashbuy_product_syn_channel
*
* @author 漂泊者及其影子
* @date 2016年7月19日
*/
public class MessageDelegateListenerImpl implements MessageListener {
@Autowired
@Qualifier("productSolrService")
SolrService productSolrService;
private static final Logger logger = LoggerFactory.getLogger(MessageDelegateListenerImpl.class);
@Override
public void onMessage(Message message) {
try {
if (message instanceof ObjectMessage) {
Serializable m = ((ObjectMessage) message).getObject();
String productIds = (String) m;
productSolrService.updateSolrIndex(productIds);
MessageQueueUtil.sendProductMessage("1592");
}
} catch (Exception e) {
logger.error("消费者更新制定商品索引失败!" + e);
}
}
}
四、redis 发布消息命令
publish channel message