public MQTTAdaptorListener( MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String topic, String mqttClientId, InputEventAdaptorListener inputEventAdaptorListener, int tenantId) { this.mqttBrokerConnectionConfiguration = mqttBrokerConnectionConfiguration; this.mqttClientId = mqttClientId; this.cleanSession = mqttBrokerConnectionConfiguration.isCleanSession(); this.keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive(); this.topic = topic; this.eventAdaptorListener = inputEventAdaptorListener; this.tenantId = tenantId; // SORTING messages until the server fetches them String temp_directory = System.getProperty("java.io.tmpdir"); MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(temp_directory); try { // Construct the connection options object that contains connection parameters // such as cleanSession and LWT connectionOptions = new MqttConnectOptions(); connectionOptions.setCleanSession(cleanSession); connectionOptions.setKeepAliveInterval(keepAlive); if (this.mqttBrokerConnectionConfiguration.getBrokerPassword() != null) { connectionOptions.setPassword( this.mqttBrokerConnectionConfiguration.getBrokerPassword().toCharArray()); } if (this.mqttBrokerConnectionConfiguration.getBrokerUsername() != null) { connectionOptions.setUserName(this.mqttBrokerConnectionConfiguration.getBrokerUsername()); } // Construct an MQTT blocking mode client mqttClient = new MqttClient( this.mqttBrokerConnectionConfiguration.getBrokerUrl(), this.mqttClientId, dataStore); // Set this wrapper as the callback handler mqttClient.setCallback(this); } catch (MqttException e) { log.error("Exception occurred while subscribing to MQTT broker" + e); throw new InputEventAdaptorEventProcessingException(e); } catch (Throwable e) { log.error("Exception occurred while subscribing to MQTT broker" + e); throw new InputEventAdaptorEventProcessingException(e); } }
public MQTTAdapterPublisher( MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String topic, String mqttClientId) { this.mqttBrokerConnectionConfiguration = mqttBrokerConnectionConfiguration; this.mqttClientId = mqttClientId; this.cleanSession = mqttBrokerConnectionConfiguration.isCleanSession(); this.keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive(); this.topic = topic; // SORTING messages until the server fetches them String temp_directory = System.getProperty(MQTTEventAdapterConstants.ADAPTER_TEMP_DIRECTORY_NAME); MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(temp_directory); try { // Construct the connection options object that contains connection parameters // such as cleanSession and LWT connectionOptions = new MqttConnectOptions(); connectionOptions.setCleanSession(cleanSession); connectionOptions.setKeepAliveInterval(keepAlive); if (this.mqttBrokerConnectionConfiguration.getBrokerPassword() != null) { connectionOptions.setPassword( this.mqttBrokerConnectionConfiguration.getBrokerPassword().toCharArray()); } if (this.mqttBrokerConnectionConfiguration.getBrokerUsername() != null) { connectionOptions.setUserName(this.mqttBrokerConnectionConfiguration.getBrokerUsername()); } // Construct an MQTT blocking mode client mqttClient = new MqttClient( this.mqttBrokerConnectionConfiguration.getBrokerUrl(), this.mqttClientId, dataStore); mqttClient.connect(connectionOptions); } catch (MqttException e) { log.error( "Error occurred when constructing MQTT client for broker url : " + mqttBrokerConnectionConfiguration.getBrokerUrl()); handleException(e); } }
public MQTTAdapterListener( MQTTBrokerConnectionConfiguration mqttBrokerConnectionConfiguration, String topic, String mqttClientId, InputEventAdapterListener inputEventAdapterListener, int tenantId) { if (mqttClientId == null || mqttClientId.trim().isEmpty()) { mqttClientId = MqttClient.generateClientId(); } this.mqttBrokerConnectionConfiguration = mqttBrokerConnectionConfiguration; this.cleanSession = mqttBrokerConnectionConfiguration.isCleanSession(); int keepAlive = mqttBrokerConnectionConfiguration.getKeepAlive(); this.topic = topic; this.eventAdapterListener = inputEventAdapterListener; this.tenantId = tenantId; // SORTING messages until the server fetches them String temp_directory = System.getProperty("java.io.tmpdir"); MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(temp_directory); try { connectionOptions = new MqttConnectOptions(); connectionOptions.setCleanSession(cleanSession); connectionOptions.setKeepAliveInterval(keepAlive); // Construct an MQTT blocking mode client mqttClient = new MqttClient( this.mqttBrokerConnectionConfiguration.getBrokerUrl(), mqttClientId, dataStore); // Set this wrapper as the callback handler mqttClient.setCallback(this); String contentValidatorClassName = this.mqttBrokerConnectionConfiguration.getContentValidatorClassName(); if (contentValidatorClassName != null && contentValidatorClassName.equals(MQTTEventAdapterConstants.DEFAULT)) { contentValidator = new DefaultContentValidator(); } else if (contentValidatorClassName != null && !contentValidatorClassName.isEmpty()) { try { Class<? extends ContentValidator> contentValidatorClass = Class.forName(contentValidatorClassName).asSubclass(ContentValidator.class); contentValidator = contentValidatorClass.newInstance(); } catch (ClassNotFoundException e) { throw new MQTTContentInitializationException( "Unable to find the class validator: " + contentValidatorClassName, e); } catch (InstantiationException e) { throw new MQTTContentInitializationException( "Unable to create an instance of :" + contentValidatorClassName, e); } catch (IllegalAccessException e) { throw new MQTTContentInitializationException("Access of the instance in not allowed.", e); } } String contentTransformerClassName = this.mqttBrokerConnectionConfiguration.getContentTransformerClassName(); if (contentTransformerClassName != null && contentTransformerClassName.equals(MQTTEventAdapterConstants.DEFAULT)) { contentTransformer = new DefaultContentTransformer(); } else if (contentTransformerClassName != null && !contentTransformerClassName.isEmpty()) { try { Class<? extends ContentTransformer> contentTransformerClass = Class.forName(contentTransformerClassName).asSubclass(ContentTransformer.class); contentTransformer = contentTransformerClass.newInstance(); } catch (ClassNotFoundException e) { throw new MQTTContentInitializationException( "Unable to find the class transfoer: " + contentTransformerClassName, e); } catch (InstantiationException e) { throw new MQTTContentInitializationException( "Unable to create an instance of :" + contentTransformerClassName, e); } catch (IllegalAccessException e) { throw new MQTTContentInitializationException("Access of the instance in not allowed.", e); } } } catch (MqttException e) { log.error( "Exception occurred while subscribing to MQTT broker at " + mqttBrokerConnectionConfiguration.getBrokerUrl()); throw new InputEventAdapterRuntimeException(e); } }