Fungx commented on code in PR #4634: URL: https://github.com/apache/eventmesh/pull/4634#discussion_r1422037387
########## eventmesh-connectors/eventmesh-connector-http/src/main/java/org/apache/eventmesh/connector/http/source/connector/HttpSourceConnector.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.http.source.connector; + +import org.apache.eventmesh.common.exception.EventMeshException; +import org.apache.eventmesh.connector.http.source.config.HttpSourceConfig; +import org.apache.eventmesh.openconnect.api.config.Config; +import org.apache.eventmesh.openconnect.api.connector.ConnectorContext; +import org.apache.eventmesh.openconnect.api.connector.SourceConnectorContext; +import org.apache.eventmesh.openconnect.api.source.Source; +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; +import org.apache.eventmesh.openconnect.util.CloudEventUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +import io.cloudevents.CloudEvent; +import io.cloudevents.core.message.MessageReader; +import io.cloudevents.http.vertx.VertxMessageFactory; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.Vertx; +import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.HttpServer; +import io.vertx.core.http.HttpServerOptions; +import io.vertx.ext.web.Router; +import io.vertx.ext.web.handler.LoggerHandler; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class HttpSourceConnector implements Source { + + private static final int DEFAULT_BATCH_SIZE = 10; + + private HttpSourceConfig sourceConfig; + private BlockingQueue<CloudEvent> queue; + private HttpServer server; + + @Override + public Class<? extends Config> configClass() { + return HttpSourceConfig.class; + } + + @Override + public void init(Config config) throws Exception { + this.sourceConfig = (HttpSourceConfig) config; + doInit(); + } + + @Override + public void init(ConnectorContext connectorContext) throws Exception { + SourceConnectorContext sourceConnectorContext = (SourceConnectorContext) connectorContext; + this.sourceConfig = (HttpSourceConfig) sourceConnectorContext.getSourceConfig(); + doInit(); + } + + private void doInit() throws Exception { + this.queue = new LinkedBlockingQueue<>(1000); + + final Vertx vertx = Vertx.vertx(); + final Router router = Router.router(vertx); + router.route() + .path(this.sourceConfig.connectorConfig.getPath()) + .method(HttpMethod.POST) + .handler(LoggerHandler.create()) + .handler(ctx -> { + VertxMessageFactory.createReader(ctx.request()) + .map(MessageReader::toEvent) Review Comment: > Is there a custom conversion method available here to directly convert data into `ConnectorRecord`? This will reduce one data conversion in `poll()`. If possible, you can optimize in this PR or in a new PR. In order to be compliant with [cloudevent-spec](https://github.com/cloudevents/spec/blob/main/cloudevents/bindings/http-protocol-binding.md), converting an http request into `connectRecord` may introduce a lot of http parsing code duplicated in cloudevent-sdk. For future maintenance, I'm wondering if this is worth it or if there is a better way, and if so, maybe we should implement it in a new PR. -- 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]
