• jetty 通过配置文件嵌入式启动web服务


    定义 jetty.xml 启动文件 

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

    <Configure id="Server" class="org.eclipse.jetty.server.Server">
    <Get name="ThreadPool">
    <Set name="minThreads" type="int">10</Set>
    <Set name="maxThreads" type="int">100</Set>
    <Set name="idleTimeout" type="int">60000</Set>
    <Set name="detailedDump">false</Set>
    </Get>

    <Call name="addBean">
    <Arg>
    <New class="org.eclipse.jetty.util.thread.ScheduledExecutorScheduler" />
    </Arg>
    </Call>

    <Call name="addConnector">
    <Arg>
    <New class="org.eclipse.jetty.server.ServerConnector">
    <Arg>
    <Ref refid="Server"></Ref>
    </Arg>
    <Set name="port">8899</Set>
    <Set name="connectionFactories">
    <Array type="org.eclipse.jetty.server.ConnectionFactory">
    <Item>
    <New class="org.eclipse.jetty.server.HttpConnectionFactory"></New>
    </Item>
    </Array>
    </Set>
    <Set name="idleTimeout">30000</Set>
    <!-- <Set name="lowResourceMaxIdleTime">3000</Set> <set name="lowResourcesConnections">90</set>
    <Set name="acceptors">1</Set> -->
    </New>
    </Arg>
    </Call>

    <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.StatisticsHandler">
    <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.HandlerCollection">
    <Set name="handlers">
    <Array type="org.eclipse.jetty.server.Handler">
    <Item>
    <New id="Contexts"
    class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
    <Set name="handlers">
    <Array type="org.eclipse.jetty.server.Handler">
    <Item>
    <New class="org.eclipse.jetty.servlets.gzip.GzipHandler">
    <Set name="mimeTypes">text/html,text/xml,application/json,text/plain,application/xhtml+xml,text/css,application/javascript,image/svg+xml
    </Set>
    <Set name="minGzipSize">0</Set>
    <Set name="handler">
    <New class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/</Set>
    <Set name="war">./web</Set>
    <Call name="setInitParameter">
    <Arg>org.eclipse.jetty.servlet.Default.dirAllowed
    </Arg>
    <Arg>false</Arg>
    </Call>
    </New>
    </Set>
    </New>
    </Item>
    </Array>
    </Set>
    </New>
    </Item>

    <Item>
    <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler" />
    </Item>

    <Item>
    <New class="org.eclipse.jetty.server.handler.RequestLogHandler">
    <Set name="requestLog">
    <New class="org.eclipse.jetty.server.NCSARequestLog">
    <Set name="filename">
    logs/yyyy_mm_dd.jetty_request.log
    </Set>
    <Set name="filenameDateFormat">yyyy_MM_dd</Set>
    <Set name="LogTimeZone">GMT</Set>
    <Set name="retainDays">90</Set>
    <Set name="append">true</Set>
    <Set name="logLatency">true</Set>
    </New>
    </Set> 
    </New>
    </Item>
    </Array>
    </Set>
    </New>
    </Set>
    </New>
    </Set>
    </Configure>

      

    定义jetty启动类

    Jetty.java

       

    public class Jetty {
    private Server server;

    public Jetty() throws Exception {
    Resource fileserver_xml = Resource.newClassPathResource("jetty.xml");
    XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
    server = (Server) configuration.configure();
    }

    public Jetty(String config) throws Exception {
    Resource fileserver_xml = Resource.newClassPathResource(config);
    XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
    server = (Server) configuration.configure();
    }

    public void startup() throws Exception {
    try {
    server.start();
    } catch (Exception e) {
    throw e;
    }
    try {
    server.join();
    } catch (InterruptedException e) {
    throw e;
    }
    }

    public void shutdown() throws Exception {
    server.stop();
    }

    public static void main(String[] args) throws Exception {
    Jetty jetty = new Jetty();
    jetty.startup();
    }

    }

     

    WEB 结构

    bin  etc  lib  logs  web

    web中文件内容同tomcat 类似

  • 相关阅读:
    spring data jpa删除的使用方式
    javax.persistence.TransactionRequiredException: No transactional EntityManager available
    java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法
    JS比较两个数组是否相等 是否拥有相同元素
    HttpSession的线程安全问题及注意事项
    easyui datagrid editor onBeforeEdit事件下使用getEditor和getEditors失效
    JQuery easyUi datagrid 中 editor 动态设置最大值最小值
    kaptcha验证码组件使用简介
    ohasd failed to start: Inappropriate ioctl for device
    web报表工具FineReport常见的数据集报错错误代码和解释
  • 原文地址:https://www.cnblogs.com/yidaxia/p/5345302.html
Copyright © 2020-2023  润新知