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