Asity 是 Java 虚拟机上各种 Web 框架的抽象层,可以在 JVM 上构建 Web 框架不可知的应用程序。
Asity 是 Web 框架的一个轻量级抽象层,它被设计用来构建应用程序和框架,这些应用程序和框架可以在 JVM 上的任何全栈框架,任何微型框架或任何原始服务器上运行,而不会降低底层框架的性能。它提供了 HTTP 和 WebSocket 抽象。
io.cettia.asity:asity-http 提供 HTTP 抽象,下边是 echo HTTP 服务器示例:
io.cettia.asity:asity-http
Action<ServerHttpExchange> httpAction = (ServerHttpExchange http) -> { // Request properties System.out.println(http.method() + " " + http.uri()); http.headerNames().stream().forEach(name -> System.out.println(name + ": " + http.header(name))); // Sets 200 OK response status http.setStatus(HttpStatus.OK); // Copies the content-type header of the request to the response http.setHeader("content-type", http.header("content-type")); // When a chunk is read from the request body, writes it to the response body http.onchunk((ByteBuffer bytes) -> http.write(bytes)); // When the request is fully read, ends the response http.onend((Void v) -> http.end()); // Reads the request body as binary to circumvent encoding issue http.readAsBinary(); // When the response is fully written and ends, http.onfinish((Void v) -> System.out.println("on finish")); // When some error happens in the request-response exchange, http.onerror((Throwable t) -> t.printStackTrace()); // When the underlying connection is terminated, http.onclose((Void v) -> System.out.println("on close")); };
io.cettia.asity:asity-websocket 提供 WebSocket 抽象,下边是 echo WebSocket 服务器示例:
io.cettia.asity:asity-websocket
Action<ServerWebSocket> wsAction = (ServerWebSocket ws) -> { // Handshake request properties System.out.println(HttpMethod.GET + " " + ws.uri()); ws.headerNames().stream().forEach(name -> System.out.println(name + ": " + ws.header(name))); // When a text frame is arrived, sends it back ws.ontext((String data) -> ws.send(data)); // When a binary frame is arrived, sends it back ws.onbinary((ByteBuffer bytes) -> ws.send(bytes)); // When some error happens in the connection, ws.onerror((Throwable t) -> t.printStackTrace()); // When the connection is closed for any reason, ws.onclose((Void v) -> System.out.println("on close")); };
io.cettia.asity:asity-bridge-xxx 是一个处理和转换框架特定资源到 Asity的 ServerHttpExchange 和 ServerWebSocket 的模块。下面这些 Bridge 都是可用的,也就是说基于 Asity 的应用程序或框架可以在以下框架中无缝地运行:
io.cettia.asity:asity-bridge-xxx
ServerHttpExchange
ServerWebSocket
Atmosphere 2
Grizzly 2
Java Servlet 3
Java WebSocket API 1
Netty 4
Spring WebFlux 5
Vert.x 2 and 3