Search in sources :

Example 31 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project activemq-artemis by apache.

the class PahoMQTTTest method testSendAndReceiveMQTT.

@Test(timeout = 300000)
public void testSendAndReceiveMQTT() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    MqttClient consumer = createPahoClient("consumerId");
    MqttClient producer = createPahoClient("producerId");
    consumer.connect();
    consumer.subscribe("test");
    consumer.setCallback(new MqttCallback() {

        @Override
        public void connectionLost(Throwable cause) {
        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            latch.countDown();
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {
        }
    });
    producer.connect();
    producer.publish("test", "hello".getBytes(), 1, false);
    waitForLatch(latch);
    producer.disconnect();
    producer.close();
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) CountDownLatch(java.util.concurrent.CountDownLatch) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) MqttException(org.eclipse.paho.client.mqttv3.MqttException) Test(org.junit.Test)

Example 32 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project FogLight-Examples by oci-pronghorn.

the class PublishDataMQTT method message.

@Override
public boolean message(CharSequence topic, ChannelReader payload) {
    try {
        if (null == client) {
            client = new MqttClient(serverURI, clientId, new MemoryPersistence());
        }
        MqttMessage message = new MqttMessage();
        int payloadSize = payload.available();
        byte[] data = new byte[payloadSize];
        payload.read(data);
        message.setPayload(data);
        message.setRetained(false);
        message.setQos(QOS);
        client.connect(connOptions);
        client.setTimeToWait(-1);
        StringBuilder builder = new StringBuilder();
        builder.append(root).append('/');
        builder.append(clientId).append('/');
        builder.append(topic);
        logger.info("publish MQTT {} {}", QOS, builder.toString());
        client.publish(builder.toString(), message);
        client.disconnect();
    } catch (MqttException e) {
        client = null;
        logger.warn("Unable to send payload", e);
    }
    return true;
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 33 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project FogLight-Examples by oci-pronghorn.

the class MQTTPublishPAHOStage method message.

public boolean message(StringBuilder topic, byte[] data) {
    try {
        if (null == client) {
            client = new MqttClient(serverURI, clientId, new MemoryPersistence());
            client.connect(connOptions);
            client.setTimeToWait(-1);
        }
        MqttMessage message = new MqttMessage();
        message.setPayload(data);
        message.setRetained(retained);
        message.setQos(QOS);
        nextMessageTime = System.currentTimeMillis() + TIME_LIMIT;
        client.publish(topic.toString(), message);
        errorCount = 0;
        logger.info("publish MQTT QOS: {} topic: {}", QOS, topic);
        return true;
    } catch (MqttException e) {
        if (e.getMessage().contains("is not connected")) {
            client = null;
            return false;
        }
        try {
            client.disconnect();
        } catch (MqttException e1) {
        // ignore
        }
        client = null;
        if (++errorCount > 10) {
            logger.warn("Unable to send payload, is the MQTT broaker {} up and running?", serverURI, e);
        }
        return false;
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 34 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project FogLight-Examples by oci-pronghorn.

the class SubscribeDataMQTT method startup.

@Override
public void startup() {
    try {
        client = new MqttClient(serverURI, clientId, new MemoryPersistence());
        client.setCallback(new MqttCallback() {

            @Override
            public void connectionLost(Throwable cause) {
                logger.warn("connection lost, re-subscribe after timeout");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                // call startup again.
                startup();
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                logger.info("received MQTT message on topic {}", topic);
                commandChannel.publishTopic(publishTopic, w -> {
                    w.writeUTF(topic);
                    w.write(message.getPayload());
                });
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {
            }
        });
        client.connect(connOptions);
        // client.setTimeToWait(-1);
        client.subscribe(subscriptionTopic, 1);
    } catch (MqttException e) {
        throw new RuntimeException(e);
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Logger(org.slf4j.Logger) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MqttException(org.eclipse.paho.client.mqttv3.MqttException) LoggerFactory(org.slf4j.LoggerFactory) FogRuntime(com.ociweb.iot.maker.FogRuntime) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel) StartupListener(com.ociweb.gl.api.StartupListener) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) MsgCommandChannel(com.ociweb.gl.api.MsgCommandChannel) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 35 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project pinpoint by naver.

the class MqttV3CallbackMessageArrivedInterceptor method recordDataByVersion.

@Override
protected void recordDataByVersion(Object target, SpanRecorder recorder, Object[] args) {
    MqttPublish mqttPublish = ArrayArgumentUtils.getArgument(args, 0, MqttPublish.class);
    if (mqttPublish != null) {
        recorder.recordRpcName(buildRpcName(mqttPublish.getTopicName(), mqttPublish.getMessage().getQos()));
        MqttMessage mqttMessage = mqttPublish.getMessage();
        String payload = BytesUtils.toString(mqttMessage.getPayload());
        recorder.recordAttribute(MQTT_MESSAGE_PAYLOAD_ANNOTATION_KEY, payload);
        String endPoint = getEndPoint(target);
        recorder.recordEndPoint(endPoint);
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttPublish(org.eclipse.paho.client.mqttv3.internal.wire.MqttPublish)

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