不少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
嵌入式服務器的核心類服務器
- package com.shine.framework.HttpServer;
-
- import org.mortbay.jetty.Connector;
- import org.mortbay.jetty.Server;
- import org.mortbay.jetty.nio.SelectChannelConnector;
- import org.mortbay.jetty.webapp.WebAppContext;
- import org.mortbay.thread.BoundedThreadPool;
-
- public class HttpServerManager {
- private Server server;
-
-
- public void initJettyHttpServerByWar(String contextName, String warPath,
- String port) {
- initJettyHttpServerByWar(contextName, warPath, port, 100);
- }
-
-
- @SuppressWarnings("deprecation")
- public void initJettyHttpServerByWar(String contextName, String warPath,
- String port, int threadPoolNum) {
- if (server != null && server.isRunning()) {
- System.err.println("請關閉http服務器再重啓");
- return;
- }
-
- try {
- server = new Server();
- BoundedThreadPool threadPool = new BoundedThreadPool();
-
- threadPool.setMaxThreads(threadPoolNum);
- server.setThreadPool(threadPool);
-
- Connector connector = new SelectChannelConnector();
-
- connector.setPort(Integer.parseInt(port));
- server.setConnectors(new Connector[] { connector });
- WebAppContext context = new WebAppContext();
-
- context.setContextPath(contextName);
-
- context.setWar(warPath);
- server.addHandler(context);
- server.setStopAtShutdown(true);
- server.setSendServerVersion(true);
-
- server.start();
- server.join();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- @SuppressWarnings("deprecation")
- public void initJettyHttpServer(String contextName, String webPath,
- String port) {
- initJettyHttpServer(contextName, webPath, port, 100);
- }
-
-
- @SuppressWarnings("deprecation")
- public void initJettyHttpServer(String contextName, String webPath,
- String port, int threadPoolNum) {
- if (server != null && server.isRunning()) {
- System.err.println("請關閉http服務器再重啓");
- return;
- }
-
- try {
- server = new Server();
- BoundedThreadPool threadPool = new BoundedThreadPool();
-
- threadPool.setMaxThreads(threadPoolNum);
- server.setThreadPool(threadPool);
-
- Connector connector = new SelectChannelConnector();
-
- connector.setPort(Integer.parseInt(port));
- server.setConnectors(new Connector[] { connector });
- WebAppContext context = new WebAppContext(webPath, contextName);
- server.addHandler(context);
- server.setStopAtShutdown(true);
- server.setSendServerVersion(true);
-
- server.start();
- server.join();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void shutdownJettyHttpServer() {
- if (server == null) {
- System.err.println("http沒有初始化再重啓");
- return;
- }
-
- try {
- if (server.isRunning())
- server.stop();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void restartJettyHttpServer() {
- if (server == null) {
- System.err.println("http沒有初始化再重啓");
- return;
- }
-
- try {
- if (!server.isRunning())
- server.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
調用類app
- package com.shine.framework.HttpServer;
-
- public class Example {
- public static void main(String args[]) {
- HttpServerManager manager = new HttpServerManager();
-
-
-
- manager.initJettyHttpServer("/ManageSystemFlex",
- "E://workspace//ManageSystemFlex//WebContent", "8080");
- }
- }
方法initJettyHttpServerByWar是調用一個war包的web應用程序;webapp
方法initJettyHttpServer是調用一個webcontent的web應用程序;優化
具體用法再深刻研究spa