Github user JPercivall commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/349#discussion_r65405495
  
    --- Diff: 
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/iot/util/MqttWebSocketAsyncClient.java
 ---
    @@ -0,0 +1,113 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.processors.aws.iot.util;
    +
    +import java.net.URI;
    +import java.util.concurrent.LinkedBlockingQueue;
    +
    +import org.apache.nifi.logging.ProcessorLog;
    +import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
    +import org.eclipse.paho.client.mqttv3.MqttCallback;
    +import org.eclipse.paho.client.mqttv3.MqttException;
    +import org.eclipse.paho.client.mqttv3.TimerPingSender;
    +import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
    +import org.eclipse.paho.client.mqttv3.MqttSecurityException;
    +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
    +import org.eclipse.paho.client.mqttv3.MqttMessage;
    +import org.eclipse.paho.client.mqttv3.internal.NetworkModule;
    +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
    +
    +public class MqttWebSocketAsyncClient extends MqttAsyncClient implements 
MqttCallback {
    +
    +    protected volatile LinkedBlockingQueue<IoTMessage> 
awsQueuedMqttMessages = new LinkedBlockingQueue<IoTMessage>();
    +    protected final ProcessorLog logger;
    +    protected final String serverURI;
    +
    +    protected static String createDummyURI(String original) {
    +        if (!original.startsWith("ws:") && !original.startsWith("wss:")) {
    +            return original;
    +        }
    +        final URI uri = URI.create(original);
    +        return "tcp://DUMMY-" + uri.getHost() + ":"
    +                + (uri.getPort() > 0 ? uri.getPort() : 80);
    +    }
    +
    +    protected static boolean isDummyURI(String uri) {
    +        return uri.startsWith("tcp://DUMMY-");
    +    }
    +
    +    public MqttWebSocketAsyncClient(String serverURI, String clientId,
    +                                    ProcessorLog logger) throws 
MqttException {
    +        super(createDummyURI(serverURI), clientId, new 
MemoryPersistence(), new TimerPingSender());
    +        this.serverURI = serverURI;
    +        this.logger = logger;
    +        this.setCallback(this);
    +    }
    +
    +    @Override
    +    protected NetworkModule[] createNetworkModules(String address,
    +                                                   MqttConnectOptions 
options) throws MqttException{
    +        String[] serverURIs = options.getServerURIs();
    +        String[] array = serverURIs == null ? new String[] { address } :
    +            serverURIs.length == 0 ? new String[] { address }: serverURIs;
    +
    +        NetworkModule[] networkModules = new NetworkModule[array.length];
    +        for (int i = 0; i < array.length; i++) {
    +            networkModules[i] = createNetworkModule(array[i], options);
    +        }
    +        return networkModules;
    +    }
    +
    +    protected NetworkModule createNetworkModule(String input,
    +                                                MqttConnectOptions 
options) throws MqttException,
    +            MqttSecurityException {
    +        final String address = isDummyURI(input) ? this.serverURI : input;
    +        if (!address.startsWith("ws:") && !address.startsWith("wss:")) {
    +            return super.createNetworkModules(address, options)[0];
    +        }
    +
    +        final String subProtocol = (options.getMqttVersion() == 
MqttConnectOptions.MQTT_VERSION_3_1) ? "mqttv3.1" : "mqtt";
    +        return newWebSocketNetworkModule(URI.create(address), subProtocol, 
options);
    +    }
    +
    +    protected NetworkModule newWebSocketNetworkModule(URI uri,
    +                                                      String subProtocol, 
MqttConnectOptions options) {
    +        final WebSocketNetworkModule netModule = new 
WebSocketNetworkModule(
    +                uri, subProtocol, getClientId());
    +        netModule.setConnectTimeout(options.getConnectionTimeout());
    +        return netModule;
    +    }
    +
    +    public LinkedBlockingQueue<IoTMessage> getAwsQueuedMqttMessages() {
    +        return awsQueuedMqttMessages;
    +    }
    +
    +    @Override
    +    public void connectionLost(Throwable t) {
    +        logger.error("Connection to " + this.getServerURI() + " lost with 
cause: " + t.getMessage());
    +    }
    +
    +    @Override
    +    public void deliveryComplete(IMqttDeliveryToken token) {
    +    }
    +
    +    @Override
    +    public void messageArrived(String topic, MqttMessage message) throws 
Exception {
    +        logger.info("Message arrived from topic: " + topic);
    --- End diff --
    
    This came up in my review as well (I did this at first), we shouldn't set 
the messageArrived message to INFO. The rate at which MQTT message potentially 
arrive would fill up the logs very quickly. Check out how I handle it in 
ConsumeMQTT[1].
    
    [1] 
https://github.com/apache/nifi/blob/f47af1ce8336c9305916f00738976f3505b01b0b/nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/ConsumeMQTT.java#L310


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to