Search in sources :

Example 61 with MqttMessage

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);
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) KeyStore(java.security.KeyStore) URL(java.net.URL) FileInputStream(java.io.FileInputStream) MqttAsyncClient(org.eclipse.paho.client.mqttv3.MqttAsyncClient) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) File(java.io.File)

Example 62 with MqttMessage

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);
    }
}
Also used : MqttException(org.eclipse.paho.client.mqttv3.MqttException) IOException(java.io.IOException) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Example 63 with MqttMessage

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());
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttSecurityException(org.eclipse.paho.client.mqttv3.MqttSecurityException)

Example 64 with MqttMessage

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.");
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) FrameworkException(org.structr.common.error.FrameworkException) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 65 with MqttMessage

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);
        }
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)

Aggregations

MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)64 MqttException (org.eclipse.paho.client.mqttv3.MqttException)28 MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)19 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)16 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)16 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)15 Test (org.junit.Test)14 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 JsonParser (com.google.gson.JsonParser)3 IOException (java.io.IOException)3 MqttAsyncClient (org.eclipse.paho.client.mqttv3.MqttAsyncClient)3 JSONObject (org.json.JSONObject)3 Test (org.testng.annotations.Test)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 MISTEdge (edu.snu.mist.common.graph.MISTEdge)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 CamelContext (org.apache.camel.CamelContext)2