You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2022/07/26 06:01:10 UTC

[GitHub] [incubator-eventmesh] LIU-WEI-git commented on a diff in pull request #1014: [ISSUE #1018] Kafka Connector: Initial Implementation of Consumer

LIU-WEI-git commented on code in PR #1014:
URL: https://github.com/apache/incubator-eventmesh/pull/1014#discussion_r929491933


##########
eventmesh-connector-plugin/eventmesh-connector-kafka/src/main/java/org/apache/eventmesh/connector/kafka/consumer/ConsumerImpl.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.eventmesh.connector.kafka.consumer;
+
+import org.apache.eventmesh.api.AbstractContext;
+import org.apache.eventmesh.api.EventListener;
+import org.apache.eventmesh.api.exception.ConnectorRuntimeException;
+
+import org.apache.kafka.clients.consumer.ConsumerConfig;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.common.PartitionInfo;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.requests.MetadataResponse;
+import org.apache.kafka.common.serialization.StringDeserializer;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.core.CloudEventUtils;
+
+public class ConsumerImpl {
+    private final KafkaConsumer<String, CloudEvent> kafkaConsumer;
+    private final Properties properties;
+    private AtomicBoolean started = new AtomicBoolean(false);
+    private EventListener eventListener;
+
+    public ConsumerImpl(final Properties properties) {
+        Properties props = new Properties();
+
+        // Other config props
+        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG));
+        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
+        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
+        props.put(ConsumerConfig.GROUP_ID_CONFIG, properties.getProperty(ConsumerConfig.GROUP_ID_CONFIG));
+        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
+
+        this.properties = props;
+        this.kafkaConsumer = new KafkaConsumer<String, CloudEvent>(props);
+    }
+
+    public Properties attributes() {
+        return properties;
+    }
+
+
+    public void start() {
+        if (this.started.compareAndSet(false, true)) {
+            try {
+                while (this.started.get()) {
+                    this.kafkaConsumer.poll(Duration.ofMillis(100));
+                }
+            } catch (Exception e) {
+                throw new ConnectorRuntimeException(e.getMessage());
+            }
+        }
+    }
+
+
+    public synchronized void shutdown() {
+        if (this.started.compareAndSet(true, false)) {
+            this.kafkaConsumer.close();
+        }
+    }
+
+
+    public boolean isStarted() {
+        return this.started.get();
+    }
+
+
+    public boolean isClosed() {
+        return !this.isStarted();
+    }
+
+    public KafkaConsumer<String, CloudEvent> getKafkaConsumer() {
+        return kafkaConsumer;
+    }
+
+    public void subscribe(String topic) {
+        try {
+            this.kafkaConsumer.subscribe(Arrays.asList(topic));
+        } catch (Exception e) {
+            throw new ConnectorRuntimeException(
+                String.format("Kafka consumer can't attach to %s.", topic));
+        }
+    }
+
+    public void unsubscribe(String topic) {
+        try {
+            // Get the current subscription
+            Map<String, List<PartitionInfo>> topicsAndPartition = this.kafkaConsumer.listTopics();
+            Set<String> topicsSet = topicsAndPartition.keySet();
+            List<String> topics = new ArrayList<>();
+            topics.addAll(topicsSet);

Review Comment:
   Hi, you get all topics but not do any change of them. It seems that you should remove the special topic from the already subscribed topics.



##########
eventmesh-connector-plugin/eventmesh-connector-kafka/src/test/resources/META-INF/services/org.apache.io.openmessaging.producer.Producer:
##########
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+org.apache.eventmesh.connector.kafka.producer.ProducerImpl

Review Comment:
   Hi, these test resource files is never used. Maybe you should commit them when you really need.



##########
eventmesh-connector-plugin/eventmesh-connector-kafka/src/main/java/org/apache/eventmesh/connector/kafka/config/ConfigurationWrapper.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.eventmesh.connector.kafka.config;
+
+import org.apache.eventmesh.common.Constants;
+import org.apache.eventmesh.connector.kafka.common.EventMeshConstants;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@UtilityClass
+public class ConfigurationWrapper {
+
+    private static final Properties properties = new Properties();
+
+    static {
+        loadProperties();
+    }
+
+    public String getProp(String key) {

Review Comment:
   This method is never used. Maybe you need write another Configration class like RocketMQ Connector to enable your .properties file.



-- 
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: dev-unsubscribe@eventmesh.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org