use of org.eclipse.paho.client.mqttv3.MqttMessage in project pinpoint by naver.
the class PahoMqttV3ClientIT method methodTests.
@Test
public void methodTests() throws Exception {
MqttMessage v3Message = new MqttMessage(MESSAGE_PAYLOAD.getBytes());
v3Message.setQos(QOS);
IMqttToken deliveryToken = mqttClient.publish(TOPIC, v3Message);
deliveryToken.waitForCompletion(WAIT_FOR_COMPLETION);
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.awaitTraceCount(3, 100, 5000);
verifier.verifyTraceCount(3);
verifier.printCache();
verifyTrace(verifier);
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project syndesis-qe by syndesisio.
the class MqttUtils method sendMessage.
public void sendMessage(String messageContent, String topic) {
final String clientId = "syndesis-mqtt-sender";
MqttClient sampleClient = null;
try {
sampleClient = new MqttClient(BROKER, clientId, new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(false);
Account account = AccountsDirectory.getInstance().get(Account.Name.MQTT);
connOpts.setUserName(account.getProperties().get("userName"));
connOpts.setPassword(account.getProperties().get("password").toCharArray());
sampleClient.connect(connOpts);
MqttMessage message = new MqttMessage(messageContent.getBytes());
message.setQos(1);
sampleClient.publish(topic, message);
System.out.println("Message published from : " + clientId + " with payload of : " + messageContent);
} catch (MqttException e) {
e.printStackTrace();
Assertions.fail("Sending a message should not have thrown any exception.");
} finally {
closeClient(sampleClient);
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project aware-client by denzilferreira.
the class Mqtt method publish.
/**
* Publish message to topic
*
* @param topicName
* @param messageText
*/
public static boolean publish(String topicName, String messageText) {
if (MQTT_CLIENT != null && MQTT_CLIENT.isConnected()) {
try {
MqttMessage message = new MqttMessage();
message.setPayload(messageText.getBytes());
message.setQos(Integer.parseInt(MQTT_QoS));
message.setRetained(true);
MQTT_CLIENT.publish(topicName, message);
} catch (MqttPersistenceException e) {
if (Aware.DEBUG)
Log.e(TAG, e.getMessage());
return false;
} catch (MqttException e) {
if (Aware.DEBUG)
Log.e(TAG, e.getMessage());
return false;
}
}
return true;
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project rocketmq-externals by apache.
the class MqttSampleProducer method main.
public static void main(String[] args) {
String topic = "mqtt-sample";
String messageContent = "hello mqtt";
int qos = 0;
String broker = "tcp://127.0.0.1:1883";
String clientId = "JavaSampleProducer";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
log.info("Connecting to broker: " + broker);
sampleClient.connect(connOpts);
log.info("Connected");
log.info("Publishing message: " + messageContent);
MqttMessage message = new MqttMessage(messageContent.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
log.info("Message published");
sampleClient.disconnect();
log.info("Disconnected");
System.exit(0);
} catch (MqttException me) {
log.error("reason " + me.getReasonCode());
log.error("msg " + me.getMessage());
log.error("loc " + me.getLocalizedMessage());
log.error("cause " + me.getCause());
log.error("excep " + me);
me.printStackTrace();
System.exit(1);
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project transporter by wang4ever.
the class MqttMessagePublisher method invoke.
/**
* 执行发布
*
* @param topic
* @param payload
*/
private void invoke(String topic, byte[] payload) {
try {
// Send message to EMQ
MqttMessage msg = new MqttMessage();
msg.setQos(config.getProducerConfig().getQosVal());
msg.setRetained(config.getProducerConfig().getRetained());
msg.setPayload(payload);
this.mqttProducer.publish(topic, msg);
if (logger.isDebugEnabled())
logger.debug("Mqtt producer encode completed. topic={}", topic);
} catch (Exception e) {
String errmsg = "Mqtt producer encode error. topic=" + topic;
logger.error(errmsg, e);
this.mailService.sendAdminMsg(errmsg, null);
}
}
Aggregations