很多java开发的朋友都是用tomcat做主要的服务器,tomcat无疑有很多优点,tomcat也是最最新的jdk API支持最好的,稳定性相对比较高。但是tomcat的弱点也很明显,tomcat毕竟不是纯java的服务器,java要和tomcat交互需要通过物理的路径或相关接口。造成tomcat的使用非常麻烦。

    相信很多人也用过ser-u的ftp服务器,这个是一个老牌但是非常稳定的服务器,也很锐意进取。这个服务器在后期的版本中,他们的控制台又原来的cs模式升级到bs模式,可以说这么进化却是革命性的。bs模式的控制台,在升级和优化都比cs模式要方便。他们可以用bs作为控制台,但是又没有部署相关的应用服务器,就是说他们有嵌入式java服务器。

    在此文中的应用式java服务器的主要是通过java main方法来启动一个嵌入应用服务器平台,监听某个端口,来持续对本机用户或者和本机相关局域网用户提供应用服务,什么服务就视乎你部署了什么样的应用。、

    嵌入式服务器的核心类

   

  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. }  

 

  调用类

  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应用程序;

方法initJettyHttpServer是调用一个webcontent的web应用程序;

具体用法再深入研究

 

posted on 2017-10-28 21:26  Sharpest  阅读(511)  评论(0编辑  收藏  举报