* @param data String to be transmitted to the Mqtt Broker.
*/
public void sendData(String data) {
Assert.hasText(data, "data must not be empty nor null");
DefaultMqttPahoClientFactory factory;
MqttClient client = null;
factory = new DefaultMqttPahoClientFactory();
factory.setPassword("guest");
factory.setUserName("foobar");
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setPayload(data.getBytes());
try {
client = factory.getClientInstance("tcp://" + host + ":" + port, "guest");
client.connect();
client.publish("xd.mqtt.test", mqttMessage);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
client.disconnect();
client.close();
}
catch (MqttException mqttException) {
throw new IllegalStateException(mqttException.getMessage());
}
}