czt
2025-06-12 930d29f39d115fe76c305af4320c2acbcb30c445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
    }
 
}