You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/10/21 08:13:22 UTC

[GitHub] [inlong] GanfengTan commented on a diff in pull request #6245: [INLONG-4986][Agent] Support MQTT Source

GanfengTan commented on code in PR #6245:
URL: https://github.com/apache/inlong/pull/6245#discussion_r1001483306


##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/MqttSource.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.inlong.agent.plugin.sources;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.plugin.Reader;
+import org.apache.inlong.agent.plugin.sources.reader.MqttReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+public class MqttSource extends AbstractSource {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttSource.class);
+
+    private static final String JOB_MQTTJOB_PARAM_PREFIX = "job.mqttJob.";
+
+    private static final String JOB_MQTTJOB_SERVERURI = "";
+
+    private static final String JOB_MQTTJOB_CLIENTID = "";
+
+    public static final String JOB_MQTTJOB_TOPICS = "job.mqttJob.topics";
+
+    public MqttSource() {
+    }
+
+    private List<Reader> splitSqlJob(String topics) {
+        final List<Reader> result = new ArrayList<>();

Review Comment:
   Would you modify it like this ‘’‘if(StringUtils.isBlank(topics)){
   return null;
   }'''



##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/MqttReader.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.inlong.agent.plugin.sources.reader;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.plugin.Message;
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.MqttSecurityException;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class MqttReader extends AbstractReader {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttReader.class);
+
+    public static final String JOB_MQTT_USERNAME = "job.mqttJob.userName";
+    public static final String JOB_MQTT_PASSWORD = "job.mqttJob.password";
+    public static final String JOB_MQTT_SERVER_URI = "job.mqttJob.serverURI";
+    public static final String JOB_MQTT_CONNECTION_TIMEOUT = "job.mqttJob.connectionTimeOut";
+    public static final String JOB_MQTT_KEEPALIVE_INTERVAL = "job.mqttJob.keepAliveInterval";
+    public static final String JOB_MQTT_QOS = "job.mqttJob.qos";
+    public static final String JOB_MQTT_CLEAN_SESSION = "job.mqttJob.cleanSession";
+    public static final String JOB_MQTT_CLIENT_ID_PREFIX = "job.mqttJob.clientIdPrefix";
+    public static final String JOB_MQTT_QUEUE_SIZE = "job.mqttJob.queueSize";
+    public static final String JOB_MQTT_AUTOMATIC_RECONNECT = "job.mqttJob.automaticReconnect";
+    public static final String JOB_MQTT_VERSION = "job.mqttJob.mqttVersion";
+
+    private boolean finished = false;
+
+    private boolean destroyed = false;
+
+    private MqttClient client;
+
+    private MqttConnectOptions options;
+
+    private String serverURI;
+    private String userName;
+    private String password;
+    private String topic;
+    private int qos;
+    private boolean cleanSession = false;
+    private boolean automaticReconnect = true;
+    private JobProfile jobProfile;
+    private String instanceId;
+    private String clientId;
+    private int mqttVersion = MqttConnectOptions.MQTT_VERSION_DEFAULT;
+
+    private LinkedBlockingQueue<DefaultMessage> mqttMessagesQueue;
+
+    public MqttReader(String topic) {
+        this.topic = topic;
+    }
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        jobProfile = jobConf;
+        LOGGER.info("init mqtt reader with jobConf {}", jobConf.toJsonStr());
+
+        mqttMessagesQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_MQTT_QUEUE_SIZE, 1000));
+        instanceId = jobConf.getInstanceId();
+        userName = jobConf.get(JOB_MQTT_USERNAME);
+        password = jobConf.get(JOB_MQTT_PASSWORD);
+        serverURI = jobConf.get(JOB_MQTT_SERVER_URI);
+        clientId = jobConf.get(JOB_MQTT_CLIENT_ID_PREFIX, "mqtt_client") + "_" + UUID.randomUUID();
+        cleanSession = jobConf.getBoolean(JOB_MQTT_CLEAN_SESSION, false);
+        automaticReconnect = jobConf.getBoolean(JOB_MQTT_AUTOMATIC_RECONNECT, true);
+        qos = jobConf.getInt(JOB_MQTT_QOS, 1);
+        mqttVersion = jobConf.getInt(JOB_MQTT_VERSION, MqttConnectOptions.MQTT_VERSION_DEFAULT);
+
+        options = new MqttConnectOptions();
+        options.setCleanSession(cleanSession);
+        options.setConnectionTimeout(jobConf.getInt(JOB_MQTT_CONNECTION_TIMEOUT, 10));
+        options.setKeepAliveInterval(jobConf.getInt(JOB_MQTT_KEEPALIVE_INTERVAL, 20));
+        options.setUserName(userName);
+        options.setPassword(password.toCharArray());
+        options.setAutomaticReconnect(automaticReconnect);
+        options.setMqttVersion(mqttVersion);
+
+        try {
+            synchronized (MqttReader.class) {
+                client = new MqttClient(serverURI, clientId, new MemoryPersistence());
+                client.setCallback(new MqttCallback() {
+                    @Override
+                    public void connectionLost(Throwable cause) {
+                        LOGGER.info("the mqtt connection is lost, try to reconnect");
+                        reconnect();
+                    }
+
+                    @Override
+                    public void messageArrived(String topic, MqttMessage message) throws Exception {
+                        Map<String, String> headerMap = new HashMap<>();
+                        headerMap.put("record.topic", topic);
+                        headerMap.put("record.messageId", String.valueOf(message.getId()));
+                        headerMap.put("record.qos", String.valueOf(message.getQos()));
+                        byte[] recordValue = message.getPayload();
+                        mqttMessagesQueue.put(new DefaultMessage(recordValue, headerMap));
+
+                        LOGGER.debug("the mqtt receive message: {}", new String(recordValue));
+
+                        readerMetric.pluginReadSuccessCount.incrementAndGet();
+                        readerMetric.pluginReadCount.incrementAndGet();
+                    }
+
+                    @Override
+                    public void deliveryComplete(IMqttDeliveryToken token) {
+                    }
+                });
+                client.connect(options);
+                client.subscribe(topic, 1);
+            }
+            LOGGER.info("the mqtt subscribe topic is [{}], qos is [{}]", topic, qos);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void reconnect() {
+        if (!client.isConnected()) {
+            try {
+                client.connect(options);
+                LOGGER.info("the mqtt client reconnect success");
+            } catch (MqttSecurityException e) {
+                e.printStackTrace();

Review Comment:
   Same as above.



##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/MqttReader.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.inlong.agent.plugin.sources.reader;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.plugin.Message;
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.MqttSecurityException;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class MqttReader extends AbstractReader {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttReader.class);
+
+    public static final String JOB_MQTT_USERNAME = "job.mqttJob.userName";
+    public static final String JOB_MQTT_PASSWORD = "job.mqttJob.password";
+    public static final String JOB_MQTT_SERVER_URI = "job.mqttJob.serverURI";
+    public static final String JOB_MQTT_CONNECTION_TIMEOUT = "job.mqttJob.connectionTimeOut";
+    public static final String JOB_MQTT_KEEPALIVE_INTERVAL = "job.mqttJob.keepAliveInterval";
+    public static final String JOB_MQTT_QOS = "job.mqttJob.qos";
+    public static final String JOB_MQTT_CLEAN_SESSION = "job.mqttJob.cleanSession";
+    public static final String JOB_MQTT_CLIENT_ID_PREFIX = "job.mqttJob.clientIdPrefix";
+    public static final String JOB_MQTT_QUEUE_SIZE = "job.mqttJob.queueSize";
+    public static final String JOB_MQTT_AUTOMATIC_RECONNECT = "job.mqttJob.automaticReconnect";
+    public static final String JOB_MQTT_VERSION = "job.mqttJob.mqttVersion";
+
+    private boolean finished = false;
+
+    private boolean destroyed = false;
+
+    private MqttClient client;
+
+    private MqttConnectOptions options;
+
+    private String serverURI;
+    private String userName;
+    private String password;
+    private String topic;
+    private int qos;
+    private boolean cleanSession = false;
+    private boolean automaticReconnect = true;
+    private JobProfile jobProfile;
+    private String instanceId;
+    private String clientId;
+    private int mqttVersion = MqttConnectOptions.MQTT_VERSION_DEFAULT;
+
+    private LinkedBlockingQueue<DefaultMessage> mqttMessagesQueue;
+
+    public MqttReader(String topic) {
+        this.topic = topic;
+    }
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        jobProfile = jobConf;
+        LOGGER.info("init mqtt reader with jobConf {}", jobConf.toJsonStr());
+
+        mqttMessagesQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_MQTT_QUEUE_SIZE, 1000));
+        instanceId = jobConf.getInstanceId();
+        userName = jobConf.get(JOB_MQTT_USERNAME);
+        password = jobConf.get(JOB_MQTT_PASSWORD);
+        serverURI = jobConf.get(JOB_MQTT_SERVER_URI);
+        clientId = jobConf.get(JOB_MQTT_CLIENT_ID_PREFIX, "mqtt_client") + "_" + UUID.randomUUID();
+        cleanSession = jobConf.getBoolean(JOB_MQTT_CLEAN_SESSION, false);
+        automaticReconnect = jobConf.getBoolean(JOB_MQTT_AUTOMATIC_RECONNECT, true);
+        qos = jobConf.getInt(JOB_MQTT_QOS, 1);
+        mqttVersion = jobConf.getInt(JOB_MQTT_VERSION, MqttConnectOptions.MQTT_VERSION_DEFAULT);
+
+        options = new MqttConnectOptions();
+        options.setCleanSession(cleanSession);
+        options.setConnectionTimeout(jobConf.getInt(JOB_MQTT_CONNECTION_TIMEOUT, 10));
+        options.setKeepAliveInterval(jobConf.getInt(JOB_MQTT_KEEPALIVE_INTERVAL, 20));
+        options.setUserName(userName);
+        options.setPassword(password.toCharArray());
+        options.setAutomaticReconnect(automaticReconnect);
+        options.setMqttVersion(mqttVersion);
+
+        try {
+            synchronized (MqttReader.class) {
+                client = new MqttClient(serverURI, clientId, new MemoryPersistence());
+                client.setCallback(new MqttCallback() {
+                    @Override
+                    public void connectionLost(Throwable cause) {
+                        LOGGER.info("the mqtt connection is lost, try to reconnect");
+                        reconnect();
+                    }
+
+                    @Override
+                    public void messageArrived(String topic, MqttMessage message) throws Exception {
+                        Map<String, String> headerMap = new HashMap<>();
+                        headerMap.put("record.topic", topic);
+                        headerMap.put("record.messageId", String.valueOf(message.getId()));
+                        headerMap.put("record.qos", String.valueOf(message.getQos()));
+                        byte[] recordValue = message.getPayload();
+                        mqttMessagesQueue.put(new DefaultMessage(recordValue, headerMap));
+
+                        LOGGER.debug("the mqtt receive message: {}", new String(recordValue));
+
+                        readerMetric.pluginReadSuccessCount.incrementAndGet();
+                        readerMetric.pluginReadCount.incrementAndGet();
+                    }
+
+                    @Override
+                    public void deliveryComplete(IMqttDeliveryToken token) {
+                    }
+                });
+                client.connect(options);
+                client.subscribe(topic, 1);
+            }
+            LOGGER.info("the mqtt subscribe topic is [{}], qos is [{}]", topic, qos);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }

Review Comment:
   Suggested to use LOGGER.



##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/MqttSource.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.inlong.agent.plugin.sources;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.plugin.Reader;
+import org.apache.inlong.agent.plugin.sources.reader.MqttReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+public class MqttSource extends AbstractSource {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttSource.class);
+
+    private static final String JOB_MQTTJOB_PARAM_PREFIX = "job.mqttJob.";
+
+    private static final String JOB_MQTTJOB_SERVERURI = "";
+
+    private static final String JOB_MQTTJOB_CLIENTID = "";
+
+    public static final String JOB_MQTTJOB_TOPICS = "job.mqttJob.topics";
+
+    public MqttSource() {
+    }
+
+    private List<Reader> splitSqlJob(String topics) {
+        final List<Reader> result = new ArrayList<>();
+        String[] topicList = topics.split(",");
+        if (Objects.nonNull(topicList)) {

Review Comment:
   Please  change ',' to ''' CommonConstants.COMMA '''



##########
inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/MqttReader.java:
##########
@@ -0,0 +1,222 @@
+/*
+ * 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.inlong.agent.plugin.sources.reader;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.agent.conf.JobProfile;
+import org.apache.inlong.agent.message.DefaultMessage;
+import org.apache.inlong.agent.plugin.Message;
+import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
+import org.eclipse.paho.client.mqttv3.MqttCallback;
+import org.eclipse.paho.client.mqttv3.MqttClient;
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.eclipse.paho.client.mqttv3.MqttMessage;
+import org.eclipse.paho.client.mqttv3.MqttSecurityException;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.LinkedBlockingQueue;
+
+public class MqttReader extends AbstractReader {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MqttReader.class);
+
+    public static final String JOB_MQTT_USERNAME = "job.mqttJob.userName";
+    public static final String JOB_MQTT_PASSWORD = "job.mqttJob.password";
+    public static final String JOB_MQTT_SERVER_URI = "job.mqttJob.serverURI";
+    public static final String JOB_MQTT_CONNECTION_TIMEOUT = "job.mqttJob.connectionTimeOut";
+    public static final String JOB_MQTT_KEEPALIVE_INTERVAL = "job.mqttJob.keepAliveInterval";
+    public static final String JOB_MQTT_QOS = "job.mqttJob.qos";
+    public static final String JOB_MQTT_CLEAN_SESSION = "job.mqttJob.cleanSession";
+    public static final String JOB_MQTT_CLIENT_ID_PREFIX = "job.mqttJob.clientIdPrefix";
+    public static final String JOB_MQTT_QUEUE_SIZE = "job.mqttJob.queueSize";
+    public static final String JOB_MQTT_AUTOMATIC_RECONNECT = "job.mqttJob.automaticReconnect";
+    public static final String JOB_MQTT_VERSION = "job.mqttJob.mqttVersion";
+
+    private boolean finished = false;
+
+    private boolean destroyed = false;
+
+    private MqttClient client;
+
+    private MqttConnectOptions options;
+
+    private String serverURI;
+    private String userName;
+    private String password;
+    private String topic;
+    private int qos;
+    private boolean cleanSession = false;
+    private boolean automaticReconnect = true;
+    private JobProfile jobProfile;
+    private String instanceId;
+    private String clientId;
+    private int mqttVersion = MqttConnectOptions.MQTT_VERSION_DEFAULT;
+
+    private LinkedBlockingQueue<DefaultMessage> mqttMessagesQueue;
+
+    public MqttReader(String topic) {
+        this.topic = topic;
+    }
+
+    @Override
+    public void init(JobProfile jobConf) {
+        super.init(jobConf);
+        jobProfile = jobConf;
+        LOGGER.info("init mqtt reader with jobConf {}", jobConf.toJsonStr());
+
+        mqttMessagesQueue = new LinkedBlockingQueue<>(jobConf.getInt(JOB_MQTT_QUEUE_SIZE, 1000));
+        instanceId = jobConf.getInstanceId();
+        userName = jobConf.get(JOB_MQTT_USERNAME);
+        password = jobConf.get(JOB_MQTT_PASSWORD);
+        serverURI = jobConf.get(JOB_MQTT_SERVER_URI);
+        clientId = jobConf.get(JOB_MQTT_CLIENT_ID_PREFIX, "mqtt_client") + "_" + UUID.randomUUID();
+        cleanSession = jobConf.getBoolean(JOB_MQTT_CLEAN_SESSION, false);
+        automaticReconnect = jobConf.getBoolean(JOB_MQTT_AUTOMATIC_RECONNECT, true);
+        qos = jobConf.getInt(JOB_MQTT_QOS, 1);
+        mqttVersion = jobConf.getInt(JOB_MQTT_VERSION, MqttConnectOptions.MQTT_VERSION_DEFAULT);
+
+        options = new MqttConnectOptions();
+        options.setCleanSession(cleanSession);
+        options.setConnectionTimeout(jobConf.getInt(JOB_MQTT_CONNECTION_TIMEOUT, 10));
+        options.setKeepAliveInterval(jobConf.getInt(JOB_MQTT_KEEPALIVE_INTERVAL, 20));
+        options.setUserName(userName);
+        options.setPassword(password.toCharArray());
+        options.setAutomaticReconnect(automaticReconnect);
+        options.setMqttVersion(mqttVersion);
+
+        try {
+            synchronized (MqttReader.class) {
+                client = new MqttClient(serverURI, clientId, new MemoryPersistence());
+                client.setCallback(new MqttCallback() {
+                    @Override
+                    public void connectionLost(Throwable cause) {
+                        LOGGER.info("the mqtt connection is lost, try to reconnect");
+                        reconnect();
+                    }
+
+                    @Override
+                    public void messageArrived(String topic, MqttMessage message) throws Exception {
+                        Map<String, String> headerMap = new HashMap<>();
+                        headerMap.put("record.topic", topic);
+                        headerMap.put("record.messageId", String.valueOf(message.getId()));
+                        headerMap.put("record.qos", String.valueOf(message.getQos()));
+                        byte[] recordValue = message.getPayload();
+                        mqttMessagesQueue.put(new DefaultMessage(recordValue, headerMap));
+
+                        LOGGER.debug("the mqtt receive message: {}", new String(recordValue));
+
+                        readerMetric.pluginReadSuccessCount.incrementAndGet();
+                        readerMetric.pluginReadCount.incrementAndGet();
+                    }
+
+                    @Override
+                    public void deliveryComplete(IMqttDeliveryToken token) {
+                    }
+                });
+                client.connect(options);
+                client.subscribe(topic, 1);
+            }
+            LOGGER.info("the mqtt subscribe topic is [{}], qos is [{}]", topic, qos);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void reconnect() {
+        if (!client.isConnected()) {
+            try {
+                client.connect(options);
+                LOGGER.info("the mqtt client reconnect success");
+            } catch (MqttSecurityException e) {
+                e.printStackTrace();
+            } catch (MqttException e) {
+                e.printStackTrace();

Review Comment:
   Same as above.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@inlong.apache.org

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