You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2026/07/09 18:20:48 UTC

Re: [PR] [feat][mqtt] Support dynamic MQTT topic via topicProperty config [pulsar-connectors]

Copilot commented on code in PR #34:
URL: https://github.com/apache/pulsar-connectors/pull/34#discussion_r3553863935


##########
mqtt/src/main/java/org/apache/pulsar/io/mqtt/MqttSink.java:
##########
@@ -106,16 +107,25 @@ public void write(Record<byte[]> record) {
                             record.ack();
                         } else {
                             record.fail();
-                            log.warn("Failed to publish message to MQTT topic {}",
-                                    mqttSinkConfig.getTopic(), throwable);
+                            log.warn("Failed to publish message to MQTT topic {}", topic, throwable);
                         }
                     });
         } catch (Exception e) {
             record.fail();
-            log.warn("Failed to schedule MQTT publish for topic {}", mqttSinkConfig.getTopic(), e);
+            log.warn("Failed to schedule MQTT publish for topic {}", topic, e);
         }
     }
 
+    String resolveTopic(Record<byte[]> record) {
+        String defaultTopic = mqttSinkConfig.getTopic();
+        String topicProperty = mqttSinkConfig.getTopicProperty();
+        if (StringUtils.isBlank(topicProperty)) {
+            return defaultTopic;
+        }
+        String dynamicTopic = record.getProperties().get(topicProperty);
+        return StringUtils.isNotBlank(dynamicTopic) ? dynamicTopic : defaultTopic;
+    }

Review Comment:
   resolveTopic() assumes record.getProperties() is non-null. Other connectors in this repo defensively handle null properties (e.g., kinesis Utils.serializeRecordToJson checks for null), so a Record implementation that returns null here will throw NPE before the write() try/catch runs, skipping record.fail() and dropping the intended fallback-to-default behavior.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org