You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "nandorsoma (via GitHub)" <gi...@apache.org> on 2023/03/13 00:36:16 UTC

[GitHub] [nifi] nandorsoma opened a new pull request, #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

nandorsoma opened a new pull request, #7032:
URL: https://github.com/apache/nifi/pull/7032

   <!-- Licensed to the Apache Software Foundation (ASF) under one or more -->
   <!-- contributor license agreements.  See the NOTICE file distributed with -->
   <!-- this work for additional information regarding copyright ownership. -->
   <!-- The ASF licenses this file to You under the Apache License, Version 2.0 -->
   <!-- (the "License"); you may not use this file except in compliance with -->
   <!-- the License.  You may obtain a copy of the License at -->
   <!--     http://www.apache.org/licenses/LICENSE-2.0 -->
   <!-- Unless required by applicable law or agreed to in writing, software -->
   <!-- distributed under the License is distributed on an "AS IS" BASIS, -->
   <!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
   <!-- See the License for the specific language governing permissions and -->
   <!-- limitations under the License. -->
   
   # Summary
   
   [NIFI-11270](https://issues.apache.org/jira/browse/NIFI-11270)
   
   # Tracking
   
   Please complete the following tracking steps prior to pull request creation.
   
   ### Issue Tracking
   
   - [x] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
   
   ### Pull Request Tracking
   
   - [x] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
   - [x] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
   
   ### Pull Request Formatting
   
   - [x] Pull Request based on current revision of the `main` branch
   - [x] Pull Request refers to a feature branch with one commit containing changes
   
   # Verification
   
   Please indicate the verification steps performed prior to pull request creation.
   
   ### Build
   
   - [x] Build completed using `mvn clean install -P contrib-check`
     - [x] JDK 11
     - [ ] JDK 17
   
   ### Licensing
   
   - [ ] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
   - [ ] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
   
   ### Documentation
   
   - [ ] Documentation formatting appears as expected in rendered files
   


-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] Lehel44 commented on a diff in pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "Lehel44 (via GitHub)" <gi...@apache.org>.
Lehel44 commented on code in PR #7032:
URL: https://github.com/apache/nifi/pull/7032#discussion_r1139467597


##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/adapters/PahoMqttClientAdapter.java:
##########
@@ -189,4 +172,53 @@ private static org.eclipse.paho.client.mqttv3.MqttClient createClient(URI broker
         }
     }
 
