Search in sources :

Example 1 with QoS

use of org.fusesource.mqtt.client.QoS in project storm by apache.

the class MqttUtils method configureClient.

public static MQTT configureClient(MqttOptions options, String clientId, KeyStoreLoader keyStoreLoader) throws Exception {
    MQTT client = new MQTT();
    URI uri = URI.create(options.getUrl());
    client.setHost(uri);
    if (!uri.getScheme().toLowerCase().equals("tcp")) {
        client.setSslContext(SslUtils.sslContext(uri.getScheme(), keyStoreLoader));
    }
    client.setClientId(clientId);
    LOG.info("MQTT ClientID: {}", client.getClientId().toString());
    client.setCleanSession(options.isCleanConnection());
    client.setReconnectDelay(options.getReconnectDelay());
    client.setReconnectDelayMax(options.getReconnectDelayMax());
    client.setReconnectBackOffMultiplier(options.getReconnectBackOffMultiplier());
    client.setConnectAttemptsMax(options.getConnectAttemptsMax());
    client.setReconnectAttemptsMax(options.getReconnectAttemptsMax());
    client.setUserName(options.getUserName());
    client.setPassword(options.getPassword());
    client.setTracer(new MqttLogger());
    if (options.getWillTopic() != null && options.getWillPayload() != null) {
        QoS qos = MqttUtils.qosFromInt(options.getWillQos());
        client.setWillQos(qos);
        client.setWillTopic(options.getWillTopic());
        client.setWillMessage(options.getWillPayload());
        client.setWillRetain(options.getWillRetain());
    }
    return client;
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) MqttLogger(org.apache.storm.mqtt.MqttLogger) QoS(org.fusesource.mqtt.client.QoS) URI(java.net.URI)

Example 2 with QoS

use of org.fusesource.mqtt.client.QoS in project storm by apache.

the class MqttSpout method connectMqtt.

private void connectMqtt() throws Exception {
    String clientId = this.topologyName + "-" + this.context.getThisComponentId() + "-" + this.context.getThisTaskId();
    MQTT client = MqttUtils.configureClient(this.options, clientId, this.keyStoreLoader);
    this.connection = client.callbackConnection();
    this.connection.listener(this);
    this.connection.connect(new ConnectCallback());
    while (!this.mqttConnected && !this.mqttConnectFailed) {
        LOG.info("Waiting for connection...");
        Thread.sleep(500);
    }
    if (this.mqttConnected) {
        List<String> topicList = this.options.getTopics();
        Topic[] topics = new Topic[topicList.size()];
        QoS qos = MqttUtils.qosFromInt(this.options.getQos());
        for (int i = 0; i < topicList.size(); i++) {
            topics[i] = new Topic(topicList.get(i), qos);
        }
        connection.subscribe(topics, new SubscribeCallback());
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) QoS(org.fusesource.mqtt.client.QoS) Topic(org.fusesource.mqtt.client.Topic)

Example 3 with QoS

use of org.fusesource.mqtt.client.QoS in project camel by apache.

the class MQTTProducer method process.

@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    if (!mqttEndpoint.isConnected()) {
        try {
            ensureConnected();
        } catch (Exception e) {
            exchange.setException(e);
            callback.done(true);
            return true;
        }
    }
    byte[] body = exchange.getIn().getBody(byte[].class);
    if (body != null) {
        MQTTConfiguration configuration = mqttEndpoint.getConfiguration();
        boolean retain = exchange.getProperty(configuration.getMqttRetainPropertyName(), configuration.isByDefaultRetain(), Boolean.class);
        QoS qoS = configuration.getQoS();
        Object qoSValue = exchange.getProperty(configuration.getMqttQosPropertyName());
        if (qoSValue != null) {
            qoS = MQTTConfiguration.getQoS(qoSValue.toString());
        }
        // where should we publish to
        String topicName = configuration.getPublishTopicName();
        // get the topic name by using the header of MQTT_PUBLISH_TOPIC
        Object topicValue = exchange.getIn().getHeader(MQTTConfiguration.MQTT_PUBLISH_TOPIC);
        if (topicValue != null) {
            topicName = topicValue.toString();
        }
        final String name = topicName;
        try {
            log.debug("Publishing to {}", name);
            mqttEndpoint.publish(name, body, qoS, retain, new Callback<Void>() {

                @Override
                public void onSuccess(Void aVoid) {
                    log.trace("onSuccess from {}", name);
                    callback.done(false);
                }

                @Override
                public void onFailure(Throwable throwable) {
                    log.trace("onFailure from {}", name);
                    exchange.setException(throwable);
                    callback.done(false);
                }
            });
        } catch (Exception e) {
            exchange.setException(e);
            callback.done(true);
            return true;
        }
        // we continue async, as the mqtt endpoint will invoke the callback when its done
        return false;
    } else {
        // no data to send so we are done
        log.trace("No data to publish");
        callback.done(true);
        return true;
    }
}
Also used : QoS(org.fusesource.mqtt.client.QoS)

Aggregations

QoS (org.fusesource.mqtt.client.QoS)3 MQTT (org.fusesource.mqtt.client.MQTT)2 URI (java.net.URI)1 MqttLogger (org.apache.storm.mqtt.MqttLogger)1 Topic (org.fusesource.mqtt.client.Topic)1