Java嵌入式web服務器(附帶源代碼)

  不少java開發的朋友都是用tomcat作主要的服務器,tomcat無疑有不少優勢,tomcat也是最最新的jdk API支持最好的,穩定性相對比較高。可是tomcat的弱點也很明顯,tomcat畢竟不是純java的服務器,java要和tomcat交互須要經過物理的路徑或相關接口。形成tomcat的使用很是麻煩。java

    相信不少人也用過ser-u的ftp服務器,這個是一個老牌可是很是穩定的服務器,也很銳意進取。這個服務器在後期的版本中,他們的控制檯又原來的cs模式升級到bs模式,能夠說這麼進化倒是革命性的。bs模式的控制檯,在升級和優化都比cs模式要方便。他們能夠用bs做爲控制檯,可是又沒有部署相關的應用服務器,就是說他們有嵌入式java服務器。web

    在此文中的應用式java服務器的主要是經過java main方法來啓動一個嵌入應用服務器平臺,監聽某個端口,來持續對本機用戶或者和本機相關局域網用戶提供應用服務,什麼服務就視乎你部署了什麼樣的應用。、tomcat

    嵌入式服務器的核心類服務器

   

[java] view plain copy
  1. package com.shine.framework.HttpServer;  
  2.   
  3. import org.mortbay.jetty.Connector;  
  4. import org.mortbay.jetty.Server;  
  5. import org.mortbay.jetty.nio.SelectChannelConnector;  
  6. import org.mortbay.jetty.webapp.WebAppContext;  
  7. import org.mortbay.thread.BoundedThreadPool;  
  8.   
  9. /** 
  10.  * HttpServerManager 
  11.  *  
  12.  * 相關的包 
  13.  *  
  14.  *  
  15.  * @author viruscodecn@gmail.com 
  16.  * @project JavaFramework 1.0 2011-03-23 
  17.  */  
  18. public class HttpServerManager {  
  19.     private Server server;  
  20.   
  21.     /** 
  22.      * 初始化服務器導入war,鏈接數默認爲100 
  23.      *  
  24.      * @param contextName 
  25.      * @param warPath 
  26.      * @param port 
  27.      */  
  28.     public void initJettyHttpServerByWar(String contextName, String warPath,  
  29.             String port) {  
  30.         initJettyHttpServerByWar(contextName, warPath, port, 100);  
  31.     }  
  32.   
  33.     /** 
  34.      * 初始化服務器導入war 
  35.      *  
  36.      * @param contextName 
  37.      * @param warPath 
  38.      * @param port 
  39.      * @param threadPoolNum 
  40.      */  
  41.     @SuppressWarnings("deprecation")  
  42.     public void initJettyHttpServerByWar(String contextName, String warPath,  
  43.             String port, int threadPoolNum) {  
  44.         if (server != null && server.isRunning()) {  
  45.             System.err.println("請關閉http服務器再重啓");  
  46.             return;  
  47.         }  
  48.   
  49.         try {  
  50.             server = new Server();  
  51.             BoundedThreadPool threadPool = new BoundedThreadPool();  
  52.             // 設置線程池  
  53.             threadPool.setMaxThreads(threadPoolNum);  
  54.             server.setThreadPool(threadPool);  
  55.             // 設置鏈接參數  
  56.             Connector connector = new SelectChannelConnector();  
  57.             // 設置監聽端口  
  58.             connector.setPort(Integer.parseInt(port));  
  59.             server.setConnectors(new Connector[] { connector });  
  60.             WebAppContext context = new WebAppContext();  
  61.             // 訪問項目地址  
  62.             context.setContextPath(contextName);  
  63.             // 啓動的war包  
  64.             context.setWar(warPath);  
  65.             server.addHandler(context);  
  66.             server.setStopAtShutdown(true);  
  67.             server.setSendServerVersion(true);  
  68.   
  69.             server.start();  
  70.             server.join();  
  71.         } catch (Exception e) {  
  72.             e.printStackTrace();  
  73.         }  
  74.     }  
  75.   
  76.     /** 
  77.      * 初始化服務器經過項目路徑,線程池默認爲100 
  78.      *  
  79.      * @param contextName 
  80.      * @param webPath 
  81.      * @param port 
  82.      */  
  83.     @SuppressWarnings("deprecation")  
  84.     public void initJettyHttpServer(String contextName, String webPath,  
  85.             String port) {  
  86.         initJettyHttpServer(contextName, webPath, port, 100);  
  87.     }  
  88.   
  89.     /** 
  90.      * 初始化服務器經過項目路徑 
  91.      *  
  92.      * @param contextName 
  93.      * @param webPath 
  94.      * @param port 
  95.      */  
  96.     @SuppressWarnings("deprecation")  
  97.     public void initJettyHttpServer(String contextName, String webPath,  
  98.             String port, int threadPoolNum) {  
  99.         if (server != null && server.isRunning()) {  
  100.             System.err.println("請關閉http服務器再重啓");  
  101.             return;  
  102.         }  
  103.   
  104.         try {  
  105.             server = new Server();  
  106.             BoundedThreadPool threadPool = new BoundedThreadPool();  
  107.             // 設置線程池  
  108.             threadPool.setMaxThreads(threadPoolNum);  
  109.             server.setThreadPool(threadPool);  
  110.             // 設置鏈接參數  
  111.             Connector connector = new SelectChannelConnector();  
  112.             // 設置監聽端口  
  113.             connector.setPort(Integer.parseInt(port));  
  114.             server.setConnectors(new Connector[] { connector });  
  115.             WebAppContext context = new WebAppContext(webPath, contextName);  
  116.             server.addHandler(context);  
  117.             server.setStopAtShutdown(true);  
  118.             server.setSendServerVersion(true);  
  119.   
  120.             server.start();  
  121.             server.join();  
  122.         } catch (Exception e) {  
  123.             e.printStackTrace();  
  124.         }  
  125.     }  
  126.   
  127.     public void shutdownJettyHttpServer() {  
  128.         if (server == null) {  
  129.             System.err.println("http沒有初始化再重啓");  
  130.             return;  
  131.         }  
  132.   
  133.         try {  
  134.             if (server.isRunning())  
  135.                 server.stop();  
  136.         } catch (Exception e) {  
  137.             e.printStackTrace();  
  138.         }  
  139.     }  
  140.   
  141.     public void restartJettyHttpServer() {  
  142.         if (server == null) {  
  143.             System.err.println("http沒有初始化再重啓");  
  144.             return;  
  145.         }  
  146.   
  147.         try {  
  148.             if (!server.isRunning())  
  149.                 server.start();  
  150.         } catch (Exception e) {  
  151.             e.printStackTrace();  
  152.         }  
  153.     }  
  154.   
  155. }  

 

  調用類app

[java] view plain copy
  1. package com.shine.framework.HttpServer;  
  2.   
  3. public class Example {  
  4.     public static void main(String args[]) {  
  5.         HttpServerManager manager = new HttpServerManager();  
  6.         // manager.initJettyHttpServerByWar("/ManageSystemFlex",  
  7.         // "C://Users//123//Desktop//ManageSystemFlex.war", "8080");  
  8.   
  9.         manager.initJettyHttpServer("/ManageSystemFlex",  
  10.                 "E://workspace//ManageSystemFlex//WebContent", "8080");  
  11.     }  
  12. }  

 

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

方法initJettyHttpServer是調用一個webcontent的web應用程序;優化

具體用法再深刻研究spa

相關文章
相關標籤/搜索