一、org.eclipse.jetty插件启动
1.maven依赖
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.4.5.v20170502</version> <scope>provided</scope> </dependency>
2.plugin
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.5.v20170502</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> <stopKey>shutdown</stopKey> <stopPort>9966</stopPort> <scanIntervalSeconds>3</scanIntervalSeconds> <webApp> <contextPath>/ciyo</contextPath> <descriptor>src/main/webapp/WEB-INF/web.xml</descriptor> <resourceBases> <resourceBase>${project.basedir}/src/main/webapp</resourceBase> </resourceBases> </webApp> <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog"> <filename>target/access-yyyy_mm_dd.log</filename> <filenameDateFormat>yyyy_MM_dd</filenameDateFormat> <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat> <logTimeZone>GMT+8:00</logTimeZone> <append>true</append> <logServer>true</logServer> <retainDays>120</retainDays> <logCookies>true</logCookies> </requestLog> </configuration> </plugin>
二、Java Main 函数启动
1.maven依赖
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.5.v20170502</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.22.v20170606</version>
<scope>provided</scope>
</dependency>
2.server_config.properties
server.host=0.0.0.0
server.web.port=8080
server.web.warFile=src/main/webapp
server.web.contextPath=/ciyo
3.JettyMainServer
/**
* Copyright 2017 公司名. All rights reserved.
*
* @Description: TODO
* @author: asus
* @date: 2017年6月13日 下午7:52:29
* @version: V1.0
*/
package ciyo.peak.web.notify.receive.controller;
import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ciyo.peak.common.utils.PropertiesUtil;
public class JettyMainServer {
private static Logger logger = LoggerFactory.getLogger(JettyMainServer.class);
public static void main(String[] args) {
try {
logger.info("start executor server");
PropertiesUtil properties = PropertiesUtil.getInstance("server_config");
Validate.notNull(properties, "properties not load[file=server_config.properties]");
// 启动http服务
String contextPath = properties.getPropertyAsString("server.web.contextPath");
String hostName = StringUtils.defaultString(properties.getPropertyAsString("server.host"), "0.0.0.0");
int port = properties.getPropertyAsInt("server.web.port");
String warFile = properties.getPropertyAsString("server.web.warFile");
Validate.isTrue(port > 0, "port < 0");
Validate.notBlank(warFile);
Server server = JettyMainServer.createServer(contextPath, hostName, port, warFile);
server.start();
logger.info("start executor server succ");
} catch (Exception e) {
logger.error("start executor server error", e);
System.exit(1);
}
}
/**
* create server
*
* @param contextPath
* context path
* @param hostName
* host name
* @param port
* port
* @return
*/
public static Server createServer(String contextPath, String hostName, int port, String warFile) {
logger.info("createServer params[contextPath={},hostName={},port={},warFile={}]", contextPath, hostName, port,
warFile);
// use Eclipse JDT compiler
System.setProperty("org.apache.jasper.compiler.disablejsr199", "true");
// threadPool
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMinThreads(10);// default 8
threadPool.setMaxThreads(200);// default 200
threadPool.setDetailedDump(false);// default false
Server server = new Server(threadPool);
server.setStopAtShutdown(true);
ProtectionDomain protectionDomain = JettyMainServer.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
WebAppContext context = new WebAppContext(warFile, contextPath);
context.setClassLoader(Thread.currentThread().getContextClassLoader());
context.setConfigurationDiscovered(true);
context.setParentLoaderPriority(true);
context.setServer(server);
context.setDescriptor(warFile + "/WEB-INF/web.xml");
// 设置work dir,war包将解压到该目录,jsp编译后的文件也将放入其中。
String currentDir = new File(location.getPath()).getParent();
File workDir = new File(currentDir, "work");
context.setTempDirectory(workDir);
server.setHandler(context);
// HttpConfiguration
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(port);
http_config.setOutputBufferSize(32768);
// no ssl
ServerConnector selectChannelConnector = new ServerConnector(server);
selectChannelConnector.setPort(port);
if (StringUtils.isNotEmpty(hostName)) {
selectChannelConnector.setHost(hostName);
}
server.setConnectors(new Connector[] { selectChannelConnector });
return server;
}
}
3.run启动
转载于:https://blog.51cto.com/ciyorecord/1935030