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