Class MqttPublisher

java.lang.Object
org.chenile.mqtt.pubsub.MqttPublisher

public class MqttPublisher extends Object
Used to publish MQTT messages. Defaults are picked up from application properties.
  • Field Details

    • logger

      org.slf4j.Logger logger
    • v5Client

      @Autowired private org.eclipse.paho.mqttv5.client.MqttAsyncClient v5Client
    • actionTimeout

      private int actionTimeout
    • qos

      private int qos
    • mqttInfoProvider

      @Autowired MqttInfoProvider mqttInfoProvider
    • retain

      private boolean retain
  • Constructor Details

    • MqttPublisher

      public MqttPublisher()
  • Method Details

    • setActionTimeout

      public void setActionTimeout(int actionTimeout)
    • setQos

      public void setQos(int qos)
    • setRetain

      public void setRetain(boolean retain)
    • publishToOperation

      public void publishToOperation(String service, String operationName, String payload, Map<String,Object> properties) throws Exception
      Send a message to a service and operation. Topic and Qos are computed from MQTTConfig for the service and operation name. Topics might have placeholders that will be substituted from the properties.
      Parameters:
      service - the service that will receive this message
      operationName - the operation within the service that will receive this message
      payload - the payload that needs to be sent
      properties - the headers properties that need to be sent
      Throws:
      Exception - if there is an error in dispatching the message
    • substituteProperties

      private static String substituteProperties(String s, Map<String,Object> properties)
      Substitute the property in the place holder.
      Parameters:
      s - the string with placeholders
      properties - the properties that need to be substituted
      Returns:
      the s
    • publish

      public void publish(String topic, String payload, Map<String,Object> properties) throws org.eclipse.paho.mqttv5.common.MqttPersistenceException, org.eclipse.paho.mqttv5.common.MqttException
      Publish to the topic with the default Qos of the system (as configured in application.yml)
      Parameters:
      topic - the topic to publish to
      payload - the payload
      properties - the user properties that need to be put into the message
      Throws:
      org.eclipse.paho.mqttv5.common.MqttPersistenceException - if there is a problem in persisting the message that needs to be sent
      org.eclipse.paho.mqttv5.common.MqttException - if there is any other exception
    • publish

      public void publish(String topic, int givenQos, String payload, Map<String,Object> properties) throws org.eclipse.paho.mqttv5.common.MqttPersistenceException, org.eclipse.paho.mqttv5.common.MqttException
      Publish to the topic with the specified Qos of the system (as configured in application.yml)
      Parameters:
      topic - the topic to publish to
      payload - the payload
      givenQos - the Qos that needs to be sent. In case this is -1 then system default will be used.
      properties - the user properties that need to be put into the message
      Throws:
      org.eclipse.paho.mqttv5.common.MqttPersistenceException - if there is a problem in persisting the message that needs to be sent
      org.eclipse.paho.mqttv5.common.MqttException - if there is any other exception
    • sendAck

      public void sendAck(org.eclipse.paho.mqttv5.common.MqttMessage message) throws Exception
      Parameters:
      message - the message that needs to be acknowledged
      Throws:
      Exception - if an exception is thrown