Nano Framework之增加Jetty内嵌支持

本文基于前一篇文章为基础进行改进,需要了解前篇内容的请访问 这里

1、pom.xml中添加nano-server依赖
1.1、依赖
<dependency>
    <groupId>org.nanoframework</groupId>
    <artifactId>nano-server</artifactId>
    <version>1.2.0-RC1</version>
    <exclusions>
        <exclusion>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.eclipse.jetty.orbit</groupId>
            <artifactId>javax.servlet</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.el</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <scope>provided</scope>
</dependency>


1.2、resources
<build>
...
<resources>
...
    <resource>
        <directory>src/main/webapp</directory>
        <targetPath>${project.basedir}/webRoot/</targetPath>
    </resource>
...
</resources>
...
</build>


2、在WEB-INF下添加jetty.xml和webdefault.xml
2.1、jetty.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
    <Set name="ThreadPool">
        <!-- Default queued blocking threadpool -->
        <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
            <Set name="minThreads">5</Set>
            <Set name="maxThreads">200</Set>
            <Set name="detailedDump">false</Set>
        </New>
    </Set>

    <Call name="addConnector">
        <Arg>
            <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <Set name="host">
                    <Property name="jetty.host" />
                </Set>
                <Set name="port">
                    <Property name="jetty.port" default="8081" />
                </Set>
                <Set name="maxIdleTime">300000</Set>
                <Set name="Acceptors">2</Set>
                <Set name="statsOn">false</Set>
                <Set name="confidentialPort">8443</Set>
                <Set name="lowResourcesConnections">20000</Set>
                <Set name="lowResourcesMaxIdleTime">5000</Set>
            </New>
        </Arg>
    </Call>
</Configure>


2.2、webdefault.xml
因篇幅过长不在这里列出,请访问 原文中的2.2配置

3、在org.nanoframework.examples.first.webapp下添加启动器
import org.nanoframework.server.JettyCustomServer;

public class Startup {
    public static void main(String[] args) {
        JettyCustomServer server = JettyCustomServer.DEFAULT;
        new Thread(() -> { server.startServer(); }).start();
    }
}


4、至此集成内嵌Jetty完成,运行Startup并访问http://ip:port/first-webapp/first/hello

猜你喜欢

转载自comicme-yanghe.iteye.com/blog/2249792