Jetty架构解析及应用示例
Jetty 是一個(gè)?Web server/servlet?container, 支持?SPDY,WebSocket,?OSGi,?JMX,JNDI,?JAAS?。Jetty非常高效而且靈活,Google App Engine 選擇了Jetty,而放棄了Tomcat,或是其他的服務(wù)器。
Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your application." What this means is that, putting an HTTP module into your application, rather than putting your application into an HTTP server.
Jetty的口號(hào)是:“不要把你的程序部署到Jetty里,而是把Jetty部署到你的程序里”,意味著,你可以把Jetty當(dāng)成程序的一個(gè)HTTP模塊放到你的程序里。
本文先通過(guò)一個(gè)簡(jiǎn)單的HelloWorld示例,展示了java應(yīng)用中的Jetty是如何啟動(dòng)的;接著詳細(xì)分析了Jetty的整體架構(gòu);最后展示了用Jetty啟動(dòng)一個(gè)標(biāo)準(zhǔn)的Java web app。
Hello World 示例
需要的jar包:
jetty-server-8.1.11.v20130520.jar
javax.servlet-3.0.0.v201112011016.jar
jetty-continuation-8.1.11.v20130520.jar
jetty-http-8.1.11.v20130520.jar
jetty-io-8.1.11.v20130520.jar
jetty-util-8.1.11.v20130520.jar
HelloWorldHandler 類(lèi):
package edu.shao.jetty.sample;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler;public class HelloWorldHandler extends AbstractHandler {public void handle(String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {response.setContentType("text/html;charset=utf-8");response.setStatus(HttpServletResponse.SC_OK);baseRequest.setHandled(true);response.getWriter().println("<h1>Hello World</h1>");} }
MyServer 類(lèi):
package edu.shao.jetty.sample;import org.eclipse.jetty.server.Server;public class MyServer {public static void main(String[] args) throws Exception {Server server = new Server(8081); server.setHandler(new HelloWorldHandler()); server.start(); server.join();} }運(yùn)行main()函數(shù),在瀏覽器內(nèi)輸入:http://localhost:8081/?就可以看得結(jié)果。
?
Jetty架構(gòu)
1、整體架構(gòu)圖:
The Jetty Server is the plumbing between a collection of Connectors that accept HTTP connections, and a collection of Handlers that service requests from the connections and produce responses, with the work being done by threads taken from a thread pool.(The concept of a Servlet itself is implemented by a Servlet Handler. ?you can build a Jetty server using only connectors and handlers, without using Servlets.)
2、頂層類(lèi)結(jié)構(gòu):
受JSR77規(guī)范的啟發(fā),Jetty的絕大多數(shù)的組件(Connector, Handler ,Buffer)都實(shí)現(xiàn)了LifeCycle接口。
3、Connectors:
The connectors represent the protocol handlers that accept connections, parse requests and generate responses. The different types of connectors available are based on the protocols, scheduling model and IO APIs used:
1、SocketConnector - for few busy connections or when NIO is not available
2、BlockingChannelConnector - for few busy connections when NIO is available
3、SelectChannelConnector - for many mostly idle connections or asynchronous handling of Ajax requests
4、SslSocketConnector - SSL without NIO
5、SslSelectChannelConnector - SSL with non blocking NIO support
6、AJPConnector - AJP protocol support for connections from apache mod_jk or mod_proxy_ajp
4、Handlers:
The Handler is the component that deals with received requests. Three styles of Handler:?
1、Coordinating Handlers - Handlers that route requests to other handlers (eg HandlerCollection, ContextHandlerCollection)
2、Filtering Handlers - Handlers that augment a request and pass it on to other handlers (eg. HandlerWrapper, ContextHandler, SessionHandler)
3、Generating Handlers - Handlers that produce content (eg ResourceHandler and ServletHandler)
重點(diǎn)Handler:
1、The ServletHandler is a Handler that generates content by passing the request to any configured Filters and then to a Servlet mapped by a URI pattern.
2、A WebAppContext combines handlers for security, session and servlets in a single unit that can be configured with a web.xml descriptor.
你可以順序調(diào)用Handler,或者嵌套調(diào)用Handler,來(lái)處理請(qǐng)求的不同方面。
5、web應(yīng)用
A WebAppContext?supports the standardized layout of a web application and configuration of session, security, listeners, filter, servlets and JSP via a web.xml descriptor normally found in the WEB-INF directory of a webapplication.
?
把Jetty“部署”到Web應(yīng)用中
1、開(kāi)發(fā)時(shí)的部署示例:
這種部署方式還有一個(gè)誘人的特性:項(xiàng)目啟動(dòng)后,如果某個(gè)類(lèi)沒(méi)有被加載到內(nèi)存中,對(duì)這個(gè)類(lèi)的修改在下次該類(lèi)被調(diào)用時(shí)就會(huì)生效,而不用重啟動(dòng)項(xiàng)目;對(duì)JSP的修改,任何時(shí)候都會(huì)在下次被調(diào)用時(shí)生效,而不用重啟項(xiàng)目。這將給開(kāi)發(fā)web應(yīng)用帶來(lái)極大的便利。
1、這是用Maven構(gòu)件的Java Web App項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下:
2、WebappStart 類(lèi):
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext;public class WebappStart {public static void main(String[] args) throws Exception {Server server = new Server(8082);WebAppContext context = new WebAppContext();context.setResourceBase("./src/main/webapp");context.setDescriptor("./src/main/webapp/WEB-INF/web.xml");context.setContextPath("/test2");context.setParentLoaderPriority(true);server.setHandler(context);server.start();server.join();}}WebappStart類(lèi)是整個(gè)項(xiàng)目的入口,運(yùn)行此類(lèi),整個(gè)web項(xiàng)目就啟動(dòng)了。
我們可以體驗(yàn)到,把Jetty嵌入到Web項(xiàng)目中,作為Web Server,十分便利、靈活,并且相比其他服務(wù)器軟件要高效,是開(kāi)發(fā)Web應(yīng)用的首選WebServer。
?
2、用Jetty部署war包
我們?cè)诖耸纠胁渴鹕厦鎤eb工程的war包:TestWebApp2.war
?1、文件目錄的組織形式如下:
2、StartWar.java
import java.io.File; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext;public class StartWar {public static void main(String[] args) throws Exception {String warPath= "E:/jetty/myapp/TestWebApp2.war";//war包的絕對(duì)地址String tempDir=new File(warPath).getParent();//war包所在的目錄,我們使用此目錄為臨時(shí)目錄 Server server = new Server(8083);WebAppContext context = new WebAppContext();//構(gòu)造Context Handler context.setWar(warPath);context.setTempDirectory(new File(tempDir));context.setContextPath("/test");server.setHandler(context);server.start();server.join();} }3、start.bat
前面是設(shè)置必要的變量,最后兩行分別是編譯StartWar.java類(lèi)、運(yùn)行StarWar.class。
@echo off set directory=E:\jetty\ set java_dir=D:\Java\jdk1.6.0_35\ set classp=.;%java_dir%lib\dt.jar;%java_dir%lib\tools.jar;%directory%lib\*;%java_dir%bin\javac -classpath %classp% StartWar.java %java_dir%bin\java -classpath %classp% StartWar4、此時(shí)控制臺(tái)會(huì)顯示如下信息
此時(shí)已經(jīng)部署完了,就是這么簡(jiǎn)單
?
參考:
http://wiki.eclipse.org/Jetty/Tutorial
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
轉(zhuǎn)載于:https://www.cnblogs.com/windlaughing/archive/2013/06/07/3125358.html
總結(jié)
以上是生活随笔為你收集整理的Jetty架构解析及应用示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: sync - 清空文件系统缓冲区
- 下一篇: 五种世界顶级思维-20190303