You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@bahir.apache.org by GitBox <gi...@apache.org> on 2021/03/12 07:05:37 UTC

[GitHub] [bahir-flink] yuruguo commented on a change in pull request #100: [BAHIR-212] Add MQTT Connector for Flink

yuruguo commented on a change in pull request #100:
URL: https://github.com/apache/bahir-flink/pull/100#discussion_r592951546



##########
File path: flink-connector-mqtt/src/main/java/org/apache/flink/streaming/connectors/mqtt/MqttSource.java
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.flink.streaming.connectors.mqtt;
+
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.state.ListState;
+import org.apache.flink.api.common.state.ListStateDescriptor;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.state.FunctionInitializationContext;
+import org.apache.flink.runtime.state.FunctionSnapshotContext;
+import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
+import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import static java.util.Collections.singletonList;
+import static org.apache.flink.shaded.guava18.com.google.common.collect.Iterables.getOnlyElement;
+import static org.apache.flink.streaming.connectors.mqtt.MqttConfig.checkProperty;
+
+/**
+ * Source for reading messages from an Mqtt queue.
+ *
+ * @param <OUT> type of output messages
+ */
+public class MqttSource<OUT> extends RichSourceFunction<OUT>
+    implements CheckpointedFunction, ResultTypeQueryable<OUT>, MqttCallbackExtended {
+    private static final Logger LOG = LoggerFactory.getLogger(MqttSource.class);
+
+    // Mqtt subcriber client id
+    private static final String MQTT_CLIENT_ID = MqttClient.generateClientId();
+    // Blocking queue max capacity
+    private static final int MAX_QUEUE_CAPACITY = 10000;
+
+    // Subscribe one or more topics
+    private final String[] topics;
+    // Convert bytes to output message
+    private final DeserializationSchema<OUT> deserializationSchema;
+    // User-supplied properties
+    private final Properties properties;
+    // Queue cache data form mqtt client
+    private final LinkedBlockingQueue<MqttMessage> queue;
+
+    private String serverUrl;
+    private String username;
+    private String password;
+    private String clientId;
+    private boolean cleanSession;
+    private int qos;
+
+    private transient ListState<String> mqttState;
+    private transient MqttClient client;
+
+    private volatile boolean isRunning;
+
+    public MqttSource(String topic, DeserializationSchema<OUT> deserializationSchema, Properties properties) {
+        this(Collections.singletonList(topic), deserializationSchema, properties, MAX_QUEUE_CAPACITY);
+    }
+
+    public MqttSource(List<String> topics, DeserializationSchema<OUT> deserializationSchema, Properties properties) {
+        this(topics, deserializationSchema, properties, MAX_QUEUE_CAPACITY);
+    }
+
+    /**
+     * Creates {@link MqttSource} for Streaming
+     *
+     * @param topics                Subscribe topic list
+     * @param deserializationSchema Convert bytes to output message
+     * @param properties            Mqtt properties
+     * @param capacity              Blocking queue capacity
+     */
+    public MqttSource(List<String> topics,
+                      DeserializationSchema<OUT> deserializationSchema,
+                      Properties properties,
+                      int capacity) {
+        checkProperty(properties, MqttConfig.SERVER_URL);
+        checkProperty(properties, MqttConfig.USERNAME);
+        checkProperty(properties, MqttConfig.PASSWORD);
+
+        this.topics = topics.toArray(new String[topics.size()]);
+        this.deserializationSchema = deserializationSchema;
+        this.properties = properties;
+        this.queue = new LinkedBlockingQueue<>(capacity);
+    }
+
+    @Override
+    public void initializeState(FunctionInitializationContext context) throws Exception {
+        this.mqttState = context.getOperatorStateStore().getListState(
+            new ListStateDescriptor<>("mqttClientId", String.class));
+        this.clientId = context.isRestored() ? getOnlyElement(mqttState.get()) : MQTT_CLIENT_ID;
+        LOG.info("Current mqtt subcriber clientId is: {}", clientId);
+    }
+
+    @Override
+    public void open(Configuration parameters) throws Exception {
+        super.open(parameters);
+        if (connect()) {
+            this.isRunning = true;
+        }
+    }
+
+    private boolean connect() throws Exception {
+        this.serverUrl = properties.getProperty(MqttConfig.SERVER_URL);

Review comment:
       It's ok to use directly where needed, but this values may be used in multiple places, such as `properties.getProperty(MqttConfig.SERVER_URL)`, so saving them separately can ensure the code is concise.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org