turcsanyip commented on code in PR #6225:
URL: https://github.com/apache/nifi/pull/6225#discussion_r953140061
##########
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:
All exceptions should be caught here to ensure `mqttClient = null` to be
executed in all cases.
I can still run into the issue that `mqttClient` is not cleared properly and
therefore it will not be reinitalized.
--
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]