package com.ld.igds.protocol.zldz.mq; import com.ld.igds.io.constant.ProtocolEnum; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; import java.util.concurrent.CountDownLatch; /** * 消息订阅配置 */ @Configuration public class RedisSubConfig { /** * 创建连接工厂 * * @param connectionFactory * @return */ @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter adapter1, MessageListenerAdapter adapter2) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); //监听对应的channel container.addMessageListener(adapter1, new PatternTopic(ProtocolEnum.TCP_ZLDZ_GRAIN_V1.getCode())); //监听对应的channel container.addMessageListener(adapter2, new PatternTopic(ProtocolEnum.TCP_ZLDZ_GRAIN_V1.getCode() + "_LOGIN")); return container; } /** * 绑定消息监听者和接收监听的方法 * * @param receiver * @return */ @Bean public MessageListenerAdapter adapter1(ReceiverServer receiver) { return new MessageListenerAdapter(receiver, "onMessage"); } /** * 绑定消息监听者和接收监听的方法 * * @param receiver * @return */ @Bean public MessageListenerAdapter adapter2(ReceiverServer receiver) { return new MessageListenerAdapter(receiver, "onLoginMessage"); } @Bean ReceiverServer receiver(CountDownLatch latch) { return new ReceiverServer(latch); } @Bean public CountDownLatch latch() { return new CountDownLatch(1);//指定了计数的次数 1 } }