Firefly FrameworkJava異步Web框架
什么是Firefly?
Firefly是一個Java異步Web框架,它能幫助您方便和快速的創(chuàng)建web應用。其主要功能包括:異步HTTP服務器/客戶端,異步TCP服務器/客戶端,數(shù)據(jù)庫訪問,IOC框架等。部署Firefly不需要任何額外的web容器。Firefly使用高度可伸縮的SEDA架構能充分發(fā)揮硬件的性能。
事件驅(qū)動
傳統(tǒng)的阻塞模型會消耗大量的線程,從而導致占用的大量內(nèi)存和上下文切換開銷。Firefly的API使用事件驅(qū)動模型,用很少的線程去處理很高的并發(fā)請求。
函數(shù)編程
Firefly提供了函數(shù)風格和鏈式調(diào)用API來編寫網(wǎng)絡應用程序,它可以讓您使用極簡主義的代碼,流暢的開發(fā)網(wǎng)絡應用程序。例如:
public class HelloHTTPServerAndClient {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
HTTP2ServerBuilder httpServer = $.httpServer();
httpServer.router().get("/").handler(ctx -> ctx.write("hello world! ").next())
.router().get("/").handler(ctx -> ctx.end("end message"))
.listen("localhost", 8080);
$.httpClient().get("http://localhost:8080/").submit()
.thenAccept(res -> System.out.println(res.getStringBody()))
.thenAccept(res -> phaser.arrive());
phaser.arriveAndAwaitAdvance();
httpServer.stop();
$.httpClient().stop();
}
}
Kotlin支持
Firefly同樣提供了Kotlin DSL風格的API,Kotlin DSL以半聲明的方式構造程序,能清晰的表達程序的結(jié)構和意圖。例如:
fun main(args: Array) {
HttpServer {
router {
httpMethod = HttpMethod.GET
path = "/"
asyncHandler {
end("hello world!")
}
}
}.listen("localhost", 8080)
}
fun main(args: Array): Unit = runBlocking {
val msg = firefly.httpClient().get("http://localhost:8080").asyncSubmit().stringBody
println(msg)
}
Firefly Kotlin HTTP 服務器和客戶端使用協(xié)程(coroutine)消除回調(diào)風格的代碼,能讓程序變得更簡單清晰,并保留了異步IO的性能與伸縮性。
更多詳細的用例可以在Firefly的文檔中找到。
評論
圖片
表情
