use of org.eclipse.paho.client.mqttv3.MqttMessage in project thingsboard by thingsboard.
the class MqttSslClient method main.
public static void main(String[] args) {
try {
URL ksUrl = Resources.getResource(KEY_STORE_FILE);
File ksFile = new File(ksUrl.toURI());
URL tsUrl = Resources.getResource(KEY_STORE_FILE);
File tsFile = new File(tsUrl.toURI());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore trustStore = KeyStore.getInstance(JKS);
char[] ksPwd = new char[] { 0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x5F, 0x6B, 0x73, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64 };
trustStore.load(new FileInputStream(tsFile), ksPwd);
tmf.init(trustStore);
KeyStore ks = KeyStore.getInstance(JKS);
ks.load(new FileInputStream(ksFile), ksPwd);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
char[] clientPwd = new char[] { 0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x5F, 0x6B, 0x65, 0x79, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64 };
kmf.init(ks, clientPwd);
KeyManager[] km = kmf.getKeyManagers();
TrustManager[] tm = tmf.getTrustManagers();
SSLContext sslContext = SSLContext.getInstance(TLS);
sslContext.init(km, tm, null);
MqttConnectOptions options = new MqttConnectOptions();
options.setSocketFactory(sslContext.getSocketFactory());
MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, CLIENT_ID);
client.connect(options);
Thread.sleep(3000);
MqttMessage message = new MqttMessage();
message.setPayload("{\"key1\":\"value1\", \"key2\":true, \"key3\": 3.0, \"key4\": 4}".getBytes());
client.publish("v1/devices/me/telemetry", message);
client.disconnect();
log.info("Disconnected");
System.exit(0);
} catch (Exception e) {
log.error("Unexpected exception occurred in MqttSslClient", e);
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project azure-iot-sdk-java by Azure.
the class MqttConnection method publishMessage.
/**
* Sends a PUBLISH message to the MQTT broker
* @param message The message to be sent
* @throws IOException if there is a Mqtt exception.
*/
public synchronized void publishMessage(MqttMessage message) throws IOException {
if (this.mqttAsyncClient == null || !this.mqttAsyncClient.isConnected()) {
throw new IOException("Mqtt is not connected");
}
if (message == null) {
throw new IOException("MqttMessage is null");
}
try {
IMqttDeliveryToken publishToken = this.mqttAsyncClient.publish(message.getTopic(), message.getMqttMessage());
publishToken.waitForCompletion();
} catch (MqttException e) {
throw new IOException("Unable to publish message on topic : " + message.getTopic(), e);
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project ha-bridge by bwssytems.
the class MQTTHandler method publishMessage.
public void publishMessage(String topic, String content, Integer qos, Boolean retain) {
MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
message.setQos(Optional.ofNullable(qos).orElse(1));
message.setRetained(Optional.ofNullable(retain).orElse(false));
try {
if (!myClient.isConnected()) {
try {
myClient.connect();
} catch (MqttSecurityException e1) {
log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage());
return;
} catch (MqttException e1) {
log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage());
return;
}
}
myClient.publish(topic, message);
} catch (MqttException e) {
log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project structr by structr.
the class MQTTClientConnection method sendMessage.
public void sendMessage(String topic, String message) throws FrameworkException {
try {
if (client.isConnected()) {
MqttMessage msg = new MqttMessage(message.getBytes());
msg.setQos(info.getQoS());
client.publish(topic, msg);
}
} catch (MqttException ex) {
throw new FrameworkException(422, "Error while sending message.");
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project rocketmq-externals by apache.
the class MqttSampleConsumer method main.
public static void main(String[] args) {
String topic = "mqtt-sample";
int qos = 0;
String broker = "tcp://127.0.0.1:1883";
String clinetId = "JavaSampleConsumer";
MemoryPersistence persistence = new MemoryPersistence();
{
try {
MqttClient sampleClient = new MqttClient(broker, clinetId, persistence);
MqttConnectOptions connectOptions = new MqttConnectOptions();
connectOptions.setCleanSession(true);
log.info("Connecting to broker: " + broker);
sampleClient.connect(connectOptions);
log.info("Connected");
sampleClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String s, MqttMessage message) throws Exception {
System.exit(0);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
sampleClient.subscribe(topic, qos);
} 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);
}
}
}
Aggregations