Search in sources :

Example 1 with MqttLogger

use of org.apache.storm.mqtt.MqttLogger 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 MqttLogger

use of org.apache.storm.mqtt.MqttLogger in project storm by apache.

the class MqttBrokerPublisher method startPublisher.

/**
 * Initializes {@code connection}.
 * @throws Exception if an exception during connecting to connector occurs
 */
public static void startPublisher() throws Exception {
    MQTT client = new MQTT();
    client.setTracer(new MqttLogger());
    client.setHost("tcp://localhost:1883");
    client.setClientId("MqttBrokerPublisher");
    connection = client.blockingConnection();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                LOG.info("Shutting down MQTT client...");
                connection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    connection.connect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) MqttLogger(org.apache.storm.mqtt.MqttLogger)

Aggregations

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