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/20 10:50:28 UTC

[GitHub] [inlong] haibo-duan opened a new pull request, #6245: [INLONG-4986][Agent] Support MQTT Source

haibo-duan opened a new pull request, #6245:
URL: https://github.com/apache/inlong/pull/6245

   ### Prepare a Pull Request
   *(Change the title refer to the following example)*
   
   - Title Example: [INLONG-XYZ][Component] Title of the pull request
   
   *(The following *XYZ* should be replaced by the actual [GitHub Issue](https://github.com/apache/inlong/issues) number)*
   
   - About #4986 
   
   ### Motivation
   
   *Explain here the context, and why you're making that change. What is the problem you're trying to solve?*
   
   ### Modifications
   
   *Describe the modifications you've done.*
   
   ### Verifying this change
   
   *(Please pick either of the following options)*
   
   - [ ] This change is a trivial rework/code cleanup without any test coverage.
   
   - [ ] This change is already covered by existing tests, such as:
     *(please describe tests)*
   
   - [ ] This change added tests and can be verified as follows:
   
     *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a follow-up issue for adding the documentation
   


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


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

Posted by GitBox <gi...@apache.org>.
pocozh commented on code in PR #6245:
URL: https://github.com/apache/inlong/pull/6245#discussion_r1004057448


##########
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");

Review Comment:
   It's better to add  some necessary info such as jobId, connection etc in this log.



##########
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) {
+            LOGGER.error("init mqtt client error ", e);
+        }
+    }
+
+    private void reconnect() {
+        if (!client.isConnected()) {
+            try {
+                client.connect(options);
+                LOGGER.info("the mqtt client reconnect success");
+            } catch (MqttSecurityException e) {
+                LOGGER.error("reconnect mqtt client error ", e);
+            } catch (MqttException e) {
+                LOGGER.error("reconnect mqtt client error ", e);
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!mqttMessagesQueue.isEmpty()) {
+            return getMqttMessage();
+        } else {
+            return null;
+        }
+    }
+
+    private DefaultMessage getMqttMessage() {
+        return mqttMessagesQueue.poll();
+    }
+
+    @Override
+    public boolean isFinished() {
+        return finished;
+    }
+
+    @Override
+    public String getReadSource() {
+        return instanceId;
+    }
+
+    @Override
+    public void setReadTimeout(long mill) {
+    }
+
+    @Override
+    public void setWaitMillisecond(long millis) {
+    }
+
+    @Override
+    public String getSnapshot() {

Review Comment:
   Could you please complete the strategy of snapshot?



##########
inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/pojo/JobProfileDto.java:
##########
@@ -54,6 +54,10 @@ public class JobProfileDto {
      * mongo source
      */
     public static final String MONGO_SOURCE = "org.apache.inlong.agent.plugin.sources.MongoDBSource";
+    /**
+     * mqtt source
+     */
+    public static final String MQTT_SOURCE = "org.apache.agent.plugin.sources.MqttSource";

Review Comment:
   wrong package name



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


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

Posted by GitBox <gi...@apache.org>.
EMsnap commented on code in PR #6245:
URL: https://github.com/apache/inlong/pull/6245#discussion_r1003955955


##########
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) {

Review Comment:
   method is way too long 



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


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

Posted by GitBox <gi...@apache.org>.
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


[GitHub] [inlong] dockerzhang merged pull request #6245: [INLONG-4986][Agent] Support MQTT Source

Posted by GitBox <gi...@apache.org>.
dockerzhang merged PR #6245:
URL: https://github.com/apache/inlong/pull/6245


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


[GitHub] [inlong] haibo-duan commented on pull request #6245: [INLONG-4986][Agent] Support MQTT Source

Posted by GitBox <gi...@apache.org>.
haibo-duan commented on PR #6245:
URL: https://github.com/apache/inlong/pull/6245#issuecomment-1288452728

   > @haibo-duan here we need to add MQTT Source to the `JobProfileDto.class`. You can refer to MongoDB in #5674. maybe other sources you added in agent also add the job definition.
   
   It has been modified. Please review it for me.


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


[GitHub] [inlong] dockerzhang commented on pull request #6245: [INLONG-4986][Agent] Support MQTT Source

Posted by GitBox <gi...@apache.org>.
dockerzhang commented on PR #6245:
URL: https://github.com/apache/inlong/pull/6245#issuecomment-1286720090

   @haibo-duan here we need to add MQTT Source to the `JobProfileDto.class`. You can refer to MongoDB in #5674. 
   maybe other sources you added in agent also add the job definition. 


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


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

Posted by GitBox <gi...@apache.org>.
haibo-duan commented on code in PR #6245:
URL: https://github.com/apache/inlong/pull/6245#discussion_r1004106638


##########
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) {
+            LOGGER.error("init mqtt client error ", e);
+        }
+    }
+
+    private void reconnect() {
+        if (!client.isConnected()) {
+            try {
+                client.connect(options);
+                LOGGER.info("the mqtt client reconnect success");
+            } catch (MqttSecurityException e) {
+                LOGGER.error("reconnect mqtt client error ", e);
+            } catch (MqttException e) {
+                LOGGER.error("reconnect mqtt client error ", e);
+            }
+        }
+    }
+
+    @Override
+    public Message read() {
+        if (!mqttMessagesQueue.isEmpty()) {
+            return getMqttMessage();
+        } else {
+            return null;
+        }
+    }
+
+    private DefaultMessage getMqttMessage() {
+        return mqttMessagesQueue.poll();
+    }
+
+    @Override
+    public boolean isFinished() {
+        return finished;
+    }
+
+    @Override
+    public String getReadSource() {
+        return instanceId;
+    }
+
+    @Override
+    public void setReadTimeout(long mill) {
+    }
+
+    @Override
+    public void setWaitMillisecond(long millis) {
+    }
+
+    @Override
+    public String getSnapshot() {

Review Comment:
   @pocozh   when the mqtt topic is subscribed, the message is pushed to the client through the broker. There is no offset attribute, and the snapshot may not be necessary.



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