redis实现消息发布和订阅

本文介绍了一个基于 Spring 和 Redis 实现的消息队列系统。该系统包括发布者发送消息到指定通道、配置消费者监听特定通道消息并进行处理的过程。文章详细展示了发布消息的方法、Redis 连接池配置、消费者配置及消息处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、发布者代码

	/**
	 * 发布消息
	 * 
	 * @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

 

转载于:https://my.oschina.net/fengshuzi/blog/715181

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值