潘志宝
8 天以前 f3de04db06bae67537d093017e28863ee685f8a3
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
package com.iailab.framework.websocket.core.sender.rocketmq;
 
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
 
/**
 * {@link RocketMQWebSocketMessage} 广播消息的消费者,真正把消息发送出去
 *
 * @author iailab
 */
@RocketMQMessageListener( // 重点:添加 @RocketMQMessageListener 注解,声明消费的 topic
        topic = "${iailab.websocket.sender-rocketmq.topic}",
        consumerGroup = "${iailab.websocket.sender-rocketmq.consumer-group}",
        messageModel = MessageModel.BROADCASTING // 设置为广播模式,保证每个实例都能收到消息
)
@RequiredArgsConstructor
public class RocketMQWebSocketMessageConsumer implements RocketMQListener<RocketMQWebSocketMessage> {
 
    private final RocketMQWebSocketMessageSender rocketMQWebSocketMessageSender;
 
    @Override
    public void onMessage(RocketMQWebSocketMessage message) {
        rocketMQWebSocketMessageSender.send(message.getSessionId(),
                message.getUserType(), message.getUserId(),
                message.getMessageType(), message.getMessageContent());
    }
 
}