Jetty源碼分析-Server

在熟悉server部分源碼之前,我們先看看Server主要干什么?
Jetty 官網對它的簡單描述:"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 threads from a thread pool doing the work",簡單明了,server就是一個管道,管道的一頭是負責 接受各種http連接請求的Connector組件,另外一頭就是一堆的Handler,這些handler就負責處理來自Connetor的各種request請求,它維護了一個線程池來完成 真正的請求任務處理并將結果返回client。所以說白了,Server組件主要工作就是負責打通Connector和Handler.


1. Jetty Server配置

廢話不多說,直接上一段

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <Get class="java.lang.System" name="out">
        <Call name="println">
            <Arg>Redirecting stderr/stdout to <Ref id="ServerLogName"/></Arg>
        </Call>
    </Get>

    <Get name="ThreadPool">
        <Set name="minThreads" type="int">20</Set>
        <Set name="maxThreads" type="int">200</Set>
        <Set name="name">jetty-worker</Set>
    </Get>

    <Set name="connectors">
        <Array type="org.eclipse.jetty.server.Connector">
            <Item>
                <New class="org.eclipse.jetty.server.ServerConnector">
                    <Arg><Ref refid="Server"/></Arg>
                    <Set name="port">8080</Set>
                    <Set name="host"><!--<SystemProperty name="jetty.host"/>--></Set>
                </New>
            </Item>
        </Array>
    </Set>

    <Array id="plusConfig" type="java.lang.String">
        <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
        <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
        <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
        <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
        <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
        <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
        <Item>org.eclipse.jetty.annotations.AnnotationConfiguration</Item>
        <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
    </Array>

    <Set name="handler">
        <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
            <Set name="handlers">
                <Array type="org.eclipse.jetty.server.Handler">
                    <Item>
                        <New class="org.eclipse.jetty.webapp.WebAppContext">
                            <Set name="resourceBase">src/main/webapp</Set>
                            <Set name="contextPath">/</Set>
                            <Set name="parentLoaderPriority">true</Set>
                            <Set name="descriptor">src/main/webapp/WEB-INF/web.xml</Set>
                            <Set name="configurationClasses"><Ref id="plusConfig"/></Set>
                        </New>
                    </Item>
                </Array>
            </Set>
        </New>
    </Set>

    <Set name="stopAtShutdown">true</Set>
    <Set name="stopTimeout">1000</Set>
    <!--如果打開了會打非常恐怖的日志,導致啟動日志沒法看了-->
    <Set name="dumpAfterStart">false</Set>
    <Set name="dumpBeforeStop">false</Set>
</Configure>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容