nandorsoma commented on code in PR #6225:
URL: https://github.com/apache/nifi/pull/6225#discussion_r953196811


##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/common/AbstractMQTTProcessor.java:
##########
@@ -289,88 +288,35 @@ public Collection<ValidationResult> customValidate(final 
ValidationContext valid
         return results;
     }
 
-    public static Properties transformSSLContextService(SSLContextService 
sslContextService){
-        Properties properties = new Properties();
-        if (sslContextService.getSslAlgorithm() != null) {
-            properties.setProperty("com.ibm.ssl.protocol", 
sslContextService.getSslAlgorithm());
-        }
-        if (sslContextService.getKeyStoreFile() != null) {
-            properties.setProperty("com.ibm.ssl.keyStore", 
sslContextService.getKeyStoreFile());
-        }
-        if (sslContextService.getKeyStorePassword() != null) {
-            properties.setProperty("com.ibm.ssl.keyStorePassword", 
sslContextService.getKeyStorePassword());
-        }
-        if (sslContextService.getKeyStoreType() != null) {
-            properties.setProperty("com.ibm.ssl.keyStoreType", 
sslContextService.getKeyStoreType());
-        }
-        if (sslContextService.getTrustStoreFile() != null) {
-            properties.setProperty("com.ibm.ssl.trustStore", 
sslContextService.getTrustStoreFile());
-        }
-        if (sslContextService.getTrustStorePassword() != null) {
-            properties.setProperty("com.ibm.ssl.trustStorePassword", 
sslContextService.getTrustStorePassword());
-        }
-        if (sslContextService.getTrustStoreType() != null) {
-            properties.setProperty("com.ibm.ssl.trustStoreType", 
sslContextService.getTrustStoreType());
-        }
-        return  properties;
-    }
-
-    protected void onScheduled(final ProcessContext context){
-        broker = 
context.getProperty(PROP_BROKER_URI).evaluateAttributeExpressions().getValue();
-        brokerUri = broker.endsWith("/") ? broker : broker + "/";
-        clientID = 
context.getProperty(PROP_CLIENTID).evaluateAttributeExpressions().getValue();
-
-        if (clientID == null) {
-            clientID = UUID.randomUUID().toString();
-        }
-
-        connOpts = new MqttConnectOptions();
-        
connOpts.setCleanSession(context.getProperty(PROP_CLEAN_SESSION).asBoolean());
-        
connOpts.setKeepAliveInterval(context.getProperty(PROP_KEEP_ALIVE_INTERVAL).asInteger());
-        
connOpts.setMqttVersion(context.getProperty(PROP_MQTT_VERSION).asInteger());
-        
connOpts.setConnectionTimeout(context.getProperty(PROP_CONN_TIMEOUT).asInteger());
-
-        PropertyValue sslProp = context.getProperty(PROP_SSL_CONTEXT_SERVICE);
-        if (sslProp.isSet()) {
-            Properties sslProps = 
transformSSLContextService((SSLContextService) sslProp.asControllerService());
-            connOpts.setSSLProperties(sslProps);
-        }
-
-        PropertyValue lastWillTopicProp = 
context.getProperty(PROP_LAST_WILL_TOPIC);
-        if (lastWillTopicProp.isSet()){
-            String lastWillMessage = 
context.getProperty(PROP_LAST_WILL_MESSAGE).getValue();
-            PropertyValue lastWillRetain = 
context.getProperty(PROP_LAST_WILL_RETAIN);
-            Integer lastWillQOS = 
context.getProperty(PROP_LAST_WILL_QOS).asInteger();
-            connOpts.setWill(lastWillTopicProp.getValue(), 
lastWillMessage.getBytes(), lastWillQOS, lastWillRetain.isSet() ? 
lastWillRetain.asBoolean() : false);
-        }
-
-
-        PropertyValue usernameProp = context.getProperty(PROP_USERNAME);
-        if(usernameProp.isSet()) {
-            
connOpts.setUserName(usernameProp.evaluateAttributeExpressions().getValue());
-            
connOpts.setPassword(context.getProperty(PROP_PASSWORD).getValue().toCharArray());
-        }
+    protected void onScheduled(final ProcessContext context) {
+        clientProperties = getMqttClientProperties(context);
+        connectionProperties = getMqttConnectionProperties(context);
     }
 
     protected void onStopped() {
-        try {
-            logger.info("Disconnecting client");
-            mqttClient.disconnect(DISCONNECT_TIMEOUT);
-        } catch(MqttException me) {
-            logger.error("Error disconnecting MQTT client due to {}", new 
Object[]{me.getMessage()}, me);
-        }
+        // Since client is created in the onTrigger method it can happen that 
it never will be created because of an initialization error.
+        // We are preventing additional nullPtrException here, but the clean 
solution would be to create the client in the onScheduled method.
+        if (mqttClient != null) {
+            try {
+                logger.info("Disconnecting client");
+                mqttClient.disconnect(DISCONNECT_TIMEOUT);
+            } catch (MqttException me) {
+                logger.error("Error disconnecting MQTT client due to {}", new 
Object[]{me.getMessage()}, me);
+            }
+
+            try {
+                logger.info("Closing client");
+                mqttClient.close();
+            } catch (MqttException me) {
+                logger.error("Error closing MQTT client due to {}", new 
Object[]{me.getMessage()}, me);
+            }

Review Comment:
   Yes, I've tested with v3 then switching to v5 and it worked because Paho 
adapter rethrows any exception as MqttException. But this is not true for v5. 
Thanks for noticing it. Changed!



-- 
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-unsubscr...@nifi.apache.org

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

Reply via email to