dongyukun
9 天以前 b2f92df3d631bf54c98afa779e2bed49f906721b
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.websocket.core.sender.rabbitmq;
H 2
3 import lombok.RequiredArgsConstructor;
4 import org.springframework.amqp.core.ExchangeTypes;
5 import org.springframework.amqp.rabbit.annotation.*;
6
7 /**
8  * {@link RabbitMQWebSocketMessage} 广播消息的消费者,真正把消息发送出去
9  *
10  * @author iailab
11  */
12 @RabbitListener(
13         bindings = @QueueBinding(
14                 value = @Queue(
15                         // 在 Queue 的名字上,使用 UUID 生成其后缀。这样,启动的 Consumer 的 Queue 不同,以达到广播消费的目的
16                         name = "${iailab.websocket.sender-rabbitmq.queue}" + "-" + "#{T(java.util.UUID).randomUUID()}",
17                         // Consumer 关闭时,该队列就可以被自动删除了
18                         autoDelete = "true"
19                 ),
20                 exchange = @Exchange(
21                         name = "${iailab.websocket.sender-rabbitmq.exchange}",
22                         type = ExchangeTypes.TOPIC,
23                         declare = "false"
24                 )
25         )
26 )
27 @RequiredArgsConstructor
28 public class RabbitMQWebSocketMessageConsumer {
29
30     private final RabbitMQWebSocketMessageSender rabbitMQWebSocketMessageSender;
31
32     @RabbitHandler
33     public void onMessage(RabbitMQWebSocketMessage message) {
34         rabbitMQWebSocketMessageSender.send(message.getSessionId(),
35                 message.getUserType(), message.getUserId(),
36                 message.getMessageType(), message.getMessageContent());
37     }
38
39 }