Pil0tXia commented on code in PR #4739: URL: https://github.com/apache/eventmesh/pull/4739#discussion_r1455740634
########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/EventMeshAdminServer.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.runtime.boot; + +import org.apache.eventmesh.runtime.admin.controller.HttpHandlerManager; +import org.apache.eventmesh.runtime.configuration.EventMeshHTTPConfiguration; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.epoll.EpollServerSocketChannel; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpRequestDecoder; +import io.netty.handler.codec.http.HttpResponseEncoder; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class EventMeshAdminServer extends AbstractHTTPServer { + + private HttpConnectionHandler httpConnectionHandler; + + HttpHandlerManager httpHandlerManager; + + public EventMeshAdminServer(int port, boolean useTLS, + EventMeshHTTPConfiguration eventMeshHttpConfiguration, HttpHandlerManager httpHandlerManager) { + super(port, useTLS, eventMeshHttpConfiguration); + this.httpHandlerManager = httpHandlerManager; + } + + @Override + public void init() throws Exception { + super.init("eventMesh-admin-http"); + httpConnectionHandler = new HttpConnectionHandler(); + } + + @Override + public void start() throws Exception { + final Thread thread = new Thread(() -> { + final ServerBootstrap bootstrap = new ServerBootstrap(); + try { + bootstrap.group(this.getBossGroup(), this.getIoGroup()) + .channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class) + .childHandler(new AdminServerInitializer(httpHandlerManager)) + .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); + + log.info("AdminHttpServer[port={}] started.", this.getPort()); + + bootstrap.bind(this.getPort()) + .channel() + .closeFuture() + .sync(); + } catch (Exception e) { + log.error("AdminHttpServer start error!", e); + try { + shutdown(); + } catch (Exception ex) { + log.error("AdminHttpServer shutdown error!", ex); + } + System.exit(-1); + } + }, "EventMesh-http-server"); + thread.setDaemon(true); + thread.start(); + started.compareAndSet(false, true); + } Review Comment: L86, better not to edit the startup status boolean of an abstract class (AbstractHTTPServer). ########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java: ########## @@ -139,13 +138,14 @@ public AbstractHTTPServer(final int port, final boolean useTLS, this.httpThreadPoolGroup = new HTTPThreadPoolGroup(eventMeshHttpConfiguration); } - private void initSharableHandlers() { + protected void initSharableHandlers() { httpConnectionHandler = new HttpConnectionHandler(); Review Comment: There is no need to decorate this method with `protected`. ########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractRemotingServer.java: ########## @@ -48,7 +49,7 @@ public abstract class AbstractRemotingServer implements RemotingServer { private int port; - private void buildBossGroup(final String threadPrefix) { + protected void buildBossGroup(final String threadPrefix) { if (useEpoll()) { Review Comment: There is no need to decorate this method with `protected`. ########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/controller/HttpHandlerManager.java: ########## @@ -17,49 +17,238 @@ package org.apache.eventmesh.runtime.admin.controller; +import org.apache.eventmesh.runtime.boot.AbstractHTTPServer; import org.apache.eventmesh.runtime.common.EventHttpHandler; +import org.apache.eventmesh.runtime.util.Utils; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.URI; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.HttpVersion; + +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; -import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.HttpPrincipal; + +import lombok.extern.slf4j.Slf4j; /** - * This class manages the registration of {@linkplain com.sun.net.httpserver.HttpHandler HttpHandler} - * for an {@linkplain com.sun.net.httpserver.HttpServer HttpServer}. + * This class manages the registration of {@linkplain com.sun.net.httpserver.HttpHandler HttpHandler} for an {@linkplain + * com.sun.net.httpserver.HttpServer HttpServer}. */ +@Slf4j public class HttpHandlerManager { private final List<HttpHandler> httpHandlers = new ArrayList<>(); + private final Map<String, HttpHandler> httpHandlerMap = new ConcurrentHashMap<>(); + + private AbstractHTTPServer abstractHTTPServer; + + public void bind(AbstractHTTPServer abstractHTTPServer) { + this.abstractHTTPServer = abstractHTTPServer; Review Comment: AbstractHTTPServer should not be a member of HttpHandlerManager. ########## eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/boot/AbstractHTTPServer.java: ########## @@ -481,7 +481,7 @@ public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cau } @Sharable - private class HttpConnectionHandler extends ChannelDuplexHandler { + protected class HttpConnectionHandler extends ChannelDuplexHandler { Review Comment: Is there any way to keep it private here? -- 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]