+    /**
+     * Paho API uses the same callback for the publisher and consumer as well.
+     * Because of that, DefaultMqttCallback sets some reasonable default logs
+     * to make it easier to track misconfiguration errors.
+     * <p>
+     * In case of subscribing clients messageArrived needs to be overridden.
+     */
+    private class DefaultMqttCallback implements MqttCallback {
+
+        @Override
+        public void connectionLost(Throwable cause) {
+            logger.error("Connection to {} lost", clientProperties.getRawBrokerUris(), cause);
+        }
+
+        @Override
+        public void messageArrived(String topic, MqttMessage message) {
+            logger.error(String.format("MQTT message arrived { topic:%s; payload:%s}", topic, Arrays.toString(message.getPayload())));

Review Comment:
   I think this is supposed to be a debug log.



##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/common/MqttClient.java:
##########
@@ -57,13 +57,7 @@ public interface MqttClient {
      *            published at a lower quality of service will be received at the published
      *            QoS. Messages published at a higher quality of service will be received using
      *            the QoS specified on the subscribe.
+     * @param handler that further processes message received by the client

Review Comment:
   typo: messages or the message



##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/ConsumeMQTT.java:
##########
@@ -383,9 +383,8 @@ private void initializeClient(ProcessContext context) {
         // non-null but not connected, so we need to handle each case and only create a new client when it is null
         try {
             mqttClient = createMqttClient();
-            mqttClient.setCallback(this);
             mqttClient.connect();
-            mqttClient.subscribe(topicPrefix + topicFilter, qos);
+            mqttClient.subscribe(topicPrefix + topicFilter, qos, this);

Review Comment:
   It's uneasy to follow the code here. A processor class being a callback itself is strange. What do you think of implementing the interface as an anonymous class instead of passing 'this'?



-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] nandorsoma commented on a diff in pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "nandorsoma (via GitHub)" <gi...@apache.org>.
nandorsoma commented on code in PR #7032:
URL: https://github.com/apache/nifi/pull/7032#discussion_r1143366825


##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/ConsumeMQTT.java:
##########
@@ -383,9 +383,8 @@ private void initializeClient(ProcessContext context) {
         // non-null but not connected, so we need to handle each case and only create a new client when it is null
         try {
             mqttClient = createMqttClient();
-            mqttClient.setCallback(this);
             mqttClient.connect();
-            mqttClient.subscribe(topicPrefix + topicFilter, qos);
+            mqttClient.subscribe(topicPrefix + topicFilter, qos, this);

Review Comment:
   It was a consequence of the original implementation, but you are right. We no longer need to implement the interface on the processor.



-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] asfgit closed pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "asfgit (via GitHub)" <gi...@apache.org>.
asfgit closed pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface
URL: https://github.com/apache/nifi/pull/7032


-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] nandorsoma commented on pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "nandorsoma (via GitHub)" <gi...@apache.org>.
nandorsoma commented on PR #7032:
URL: https://github.com/apache/nifi/pull/7032#issuecomment-1477940590

   Thanks for your review @Lehel44! As you requested, I've addressed your comments and added a description to the Jira ticket!


-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] nandorsoma commented on pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "nandorsoma (via GitHub)" <gi...@apache.org>.
nandorsoma commented on PR #7032:
URL: https://github.com/apache/nifi/pull/7032#issuecomment-1477939848

   Thanks for your review! As you requested, I've addressed your comments and added a description to the Jira ticket!


-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] nandorsoma commented on a diff in pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "nandorsoma (via GitHub)" <gi...@apache.org>.
nandorsoma commented on code in PR #7032:
URL: https://github.com/apache/nifi/pull/7032#discussion_r1143140131


##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/adapters/PahoMqttClientAdapter.java:
##########
@@ -189,4 +172,53 @@ private static org.eclipse.paho.client.mqttv3.MqttClient createClient(URI broker
         }
     }
 
+    /**
+     * Paho API uses the same callback for the publisher and consumer as well.
+     * Because of that, DefaultMqttCallback sets some reasonable default logs
+     * to make it easier to track misconfiguration errors.
+     * <p>
+     * In case of subscribing clients messageArrived needs to be overridden.
+     */
+    private class DefaultMqttCallback implements MqttCallback {
+
+        @Override
+        public void connectionLost(Throwable cause) {
+            logger.error("Connection to {} lost", clientProperties.getRawBrokerUris(), cause);
+        }
+
+        @Override
+        public void messageArrived(String topic, MqttMessage message) {
+            logger.error(String.format("MQTT message arrived { topic:%s; payload:%s}", topic, Arrays.toString(message.getPayload())));

Review Comment:
   In the original code, a comment implied that it was an unlikely situation. The main issue is that the Paho library uses the same interface regardless of whether the client is a subscriber or publisher. In the case of the publisher, this callback doesn't make sense. That's why it is logged with an error. But I see it is confusing, so I'll add that comment back.



-- 
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: issues-unsubscribe@nifi.apache.org

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


[GitHub] [nifi] Lehel44 commented on a diff in pull request #7032: NIFI-11270 Refactoring of the overly Paho-specific MQTT interface

Posted by "Lehel44 (via GitHub)" <gi...@apache.org>.
Lehel44 commented on code in PR #7032:
URL: https://github.com/apache/nifi/pull/7032#discussion_r1139477771


##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/ConsumeMQTT.java:
##########
@@ -383,9 +383,8 @@ private void initializeClient(ProcessContext context) {
         // non-null but not connected, so we need to handle each case and only create a new client when it is null
         try {
             mqttClient = createMqttClient();
-            mqttClient.setCallback(this);
             mqttClient.connect();
-            mqttClient.subscribe(topicPrefix + topicFilter, qos);
+            mqttClient.subscribe(topicPrefix + topicFilter, qos, this);

Review Comment:
   It's uneasy to follow the code here. A processor class being a callback itself is strange. What do you think of implementing the interface with a lambda or as a class instead of passing 'this'?



-- 
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: issues-unsubscribe@nifi.apache.org

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