Search in sources :

Example 36 with MqttMessage

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);
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 37 with MqttMessage

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);
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) Account(io.syndesis.qe.account.Account) 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)

Example 38 with MqttMessage

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

Example 39 with MqttMessage

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);
    }
}
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)

Example 40 with MqttMessage

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

Aggregations

MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)65 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