Created
July 5, 2018 07:33
-
-
Save gkesharwani/a3601fc1fe53be1c43f4609c218d4830 to your computer and use it in GitHub Desktop.
MQTT Publisher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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