Search in sources :

Example 66 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project irontest by zheng-wang.

the class MQTTTeststepRunner method run.

@Override
public BasicTeststepRun run() throws Exception {
    Teststep teststep = getTeststep();
    MQTTTeststepProperties otherProperties = (MQTTTeststepProperties) teststep.getOtherProperties();
    // validate arguments
    if ("".equals(StringUtils.trimToEmpty(otherProperties.getTopicString()))) {
        throw new IllegalArgumentException("Topic String not specified.");
    }
    Endpoint endpoint = teststep.getEndpoint();
    MqttClient mqttClient = new MqttClient(endpoint.getUrl(), "irontest-mqtt-teststep");
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setUserName(endpoint.getUsername());
    connOpts.setPassword(getDecryptedEndpointPassword() == null ? null : getDecryptedEndpointPassword().toCharArray());
    mqttClient.connect(connOpts);
    try {
        MQTTRequest request = (MQTTRequest) teststep.getApiRequest();
        MqttMessage message = new MqttMessage(request.getPayload() == null ? null : request.getPayload().getBytes());
        message.setQos(1);
        mqttClient.publish(otherProperties.getTopicString(), message);
    } finally {
        mqttClient.disconnect();
    }
    return new BasicTeststepRun();
}
Also used : Teststep(io.irontest.models.teststep.Teststep) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Endpoint(io.irontest.models.endpoint.Endpoint) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) MQTTTeststepProperties(io.irontest.models.teststep.MQTTTeststepProperties) MQTTRequest(io.irontest.models.teststep.MQTTRequest)

Example 67 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project pentaho-kettle by pentaho.

the class MQTTProducer method getMessage.

private MqttMessage getMessage(Object[] row) throws KettleStepException {
    MqttMessage mqttMessage = new MqttMessage();
    try {
        mqttMessage.setQos(Integer.parseInt(meta.qos));
    } catch (NumberFormatException e) {
        throw new KettleStepException(getString(PKG, "MQTTProducer.Error.QOS", meta.qos));
    }
    // noinspection ConstantConditions
    mqttMessage.setPayload(getFieldData(row, meta.messageField).map(this::dataAsBytes).orElse(// allow nulls to pass through
    null));
    return mqttMessage;
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 68 with MqttMessage

use of org.eclipse.paho.client.mqttv3.MqttMessage in project pentaho-kettle by pentaho.

the class MQTTProducerTest method testSendRowToProducer.

@Test
public void testSendRowToProducer() throws Exception {
    when(mqttClient.isConnected()).thenReturn(true);
    handleAsSecondRow(trans);
    doAnswer(invocation -> {
        String topic = (String) invocation.getArguments()[0];
        MqttMessage message = (MqttMessage) invocation.getArguments()[1];
        assertEquals("TestWinning", topic);
        assertEquals(0, message.getQos());
        assertEquals("#winning", new String(message.getPayload(), UTF_8));
        return null;
    }).when(mqttClient).publish(any(), any());
    trans.startThreads();
    trans.waitUntilFinished();
    verify(mqttClient).disconnect();
    assertEquals(4, trans.getSteps().get(1).step.getLinesOutput());
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) Test(org.junit.Test)

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