use of org.eclipse.paho.client.mqttv3.MqttMessage in project kafka-connect-mqtt by jcustenborder.
the class MqttSinkTask method put.
@Override
public void put(Collection<SinkRecord> records) {
try {
for (SinkRecord record : records) {
MqttMessage message = this.converter.convert(record.valueSchema(), record.value());
this.client.publish(record.topic(), message);
}
} catch (MqttException e) {
throw new RetriableException(e);
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project kafka-connect-mqtt by jcustenborder.
the class SinkConverter method convert.
public MqttMessage convert(Schema schema, Object value) {
final byte[] payload;
if (null != schema) {
if (Schema.Type.BYTES == schema.type()) {
payload = (byte[]) value;
} else if (Schema.Type.STRING == schema.type()) {
payload = ((String) value).getBytes(Charsets.UTF_8);
} else {
throw new UnsupportedOperationException(String.format("%s is not a supported type.", schema.type()));
}
} else {
if (value instanceof byte[]) {
payload = (byte[]) value;
} else if (value instanceof String) {
payload = ((String) value).getBytes(Charsets.UTF_8);
} else {
throw new UnsupportedOperationException(String.format("%s is not a supported type.", value.getClass().getName()));
}
}
MqttMessage message = new MqttMessage();
message.setPayload(payload);
message.setQos(this.config.qos);
message.setRetained(this.config.retained);
return message;
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project wildfly-camel by wildfly-extras.
the class PahoIntegrationTest method testPahoConsumer.
@Test
public void testPahoConsumer() throws Exception {
String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("paho:" + BrokerSetup.TEST_TOPIC + "?brokerUrl=" + conUrl).transform(body().prepend("Hello ")).to("seda:end");
}
});
camelctx.start();
try {
PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer();
consumer.start();
MqttClient client = null;
try {
client = new MqttClient(conUrl, "MqttClient", new MemoryPersistence());
MqttConnectOptions opts = new MqttConnectOptions();
opts.setCleanSession(true);
client.connect(opts);
MqttMessage message = new MqttMessage("Kermit".getBytes());
message.setQos(2);
client.publish(BrokerSetup.TEST_TOPIC, message);
} finally {
client.disconnect();
}
String result = consumer.receive(3000).getIn().getBody(String.class);
Assert.assertEquals("Hello Kermit", result);
} finally {
camelctx.stop();
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project wildfly-camel by wildfly-extras.
the class PahoIntegrationTest method testMQTTProducer.
@Test
public void testMQTTProducer() throws Exception {
String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").transform(body().prepend("Hello ")).to("paho:" + BrokerSetup.TEST_TOPIC + "?brokerUrl=" + conUrl);
}
});
camelctx.start();
try {
MqttClient client = new MqttClient(conUrl, "MqttClient", new MemoryPersistence());
MqttConnectOptions opts = new MqttConnectOptions();
opts.setCleanSession(true);
client.connect(opts);
client.subscribe(BrokerSetup.TEST_TOPIC, 2);
final List<String> result = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
result.add(new String(message.getPayload()));
latch.countDown();
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
ProducerTemplate producer = camelctx.createProducerTemplate();
producer.asyncSendBody("direct:start", "Kermit");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertEquals("One message", 1, result.size());
Assert.assertEquals("Hello Kermit", result.get(0));
} finally {
camelctx.stop();
}
}
use of org.eclipse.paho.client.mqttv3.MqttMessage in project joynr by bmwcarit.
the class MqttPahoClient method publishMessage.
@Override
public void publishMessage(String topic, byte[] serializedMessage, int qosLevel) {
if (messagingSkeleton == null) {
throw new JoynrDelayMessageException("MQTT Publish failed: messagingSkeleton has not been set yet");
}
if (maxMsgSizeBytes != 0 && serializedMessage.length > maxMsgSizeBytes) {
throw new JoynrMessageNotSentException("MQTT Publish failed: maximum allowed message size of " + maxMsgSizeBytes + " bytes exceeded, actual size is " + serializedMessage.length + " bytes");
}
try {
MqttMessage message = new MqttMessage();
message.setPayload(serializedMessage);
message.setQos(qosLevel);
message.setRetained(false);
logger.debug("MQTT Publish to: {}", topic);
mqttClient.publish(topic, message);
} catch (MqttException e) {
logger.debug("MQTT Publish failed: {}. Error code {}", e.getMessage(), e.getReasonCode(), e);
switch(e.getReasonCode()) {
case MqttException.REASON_CODE_CLIENT_EXCEPTION:
Throwable cause = e.getCause();
if (cause != null) {
throw new JoynrDelayMessageException("MqttException: " + cause.getMessage());
} else {
throw new JoynrDelayMessageException("MqttException: " + e.getMessage());
}
case MqttException.REASON_CODE_BROKER_UNAVAILABLE:
case MqttException.REASON_CODE_CLIENT_DISCONNECTING:
case MqttException.REASON_CODE_CLIENT_NOT_CONNECTED:
case MqttException.REASON_CODE_CLIENT_TIMEOUT:
case MqttException.REASON_CODE_CONNECTION_LOST:
case MqttException.REASON_CODE_MAX_INFLIGHT:
case MqttException.REASON_CODE_NO_MESSAGE_IDS_AVAILABLE:
case MqttException.REASON_CODE_SERVER_CONNECT_ERROR:
case MqttException.REASON_CODE_UNEXPECTED_ERROR:
case MqttException.REASON_CODE_WRITE_TIMEOUT:
case MqttException.REASON_CODE_CONNECT_IN_PROGRESS:
throw new JoynrDelayMessageException("MqttException: " + e.getMessage());
default:
throw new JoynrMessageNotSentException(e.getMessage());
}
} catch (Exception e) {
throw new JoynrMessageNotSentException(e.getMessage(), e);
}
logger.debug("Published message: " + new String(serializedMessage, Charsets.UTF_8));
}
Aggregations