Skip to content

Instantly share code, notes, and snippets.

@gkesharwani
Created July 5, 2018 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkesharwani/a3601fc1fe53be1c43f4609c218d4830 to your computer and use it in GitHub Desktop.
Save gkesharwani/a3601fc1fe53be1c43f4609c218d4830 to your computer and use it in GitHub Desktop.
MQTT Publisher
/*
* Publisher method: publishing data over MQTT broker (RABBIT_MQ)
*/
public void publishMessage() {
publishThreadMQTT = new Thread(new Runnable() {
@Override
public void run() {
try {
if (mqttAndroidClient != null) {
MqttMessage message = new MqttMessage(queue.takeFirst().getBytes());
if (message != null) {
mqttAndroidClient.publish(topic, message);
onConnection.onPublish();
}
}
} catch (MqttException e) {
onConnection.onPublishFail();
System.err.println("Error Publishing: " + e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
onConnection.onPublishFail();
e.printStackTrace();
}
}
});
publishThreadMQTT.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment