xwm1992 commented on code in PR #1027:
URL: 
https://github.com/apache/incubator-eventmesh/pull/1027#discussion_r924014604


##########
eventmesh-connector-plugin/eventmesh-connector-knative/src/main/java/org/apache/eventmesh/connector/knative/producer/ProducerImpl.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.knative.producer;
+
+import io.cloudevents.CloudEvent;
+import org.apache.eventmesh.connector.knative.cloudevent.KnativeMessageFactory;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+
+public class ProducerImpl extends AbstractProducer {
+
+    public ProducerImpl(final Properties properties) throws IOException {
+        super(properties);
+    }
+
+    public Properties attributes() {
+        return properties;
+    }
+
+    public void sendOneway(CloudEvent cloudEvent) {
+        // Get CloudEvent data:
+        try {
+            String data = KnativeMessageFactory.createReader(cloudEvent);
+            
super.getHttpUrlConnection().getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
+
+            // Send CloudEvent message:
+            String s = "";
+            int code = super.getHttpUrlConnection().getResponseCode();
+            if (code == 200) {
+                BufferedReader reader = new BufferedReader(
+                        new 
InputStreamReader(super.getHttpUrlConnection().getInputStream()));
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    s += line + "\n";
+                }
+                reader.close();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

Review Comment:
   You don't need to have this method, and when you implement the publish 
method with the callback, you can use the `Async Http Client`, here don't 
implement synchronously.



##########
eventmesh-connector-plugin/eventmesh-connector-knative/src/main/java/org/apache/eventmesh/connector/knative/producer/AbstractProducer.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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.knative.producer;
+
+import org.apache.eventmesh.connector.knative.cloudevent.impl.KnativeHeaders;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public abstract class AbstractProducer {
+
+    final Properties properties;
+    HttpURLConnection httpUrlConnection;
+    protected final AtomicBoolean started = new AtomicBoolean(false);
+
+    AbstractProducer(final Properties properties) throws IOException {
+        this.properties = properties;
+        URL url = new URL(properties.getProperty("url"));
+        this.httpUrlConnection = (HttpURLConnection) url.openConnection();
+        httpUrlConnection.setDoOutput(true);
+        httpUrlConnection.setDoInput(true);
+
+        // Set HTTP header for CloudEvent:
+        this.httpUrlConnection.setRequestProperty(KnativeHeaders.CONTENT_TYPE, 
properties.getProperty(KnativeHeaders.CONTENT_TYPE));
+        this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_ID, 
properties.getProperty(KnativeHeaders.CE_ID));
+        
this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_SPECVERSION, 
properties.getProperty(KnativeHeaders.CE_SPECVERSION));
+        this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_TYPE, 
properties.getProperty(KnativeHeaders.CE_TYPE));
+        this.httpUrlConnection.setRequestProperty(KnativeHeaders.CE_SOURCE, 
properties.getProperty(KnativeHeaders.CE_SOURCE));

Review Comment:
   These code for HTTP header attributes with the request, I recommend that you 
can write them when you `send the cloud events not when you init the 
`producer`. 



##########
eventmesh-connector-plugin/eventmesh-connector-knative/src/main/java/org/apache/eventmesh/connector/knative/producer/KnativeProducerImpl.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.knative.producer;
+
+import io.cloudevents.CloudEvent;
+import org.apache.eventmesh.api.RequestReplyCallback;
+import org.apache.eventmesh.api.SendCallback;
+import org.apache.eventmesh.api.producer.Producer;
+
+import java.util.Properties;
+
+public class KnativeProducerImpl implements Producer {
+
+    private ProducerImpl producer;
+
+    @Override
+    public void init(Properties properties) throws Exception {
+        producer = new ProducerImpl(properties);
+    }
+
+    @Override
+    public boolean isStarted() {
+        return producer.isStarted();
+    }
+
+    @Override
+    public boolean isClosed() {
+        return producer.isClosed();
+    }
+
+    @Override
+    public void sendOneway(CloudEvent cloudEvent) {

Review Comment:
   Please not implement this method, you should impelement the `publish` method 
with the send call back.



##########
eventmesh-connector-plugin/eventmesh-connector-knative/src/main/java/org/apache/eventmesh/connector/knative/cloudevent/impl/KnativeMessageWriter.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.knative.cloudevent.impl;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.CloudEventData;
+import io.cloudevents.SpecVersion;
+import io.cloudevents.core.v1.CloudEventBuilder;
+import io.cloudevents.core.format.EventFormat;
+import io.cloudevents.core.message.MessageWriter;
+import io.cloudevents.rw.CloudEventContextWriter;
+import io.cloudevents.rw.CloudEventRWException;
+import io.cloudevents.rw.CloudEventWriter;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+
+public class KnativeMessageWriter implements 
MessageWriter<CloudEventWriter<String>, String>, CloudEventWriter<String> {
+
+    public CloudEvent message;
+
+    public KnativeMessageWriter(String data) {
+        String s = "{ \"msg\": [\"" + data + "\"]}";
+        this.message = new CloudEventBuilder()
+                .withId("my-id")
+                .withSource(URI.create("/myClient"))
+                .withType("dev.knative.cronjob.event")
+                .withDataContentType("application/json")
+                .withData(s.getBytes(StandardCharsets.UTF_8))
+                .build();
+    }

Review Comment:
   These codes look strange, you use the cloudeventbuilder to build the cloud 
events, but you only want to put the data, other attributes are useless.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to