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/09/01 09:56:20 UTC

[GitHub] [incubator-eventmesh] Markliniubility commented on a diff in pull request #1203: [ISSUE #270] Pravega connector

Markliniubility commented on code in PR #1203:
URL: https://github.com/apache/incubator-eventmesh/pull/1203#discussion_r960449193


##########
eventmesh-connector-plugin/eventmesh-connector-pravega/src/main/java/org/apache/eventmesh/connector/pravega/client/PravegaEvent.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.pravega.client;
+
+import org.apache.eventmesh.common.utils.JsonUtils;
+import org.apache.eventmesh.connector.pravega.exception.PravegaConnectorException;
+
+import java.io.Serializable;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.SpecVersion;
+import io.cloudevents.core.builder.CloudEventBuilder;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+public class PravegaEvent implements Serializable {
+    private SpecVersion version;
+    private String topic;
+    private String data;
+    private Map<String, String> extensions = new HashMap<>();
+    private long createTimestamp;
+
+    public static byte[] toByteArray(PravegaEvent pravegaEvent) {
+        return JsonUtils.serialize(pravegaEvent).getBytes(StandardCharsets.UTF_8);
+    }
+
+    public static PravegaEvent getFromByteArray(byte[] body) {
+        return JsonUtils.deserialize(new String(body, StandardCharsets.UTF_8), PravegaEvent.class);
+    }
+
+    public CloudEvent convertToCloudEvent() {
+        CloudEventBuilder builder;
+        switch (version) {
+            case V03:
+                builder = CloudEventBuilder.v03();
+                break;
+            case V1:
+                builder = CloudEventBuilder.v1();
+                break;
+            default:
+                throw new PravegaConnectorException(String.format("CloudEvent version %s does not support.", version));
+        }
+        builder.withData(data.getBytes())
+               .withId(extensions.remove("id"))
+               .withSource(URI.create(extensions.remove("source")))
+               .withType(extensions.remove("type"))
+               .withDataContentType(extensions.remove("datacontenttype"))
+               .withSubject(extensions.remove("subject"));
+        for (Map.Entry<String, String> extension : extensions.entrySet()) {
+            builder.withExtension(extension.getKey(), extension.getValue());
+        }

Review Comment:
   nit (maybe?): 
   `extensions.entrySet().stream().forEach(extension -> builder.withExtension(extension.getKey(), extension.getValue())`



##########
eventmesh-connector-plugin/eventmesh-connector-pravega/src/main/java/org/apache/eventmesh/connector/pravega/PravegaProducerImpl.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.pravega;
+
+import io.cloudevents.CloudEvent;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.eventmesh.api.RequestReplyCallback;
+import org.apache.eventmesh.api.SendCallback;
+import org.apache.eventmesh.api.SendResult;
+import org.apache.eventmesh.api.exception.ConnectorRuntimeException;
+import org.apache.eventmesh.api.exception.OnExceptionContext;
+import org.apache.eventmesh.api.producer.Producer;
+import org.apache.eventmesh.connector.pravega.client.PravegaClient;
+import org.apache.eventmesh.connector.pravega.exception.PravegaConnectorException;
+
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+@Slf4j
+public class PravegaProducerImpl implements Producer {
+    private final AtomicBoolean started = new AtomicBoolean(false);
+    private PravegaClient client;
+
+    @Override
+    public void init(Properties properties) throws Exception {
+        client = PravegaClient.getInstance();
+    }
+
+    @Override
+    public void start() {
+        started.compareAndSet(false, true);
+    }
+
+    @Override
+    public void shutdown() {
+        started.compareAndSet(true, false);
+    }
+
+    @Override
+    public boolean isStarted() {
+        return started.get();
+    }
+
+    @Override
+    public boolean isClosed() {
+        return !started.get();
+    }
+
+    @Override
+    public void publish(CloudEvent cloudEvent, SendCallback sendCallback) throws Exception {
+        try {
+            SendResult sendResult = client.publish(cloudEvent.getSubject(), cloudEvent);
+            sendCallback.onSuccess(sendResult);
+        } catch (Exception e) {
+            log.error("send message error, topic: {}", cloudEvent.getSubject());
+            OnExceptionContext onExceptionContext = OnExceptionContext.builder()
+                    .messageId("-1")
+                    .topic(cloudEvent.getSubject())
+                    .exception(new ConnectorRuntimeException(e))
+                    .build();
+            sendCallback.onException(onExceptionContext);
+        }
+    }
+
+    @Override
+    public void sendOneway(CloudEvent cloudEvent) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void request(CloudEvent cloudEvent, RequestReplyCallback rrCallback, long timeout) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public boolean reply(CloudEvent cloudEvent, SendCallback sendCallback) throws Exception {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void checkTopicExist(String topic) throws Exception {
+        boolean exist = client.checkTopicExist(topic);
+        if (!exist) {

Review Comment:
   nit: may merge them to one line



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