Undertow —— JBOSS 的社區版,redhat 下場維護的開源項目,頂頂好用的 Java web server

Undertow · JBoss Community

Undertow
Undertow is a flexible performant web server written in java, providing both blocking and non-blocking API’s based on NIO.

Undertow 是一個用 Java 編寫的靈活高性能 Web 服務器,提供基于 NIO 的阻塞和非阻塞 API。

Undertow has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet 4.0 container, or a low level non-blocking handler, to anything in between.

Undertow采用基于組合的架構,允許您通過組合小型單一功能處理器來構建Web服務器。這種架構讓您能夠靈活選擇——無論是完整的Java EE Servlet 4.0容器,還是底層的非阻塞處理器,抑或是介于兩者之間的任何方案。(備注: Oracle Java EE 是收訂閱費的。)

Undertow is designed to be fully embeddable, with easy to use fluent builder APIs. Undertow’s lifecycle is completely controlled by the embedding application.

Undertow 設計為完全可嵌入式,提供易于使用的流式構建器 API。其生命周期完全由嵌入應用控制。

Undertow is sponsored by JBoss and is the default web server in the Wildfly Application Server.

文檔? Online?Undertow

There are two main ways that Undertow can be used, either by directly embedding it in your code, or as part of the Wildfly Application Server. This guide mostly focuses on the embedded API’s, although a lot of the content is still relevant if you are using Wildfly, it is just that the relevant functionality will generally be exposed via XML configuration rather than programmatic configuration.

Undertow主要有兩種使用方式:一種是直接嵌入到代碼中,另一種是作為Wildfly應用服務器的一部分。本指南主要聚焦于嵌入式API,盡管大部分內容對Wildfly用戶同樣適用,只是相關功能通常通過XML配置而非編程配置來實現。

HTTP/2 Support
Undertow supports HTTP/2 out of the box, no overriding the boot class path required.

Undertow 開箱即支持 HTTP/2,無需覆蓋引導類路徑。

HTTP Upgrade Support
Support for HTTP upgrade, to allow multiple protocols to be multiplexed over the HTTP port.

支持HTTP升級,允許多種協議在HTTP端口上實現多路復用。

Web Socket Support
Undertow provides full support for Web Sockets, including JSR-356 support.

Undertow 全面支持 Web Sockets,包括對 JSR-356 的支持。

Servlet 4.0
Undertow provides support for Servlet 4.0, including support for embedded servlet. It is also possible to mix both Servlets and native undertow non-blocking handlers in the same deployment.

Undertow 支持 Servlet 4.0,包括對嵌入式 servlet 的支持。在同一部署中也可以混合使用 Servlets 和原生的 undertow 非阻塞處理程序。

Embeddable
Undertow can be embedded in an application or run standalone with just a few lines of code.

Undertow 可以嵌入到應用程序中,也可以僅用幾行代碼獨立運行。

Flexible
An Undertow server is configured by chaining handlers together. It is possible to add as much or as little functionality as you need, so you don’t pay for what you are not using.

Undertow服務器的配置通過將處理器鏈式連接來實現。您可以根據需求自由增減功能模塊,從而避免為未使用的功能付出額外開銷。

啟動

