Package org.chenile.mqtt.pubsub
Class MqttPublisher
java.lang.Object
org.chenile.mqtt.pubsub.MqttPublisher
Used to publish MQTT messages. Defaults are picked up from application properties.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int(package private) org.slf4j.Logger(package private) MqttInfoProviderprivate intprivate booleanprivate org.eclipse.paho.mqttv5.client.MqttAsyncClient -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPublish to the topic with the specified Qos of the system (as configured in application.yml)voidPublish to the topic with the default Qos of the system (as configured in application.yml)voidpublishToOperation(String service, String operationName, String payload, Map<String, Object> properties) Send a message to a service and operation.voidsendAck(org.eclipse.paho.mqttv5.common.MqttMessage message) voidsetActionTimeout(int actionTimeout) voidsetQos(int qos) voidsetRetain(boolean retain) private static StringsubstituteProperties(String s, Map<String, Object> properties) Substitute the property in the place holder.
-
Field Details
-
logger
org.slf4j.Logger logger -
v5Client
-
actionTimeout
private int actionTimeout -
qos
private int qos -
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 ExceptionSend 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 messageoperationName- the operation within the service that will receive this messagepayload- the payload that needs to be sentproperties- the headers properties that need to be sent- Throws:
Exception- if there is an error in dispatching the message
-
substituteProperties
Substitute the property in the place holder.- Parameters:
s- the string with placeholdersproperties- 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.MqttExceptionPublish to the topic with the default Qos of the system (as configured in application.yml)- Parameters:
topic- the topic to publish topayload- the payloadproperties- 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 sentorg.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.MqttExceptionPublish to the topic with the specified Qos of the system (as configured in application.yml)- Parameters:
topic- the topic to publish topayload- the payloadgivenQos- 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 sentorg.eclipse.paho.mqttv5.common.MqttException- if there is any other exception
-
sendAck
- Parameters:
message- the message that needs to be acknowledged- Throws:
Exception- if an exception is thrown
-