YYYYWD commented on code in PR #131:
URL: https://github.com/apache/rocketmq-mqtt/pull/131#discussion_r990636586


##########
mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/protocol/mqtt/handler/MqttSubscribeHandler.java:
##########
@@ -128,4 +159,63 @@ private MqttSubAckMessage getResponse(MqttSubscribeMessage 
mqttSubscribeMessage)
         return mqttSubAckMessage;
     }
 
+
+    private void sendRetainMessage(ChannelHandlerContext ctx, 
Set<Subscription> subscriptions) throws InterruptedException, 
RemotingException, org.apache.rocketmq.remoting.exception.RemotingException {
+
+        String clientId = ChannelInfo.getClientId(ctx.channel());
+        Session session = 
sessionLoop.getSession(ChannelInfo.getId(ctx.channel()));
+        Set<Subscription> preciseTopics = new HashSet<>();
+        Set<Subscription> wildcardTopics = new HashSet<>();
+
+        for (Subscription subscription : subscriptions) {
+            if (!TopicUtils.isWildCard(subscription.getTopicFilter())) {
+                preciseTopics.add(subscription);
+            } else {
+                wildcardTopics.add(subscription);
+            }
+        }
+
+        for (Subscription subscription : preciseTopics) {
+            CompletableFuture<Message> retainedMessage = 
retainedPersistManager.getRetainedMessage(subscription.getTopicFilter());
+            retainedMessage.whenComplete((msg, throwable) -> {
+                if (msg == null) {
+                    return;
+                }
+                _sendMessage(session, clientId, subscription, msg);
+            });
+        }
+
+        for (Subscription subscription : wildcardTopics) {
+
+            CompletableFuture<ArrayList<Message>> future = 
retainedPersistManager.getMsgsFromTrie(subscription);
+            future.whenComplete((msgsList, throwable) -> {
+                for (Message msg : msgsList) {
+                    if (msg == null) {
+                        return;
+                    }
+                    _sendMessage(session, clientId, subscription, msg);
+                }
+            });
+
+        }
+    }
+
+    private void _sendMessage(Session session, String clientId, Subscription 
subscription, Message message) {
+
+        String payLoad = new String(message.getPayload());
+        if (payLoad.equals(MessageUtil.EMPTYSTRING) && message.isEmpty()) {
+            return;
+        }
+
+        int mqttId = mqttMsgId.nextId(clientId);
+        int qos = min(subscription.getQos(), message.qos());
+        if (qos == 0) {
+            pushAction.write(session, message, mqttId, 0, subscription);
+            pushAction.rollNextByAck(session, mqttId);
+        } else {
+            retryDriver.mountPublish(mqttId, message, subscription.getQos(), 
ChannelInfo.getId(session.getChannel()), subscription);

Review Comment:
   Through experimentation and log observations, everything works fine in this 
case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to