嵌入式tomcat服务代码实现
2018-03-29 14:48
103 查看
package com.variflight.data.analysis.entity.web.server; import java.io.File; import javax.servlet.ServletException; import org.apache.catalina.LifecycleException; import org.apache.catalina.connector.Connector; import org.apache.catalina.startup.Tomcat; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; /** * @author sunli */ public class TomcatHttpServer { private static Tomcat tomcat; public static class Builder { private String context = ""; private String webapp = ""; private String baseDir = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); private int port = 8080; private String protocol = "org.apache.coyote.http11.Http11NioProtocol"; private int maxThreads = 100; private int maxConnections = 100; private int connectionTimeout = 60000; private String uriEncoding = "UTF-8"; private int maxKeepAliveRequests = -1; public String getBaseDir() { return baseDir; } public Builder setBaseDir(String baseDir) { this.baseDir = baseDir; return this; } public int getPort() { return port; } public Builder setPort(int port) { this.port = port; return this; } public String getProtocol() { return protocol; } public Builder setProtocol(String protocol) { this.protocol = protocol; return this; } public int getMaxThreads() { return maxThreads; } public Builder setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; return this; } public int getMaxConnections() { return maxConnections; } public Builder setMaxConnections(int maxConnections) { this.maxConnections = maxConnections; return this; } public int getConnectionTimeout() { return connectionTimeout; } public Builder setConnectionTimeout(int connectionTimeout) { this.connectionTimeout = connectionTimeout; return this; } public String getUriEncoding() { return uriEncoding; } public Builder setUriEncoding(String uriEncoding) { this.uriEncoding = uriEncoding; return this; } public int getMaxKeepAliveRequests() { return maxKeepAliveRequests; } public Builder setMaxKeepAliveRequests(int maxKeepAliveRequests) { this.maxKeepAliveRequests = maxKeepAliveRequests; return this; } public String getContext() { return context; } public Builder setContext(String context) { this.context = context; return this; } public String getWebapp() { return webapp; } public Builder setWebapp(String webapp) { this.webapp = webapp; return this; } public TomcatHttpServer build() { if (StringUtils.isBlank(context)) { throw new IllegalArgumentException("context not set!"); } if (StringUtils.isBlank(webapp)) { throw new IllegalArgumentException("webapp directory not set!"); } File web = new File(webapp); if (!web.isDirectory()) { throw new IllegalArgumentException("webapp is not directory!"); } if (!web.exists()) { throw new IllegalArgumentException("webapp directory not exists!"); } return new TomcatHttpServer(this); } } public TomcatHttpServer(Builder builder) { tomcat = new Tomcat(); // tomcat.getHost().setDeployOnStartup(true); // 设置appbase则会将该目录下的所有web应用进行管理,并且通过设置自动发布从而会实现自动解压war // tomcat.getHost().setAppBase(WORKSPACE); // tomat work目录 可设置成tmp目录 tomcat.setBaseDir(FilenameUtils.concat(builder.getBaseDir(), String.valueOf(builder.getPort()))); // tomcat.setHostname(ip); // tomcat.enableNaming(); Connector connector = new Connector(builder.getProtocol()); connector.setPort(builder.getPort()); connector.setProperty("maxThreads", String.valueOf(builder.getMaxThreads())); connector.setProperty("maxConnections", String.valueOf(builder.getMaxConnections())); connector.setProperty("connectionTimeout", String.valueOf(builder.getConnectionTimeout())); connector.setURIEncoding(builder.getUriEncoding())
connector.setProperty("maxKeepAliveRequests", String.valueOf(builder.getMaxKeepAliveRequests())); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); try { tomcat.addWebapp(builder.getContext(), builder.getWebapp()); } catch (ServletException e) { throw new IllegalStateException("init server", e); } } public void startup() { try { tomcat.start(); } catch (LifecycleException e) { throw new IllegalStateException("Failed to start tomcat server", e); } } }调用:
package com.variflight.data.analysis.entity.web.server; import java.io.File; import java.io.IOException; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.core.io.support.PropertiesLoaderUtils; /** * EntityWebServer * @author sunli */ public class EntityWebServer { private static Log logger = LogFactory.getLog(EntityWebServer.class); public static void main(String[] args) throws InterruptedException, IOException { startTomcat(); System.out.println("EntityWebServer is running!"); logger.info("EntityWebServer is running!"); synchronized (EntityWebServer.class) { try { EntityWebServer.class.wait(); } catch (Throwable e) { System.exit(1); } } } private static void startTomcat() throws IOException { Properties config = PropertiesLoaderUtils.loadAllProperties("config.properties"); int port = Integer.parseInt(getProperty(config, "ENTITY.WEB.PORT")); String context = getProperty(config, "ENTITY.WEB.CONTEXT"); if (!context.startsWith("/")) { context = "/" + context; } String webapp = new File(getProperty(config, "ENTITY.WEB.APP")).getAbsolutePath(); new TomcatHttpServer.Builder().setPort(port).setContext(context).setWebapp(webapp).build().startup(); } private static String getProperty(Properties config, String propertyName) { String propertyValue = config.getProperty(propertyName); if (StringUtils.isBlank(propertyValue)) { throw new IllegalArgumentException("property: " + propertyName + " not set!"); } return propertyValue; } }
相关文章推荐
- 嵌入式Tomcat的使用(即使用java代码控制tomcat服务)
- 将Tomcat加入服务,实现开机自动运行
- WMI 创建系统服务的实现代码(Automatic)
- 嵌入式 linux下cp带进度条的实现示例代码
- 【How Tomcat Works】第一章——一个简易的java web服务实现(上)
- SpringMVC整合Quartz实现定时任务以及Tomcat服务执行初始化定时任务
- 分布式系统中利用zookeeper集群实现微服务主备切换代码片段(C语言版)
- nginx tomcat集群配置实现无痛重启服务教程python语言版本
- windows下的网络编程——Select模型实例,一款ECHO服务的实现代码
- tomcat服务实现多个应用的配置
- linux环境下编写shell脚本实现启动停止tomcat服务的方法
- 实现TOMCAT服务下一个ip绑定多域名绑定的方法
- Apache + Tomcat + mod_jk实现集群服务及session共享
- tomcat脚本启动正常,服务无法启动,特定错误代码1
- apache+tomcat+jk+ssl实现tomcat集群https服务(windows)
- 高性能Web服务之Httpd+Tomcat实现负债均衡反向代理实现 推荐
- Spring+JMS+ActiveMQ+Tomcat实现消息服务
- 实现tomcat启动时自动运行代码
- JAVA与.NET的相互调用——通过Web服务实现相互调用(附原代码)
- mysql服务的注册,启动、停止、注销。 [delphi代码实现]