用户工具

站点工具


light4j:undertow
  • 快速上手
Undertow server = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build();
        server.start();
  • 选项配置
# Options选项:OptionMap.builder().set(Options.TCP_NODELAY,true).getMap()
WORKER_IO_THREADS=CPU*2,接收请求,不能执行阻塞调用,builder.setIoThreads
WORKER_TASK_CORE_THREADS=CPU*10,处理请求,可以执行阻塞调用,builder.setWorkerThreads
System.setProperty("org.jboss.logging.provider", "slf4j"); // LoggerProvider会适配日志实现类log4j2 jdk jboss
  • HttpServerExchange
请求行:方法getRequestMethod 路径getRequestURI
参数:getQueryParameters
请求头:getRequestHeaders,getRequestCookies;Headers.CONTENT_TYPE=multipart/form-data application/x-www-form-urlencoded
正文:getInputStream需要在工作线程调用(isInIoThread->dispatch,startBlocking->FormParserFactory),FormData表单,FormValue字段:isFileItem是否文件,getFileItem().getInputStream()文件内容,getFileName文件名;getValue字段值
响应头:getResponseHeaders,application/json,MimeMappings后缀,ContentType枚举,StatusCodes状态码,ResponseCodeHandler
响应:getResponseSender().send(string),getOutputStream().write(bs).close(),endExchange结束,isComplete是否已结束
light4j/undertow.txt · 最后更改: 2022/03/11 18:28 由 admin