public class HelloWorldServer {public static void main(final String[] args) {Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(new HttpHandler() {@Overridepublic void handleRequest(final HttpServerExchange exchange) throws Exception {exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");exchange.getResponseSender().send("Hello World");}}).build();server.start();}
}

處理 500 錯誤

public class SimpleErrorPageHandler implements HttpHandler {private final HttpHandler next;public SimpleErrorPageHandler(final HttpHandler next) {this.next = next;}@Overridepublic void handleRequest(final HttpServerExchange exchange) throws Exception {exchange.addDefaultResponseListener(new DefaultResponseListener() {@Overridepublic boolean handleDefaultResponse(final HttpServerExchange exchange) {if (!exchange.isResponseChannelAvailable()) {return false;}Set<Integer> codes = responseCodes;if (exchange.getResponseCode() == 500) {final String errorPage = "<html><head><title>Error</title></head><body>Internal Error</body></html>";exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + errorPage.length());exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");Sender sender = exchange.getResponseSender();sender.send(errorPage);return true;}return false;}});next.handleRequest(exchange);}
}

In order to use Undertow in your maven projects just include the following section in your pom.xml, and set the undertow.version property to whatever version of Undertow you wish to use. Only the core artifact is required, if you are not using Servlet or JSR-356 then those artifacts are not required.

要在您的 Maven 項目中使用 Undertow,只需在 pom.xml 文件中包含以下部分,并將 undertow.version 屬性設置為您希望使用的 Undertow 版本。僅需核心構件(core artifact),如果您不使用 Servlet 或 JSR-356,則無需包含這些構件。

<dependency><groupId>io.undertow</groupId><artifactId>undertow-core</artifactId><version>${undertow.version}</version>
</dependency>
<dependency><groupId>io.undertow</groupId><artifactId>undertow-servlet</artifactId><version>${undertow.version}</version>
</dependency>
<dependency><groupId>io.undertow</groupId><artifactId>undertow-websockets-jsr</artifactId><version>${undertow.version}</version>
</dependency>

Undertow depends on XNIO and JBoss Logging, which will need to be downloaded as well.

Undertow依賴于XNIO和JBoss Logging,這些也需要下載。

https://github.com/jboss-logging/jboss-logging?

XNIO - JBoss Community

Bootstrapping Undertow
There are two ways to bootstrap Undertow. The first and most simple is to use the io.undertow.Undertow builder API. The second is to assemble a server using XNIO and the Undertow listener classes directly. This second approach requires more code, but gives more flexibility. It is anticipated that for most use cases the builder API will be sufficient.

有兩種方式來引導Undertow。第一種也是最簡單的方式是使用io.undertow.Undertow構建器API。第二種是直接使用XNIO和Undertow監聽器類來組裝服務器。第二種方法需要編寫更多代碼,但提供了更大的靈活性。預計對于大多數用例來說,構建器API已經足夠。

One thing that it is important to understand about Undertow is that there is not really any concept of an Undertow container. Undertow applications are assembled from multiple handler classes, and it is up to the embedding application to manage the lifecycle of all the these handlers. This was a deliberate design decision in order to give the embedding application as much control as possible. This is generally only an issue if you have handlers that hold resources that need to be cleaned up at server stop.

關于Undertow需要理解的重要一點是,它實際上并不存在"Undertow容器"的概念。Undertow應用程序由多個處理器類組裝而成,而這些處理器的生命周期管理完全取決于嵌入應用程序。這是一項經過深思熟慮的設計決策,目的是為了賦予嵌入應用程序最大限度的控制權。通常只有當您使用的處理器持有需要在服務器停止時清理的資源時,這才成為一個需要考慮的問題。

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;public class HelloWorldServer {public static void main(final String[] args) {Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(new HttpHandler() {@Overridepublic void handleRequest(final HttpServerExchange exchange) throws Exception {exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");exchange.getResponseSender().send("Hello World");}}).build();server.start();}
}

Why isn’t Undertow based on Mina, Netty, RNIO, Grizzly, or <insert network framework>?
In order to best achieve its goals, Undertow requires very close integration with the underlying I/O infrastructure in the Java platform. The simplicity offered by any abstraction comes from hiding the underlying mechanisms behind it. However, the dilemma is that building an extremely efficient and flexible web server requires customization and control of these mechanisms. Undertow attempts to strike the right balance by reusing a minimalistic I/O library, XNIO, that was created for WildFly’s remote invocation layer.

為了最好地實現其目標,Undertow需要與Java平臺中的底層I/O基礎設施進行非常緊密的集成。任何抽象所提供的簡單性都源于對其背后底層機制的隱藏。然而,難題在于構建一個極其高效且靈活的Web服務器需要對這些機制進行定制和控制。Undertow嘗試通過復用為WildFly遠程調用層創建的極簡I/O庫XNIO,來達到恰當的平衡。

XNIO allows us to eliminate some boiler plate, and also allows for direct I/O integration with the operating system, but it does not go further than that. In addition, XNIO offers very strong backwards compatibility which is important since this is also a concern for the Undertow project. Of course, other projects may have different needs, and thus might make different choices.

XNIO 讓我們能夠減少一些樣板代碼,并實現與操作系統的直接 I/O 集成,但它的功能也僅限于此。此外,XNIO 提供了極強的向后兼容性,這一點非常重要,因為這也是 Undertow 項目關注的重點。當然,其他項目可能有不同的需求,因此可能會做出不同的選擇。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/94183.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/94183.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/94183.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【AI智能體】Dify 搭建業務單據差異核對助手實戰詳解

目錄 一、前言 二、Dify介紹 2.1 Dify 是什么 2.2 Dify 核心特性 2.2.1 Dify特點 2.2.2 Dify 多模型支持 2.2.3 Dify 適應場景 2.2.4 基于Dify 搭建發票識別應用優勢 三、Dify 搭建業務單據核對助手實戰過程 3.1 前置準備 3.1.1 安裝必要的插件 3.2 完整操作步驟 3…

Centos編譯安裝Python3.10

gcc編譯源碼包 下載python源碼包并解壓 wget https://www.python.org/ftp/python/3.10.18/Python-3.10.18.tgz tar -xf Python-3.10.18.tgz cd Python-3.10.18系統編譯依賴環境安裝 sudo yum install zlib-devel ncurses-devel gdbm-devel nss-devel openssl-devel readline-de…

Maya 3D建模 導入參考圖、鎖定參考圖

1 導入參考圖切換到 前視圖 或者 側視圖 導入 &#xff08;根據參考圖片類別去選擇&#xff09;方法1&#xff1a;視圖--圖像平面--導入圖像方法2&#xff1a;直接點 圖像平面 備注&#xff1a;誤操作導致看不到 解決辦法&#xff1a;顯示--視口 找對應的2 鎖定參考圖目的&…

基于單片機智能加濕器/空氣加濕器

傳送門 &#x1f449;&#x1f449;&#x1f449;&#x1f449;其他作品題目速選一覽表 &#x1f449;&#x1f449;&#x1f449;&#x1f449;其他作品題目功能速覽 概述 基于單片機的智能加濕器通過集成溫濕度傳感器、控制模塊和霧化裝置&#xff0c;實現環境濕度的自…

SNDR:高精度ADC系統的綜合性能標尺

SNDR&#xff1a;高精度ADC系統的綜合性能標尺 一、SNDR的本質定義與理論基礎 信噪失真比(Signal-to-Noise-and-Distortion Ratio) 是評估ADC系統綜合性能的核心指標&#xff0c;定義為信號功率與噪聲及失真功率之和的比值&#xff1a; SNDRdB10log?10(PsignalPnoisePdistorti…

2025年滲透測試面試題總結-31(題目+回答)

安全領域各種資源&#xff0c;學習文檔&#xff0c;以及工具分享、前沿信息分享、POC、EXP分享。不定期分享各種好玩的項目及好用的工具&#xff0c;歡迎關注。 目錄 一、代碼審計核心思路&#xff08;261&#xff09; 二、MySQL Getshell前提&#xff08;262&#xff09; …

[創業之路-560]:機械、電氣、自控、電子、軟件、信息、通信、大數據、人工智能,上述技術演進過程

上述關鍵詞反映的技術演進過程可梳理為一條從機械執行到智能決策的遞進式發展主線&#xff0c;各技術領域在不同階段相互滲透、共同推動機器人技術從功能替代向認知革命躍遷。以下是具體演進邏輯與趨勢分析&#xff1a;一、技術演進的三階段遞進機械主導階段&#xff08;工業革…

芋道前端項目部署后刷新 404 的解決辦法(Nginx 配置教程)

很多同學在把 芋道前端項目 部署到服務器后&#xff0c;會遇到一個奇怪的問題&#xff1a; &#x1f449; 項目首頁能正常訪問&#xff0c;但一旦在瀏覽器里手動刷新某個頁面&#xff0c;就會報 404 Not Found 錯誤。 這到底是為什么呢&#xff1f;又該怎么解決呢&#xff1f;下…

更適合后端寶寶的前端三件套之HTML

文章目錄&#x1f4d5;1. HTML基礎??1.1 什么是HTML??1.2 認識HTML標簽??1.3 HTML文件基本結構??1.4 標簽層次結構&#x1f4d5;2. HTML常見標簽??2.1 標題標簽??2.2 段落標簽??2.3 換行標簽??2.4 圖片標簽??2.5 超鏈接標簽??2.6 表格標簽&#x1f4d5;3. …

【JVM內存結構系列】四、不同垃圾回收器與堆內存的適配關系:從分代GC到Region GC

在JVM內存體系中&#xff0c;堆內存的“分代結構”與“對象流轉規則”是通用基礎&#xff0c;但垃圾回收器&#xff08;GC&#xff09;是決定堆內存實際表現的核心變量——不同GC為實現“低延遲”“高吞吐量”等目標&#xff0c;會對堆的劃分方式、對象管理邏輯、參數配置規則進…

Zemax光學設計輸出3D

輸出立體數據文件&#xff08;IGES/STEP/SAT/STL 格式&#xff09;的參數設置界面&#xff0c;各參數含義如下&#xff1a;1. 起始面/終止面&#xff1a;設定要輸出立體數據對應的光學表面范圍&#xff0c;從第 0 個表面到第 9 個表面 &#xff0c;限定參與輸出的光學結構表面區…

模塊測試與低功耗模式全攻略

一、模塊測試流程在測試一個模塊時&#xff0c;建議遵循以下步驟&#xff1a;基本測試&#xff1a;測試該模塊的寄存器讀寫功能是否正常。可以向每個寄存器寫入 0x5A5A 和 0xA5A5&#xff0c;這兩種模式可以覆蓋對寄存器寫入 0 和 1 的情況。進階測試&#xff1a;在基本測試通過…

機器學習實驗三、使用決策樹算法預測泰坦尼克號幸存者

實驗目的1. 掌握特征工程&#xff0c;會進行特征提取與特征選擇&#xff0c;會進行缺失值填充。2. 建立決策樹模型&#xff0c;解決實際問題。3. 會對模型進行調試&#xff0c;能夠繪制并保存決策樹。實驗環境Python 3.7.0&#xff0c;Sklearn &#xff0c;PyCharm實驗原理1、特…

從全棧開發到微服務架構:一次真實的Java面試實錄

從全棧開發到微服務架構&#xff1a;一次真實的Java面試實錄 面試官與應聘者介紹 面試官&#xff1a;李明&#xff0c;某互聯網大廠技術負責人&#xff0c;擅長Java后端、微服務及云原生架構。 應聘者&#xff1a;張偉&#xff0c;28歲&#xff0c;碩士學歷&#xff0c;擁有5年…

新的 Gmail 網絡釣魚攻擊利用 AI 提示注入來逃避檢測

網絡釣魚一直以來都是為了欺騙人們。但在這次活動中&#xff0c;攻擊者不僅瞄準用戶&#xff0c;還試圖操縱基于人工智能的防御系統。 這是我上周記錄的Gmail 網絡釣魚鏈的演變。那次攻擊活動依賴于緊迫性和重定向&#xff0c;但這次引入了隱藏的 AI 提示&#xff0c;旨在混淆…

Restful風格設計

文章目錄什么是Restful風格&#xff1f;RESTful API設計最佳實踐1. URL設計原則2. HTTP狀態碼的正確使用3. 統一的響應格式實際案例&#xff1a;用戶管理系統API總結什么是Restful風格&#xff1f; 我的理解是&#xff1a;Restful是一種基于HTTP協議的架構設計風格&#xff0c…

深入 Glide 圖像變換:自定義效果、GIF處理與組合變換

在 Android 開發中&#xff0c;Glide 的強大不僅在于其高效的加載和緩存能力&#xff0c;更在于其無與倫比的可擴展性&#xff0c;尤其是在圖像處理層面。當內置的 fitCenter() 和 circleCrop() 無法滿足你的設計需求時&#xff0c;自定義 Transformation 便是你的終極武器。本…

數據挖掘 4.8 評估泛化能力

4.8 Estimating Generalization 4.8 評估泛化能力 評估模型的泛化能力如何合理評估模型的泛化能力指導原則 (Guidelines)存在的問題 (Issues)K-fold 交叉驗證&#xff08;Cross-Validation)留一交叉驗證&#xff08;Leave One Out CV&#xff09;(LOOCV)Stratification 分層訓練…

46.【.NET8 實戰--孢子記賬--從單體到微服務--轉向微服務】--擴展功能--集成網關--網關集成日志

本篇文章&#xff0c;我們一起在網關中集成日志功能&#xff0c;我們要在網關中記錄下游微服務出現的異常信息、請求信息以及響應信息。在微服務架構中&#xff0c;網關作為系統的入口&#xff0c;承擔著非常重要的職責。通過在網關層面集成日志功能&#xff0c;我們可以更好地…

使用 FastAPI 的 WebSockets 和 Elasticsearch 來構建實時應用

作者&#xff1a;來自 Elastic Jeffrey Rengifo 學習如何使用 FastAPI WebSockets 和 Elasticsearch 構建實時應用程序。 更多閱讀&#xff1a;使用 FastAPI 構建 Elasticsearch API 想要獲得 Elastic 認證嗎&#xff1f;看看下一次 Elasticsearch Engineer 培訓什么時候開始&…