Java嵌入式web伺服器(附帶原始碼)

來源:互聯網
上載者:User

    很多java開發的朋友都是用tomcat做主要的伺服器,tomcat無疑有很多優點,tomcat也是最最新的jdk API支援最好的,穩定性相對比較高。但是tomcat的弱點也很明顯,tomcat畢竟不是純java的伺服器,java要和tomcat互動需要通過物理的路徑或相關介面。造成tomcat的使用非常麻煩。

    相信很多人也用過ser-u的ftp伺服器,這個是一個老牌但是非常穩定的伺服器,也很銳意進取。這個伺服器在後期的版本中,他們的控制台又原來的cs模式升級到bs模式,可以說這麼進化卻是革命性的。bs模式的控制台,在升級和最佳化都比cs模式要方便。他們可以用bs作為控制台,但是又沒有部署相關的應用伺服器,就是說他們有嵌入式java伺服器。

    在此文中的應用式java伺服器的主要是通過java main方法來啟動一個嵌入應用伺服器平台,監聽某個連接埠,來持續對本機使用者或者和本機相關區域網路使用者提供應用服務,什麼服務就視乎你部署了什麼樣的應用。、

    嵌入式伺服器的核心類

    package com.shine.framework.HttpServer;</p><p>import org.mortbay.jetty.Connector;<br />import org.mortbay.jetty.Server;<br />import org.mortbay.jetty.nio.SelectChannelConnector;<br />import org.mortbay.jetty.webapp.WebAppContext;<br />import org.mortbay.thread.BoundedThreadPool;</p><p>/**<br /> * HttpServerManager<br /> *<br /> * 相關的包<br /> *<br /> *<br /> * @author viruscodecn@gmail.com<br /> * @project JavaFramework 1.0 2011-03-23<br /> */<br />public class HttpServerManager {<br />private Server server;</p><p>/**<br /> * 初始化伺服器匯入war,串連數預設為100<br /> *<br /> * @param contextName<br /> * @param warPath<br /> * @param port<br /> */<br />public void initJettyHttpServerByWar(String contextName, String warPath,<br />String port) {<br />initJettyHttpServerByWar(contextName, warPath, port, 100);<br />}</p><p>/**<br /> * 初始化伺服器匯入war<br /> *<br /> * @param contextName<br /> * @param warPath<br /> * @param port<br /> * @param threadPoolNum<br /> */<br />@SuppressWarnings("deprecation")<br />public void initJettyHttpServerByWar(String contextName, String warPath,<br />String port, int threadPoolNum) {<br />if (server != null && server.isRunning()) {<br />System.err.println("請關閉http伺服器再重啟");<br />return;<br />}</p><p>try {<br />server = new Server();<br />BoundedThreadPool threadPool = new BoundedThreadPool();<br />// 設定線程池<br />threadPool.setMaxThreads(threadPoolNum);<br />server.setThreadPool(threadPool);<br />// 設定串連參數<br />Connector connector = new SelectChannelConnector();<br />// 設定監聽連接埠<br />connector.setPort(Integer.parseInt(port));<br />server.setConnectors(new Connector[] { connector });<br />WebAppContext context = new WebAppContext();<br />// 訪問項目地址<br />context.setContextPath(contextName);<br />// 啟動的war包<br />context.setWar(warPath);<br />server.addHandler(context);<br />server.setStopAtShutdown(true);<br />server.setSendServerVersion(true);</p><p>server.start();<br />server.join();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}</p><p>/**<br /> * 初始化伺服器通過項目路徑,線程池預設為100<br /> *<br /> * @param contextName<br /> * @param webPath<br /> * @param port<br /> */<br />@SuppressWarnings("deprecation")<br />public void initJettyHttpServer(String contextName, String webPath,<br />String port) {<br />initJettyHttpServer(contextName, webPath, port, 100);<br />}</p><p>/**<br /> * 初始化伺服器通過項目路徑<br /> *<br /> * @param contextName<br /> * @param webPath<br /> * @param port<br /> */<br />@SuppressWarnings("deprecation")<br />public void initJettyHttpServer(String contextName, String webPath,<br />String port, int threadPoolNum) {<br />if (server != null && server.isRunning()) {<br />System.err.println("請關閉http伺服器再重啟");<br />return;<br />}</p><p>try {<br />server = new Server();<br />BoundedThreadPool threadPool = new BoundedThreadPool();<br />// 設定線程池<br />threadPool.setMaxThreads(threadPoolNum);<br />server.setThreadPool(threadPool);<br />// 設定串連參數<br />Connector connector = new SelectChannelConnector();<br />// 設定監聽連接埠<br />connector.setPort(Integer.parseInt(port));<br />server.setConnectors(new Connector[] { connector });<br />WebAppContext context = new WebAppContext(webPath, contextName);<br />server.addHandler(context);<br />server.setStopAtShutdown(true);<br />server.setSendServerVersion(true);</p><p>server.start();<br />server.join();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}</p><p>public void shutdownJettyHttpServer() {<br />if (server == null) {<br />System.err.println("http沒有初始化再重啟");<br />return;<br />}</p><p>try {<br />if (server.isRunning())<br />server.stop();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}</p><p>public void restartJettyHttpServer() {<br />if (server == null) {<br />System.err.println("http沒有初始化再重啟");<br />return;<br />}</p><p>try {<br />if (!server.isRunning())<br />server.start();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}</p><p>}<br />

 

  調用類

package com.shine.framework.HttpServer;</p><p>public class Example {<br />public static void main(String args[]) {<br />HttpServerManager manager = new HttpServerManager();<br />// manager.initJettyHttpServerByWar("/ManageSystemFlex",<br />// "C://Users//123//Desktop//ManageSystemFlex.war", "8080");</p><p>manager.initJettyHttpServer("/ManageSystemFlex",<br />"E://workspace//ManageSystemFlex//WebContent", "8080");<br />}<br />}<br />

 

方法initJettyHttpServerByWar是調用一個war包的web應用程式;

方法initJettyHttpServer是調用一個webcontent的web應用程式;

具體用法再深入研究

 

相關原始碼位置:http://code.google.com/p/ken-javaframeword/source/browse/trunk/JavaFramework2.0/src/com/shine/framework/HtmlManager/HtmlManager.java

  如果大家有其他想法可以留言給我

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.