话不多说,直接上代码,相信很好懂。Gradle构建项目。
compile 'org.eclipse.jetty.aggregate:jetty-all:9.4.9.v20180320'
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.file.Path;
public class BaseServer {
protected Path contextPath;
public WebAppContext webAppContext=null;
public Server server=null;
protected int port;
protected String serverName="server";
public BaseServer(){
}
public BaseServer(int port,Path contextPath){
this.contextPath = contextPath;
this.port=port;
}
RunStatus runStatus=RunStatus.stoped;
protected Logger logger= LoggerFactory.getLogger(this.getClass());
private String throwable;
public void start() throws Exception {
start(null);
}
void callHandler(RuningStatusHandler handler, RunStatus status){
runStatus=status;
if(handler!=null){
handler.onSatusChanged(status);
}
}
public String getThrowable() {
return throwable;
}
public void setThrowable(String throwable) {
this.throwable = throwable;
}
public void start(RuningStatusHandler handler) throws Exception {
this.stop();
if(runStatus!=RunStatus.stoped){
return;
}
callHandler(handler,RunStatus.starting);
server=new Server(port);
String webContext=this.contextPath.toString();
webAppContext = new WebAppContext(webContext,"/");
webAppContext.setResourceBase(webContext);
webAppContext.setDisplayName(this.serverName);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
webAppContext.setClassLoader(classLoader);
webAppContext.setConfigurationDiscovered(true);
webAppContext.setParentLoaderPriority(true);
server.setHandler(webAppContext);
server.addLifeCycleListener(new AbstractLifeCycle.AbstractLifeCycleListener() {
@Override
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
super.lifeCycleFailure(event, cause);
setThrowable(cause.getMessage());
logger.error("服务启动失败",cause);
callHandler(handler,RunStatus.startingfail);
server=null;
webAppContext=null;
}
@Override
public void lifeCycleStarted(LifeCycle event) {
super.lifeCycleStarted(event);
logger.info("服务已启动");
callHandler(handler,RunStatus.started);
}
@Override
public void lifeCycleStarting(LifeCycle event) {
super.lifeCycleStarting(event);
logger.info("服务启动中");
callHandler(handler,RunStatus.starting);
}
@Override
public void lifeCycleStopped(LifeCycle event) {
super.lifeCycleStopped(event);
logger.info("服务已停止");
server=null;
webAppContext=null;
callHandler(handler,RunStatus.stoped);
}
@Override
public void lifeCycleStopping(LifeCycle event) {
super.lifeCycleStopping(event);
callHandler(handler,RunStatus.stoping);
logger.info("服务正在停止");
}
});
// 检测环境
server.start();
server.dumpStdErr();
server.join();
}
public void stop(){
if(runStatus!=RunStatus.started){
return;
}
if(this.server!=null){
try{
this.server.stop();
}catch (Exception ex){
logger.error("停止服务异常",ex);
}
}
}
public enum RunStatus{
starting,
startingfail,
started,
stoped,
stoping
}
public interface RuningStatusHandler{
void onSatusChanged(RunStatus status);
}
}
import com.jdsoft.studio.server.BaseServer
import java.nio.file.Path
import java.nio.file.Paths
/**
* 启动Server
*/
class AppRepoServer extends BaseServer {
static {
System.setProperty("logback.configurationFile", "logback.xml")
}
AppRepoServer(){
}
@Override
void start(BaseServer.RuningStatusHandler handler) throws Exception {
File startPath = null
try {
startPath = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath())
if(startPath.isFile()){
startPath=startPath.getParentFile()
}
logger.info("起始位置:{}",startPath)
boolean isDevEnv=false
Path classFile = Paths.get(startPath.getPath(), "com", "jdsoft", "studio", "server", "appreop", "AppRepoServer.class")
if(classFile.toFile().exists()){
//开发环境
isDevEnv=true
}
logger.info("是否是开发环境:{}",isDevEnv)
String webContext=null
String userdir = System.getProperty("user.dir")
logger.info("user.dir:{}",userdir)
if(isDevEnv){
webContext=userdir+ "/modules/appRepoServer/src/main/webapp"
}else{
// todo
// webContext=startPath+ "webapp"
webContext="webapp"
}
logger.info("webContext:{}",webContext)
this.contextPath=Paths.get(webContext)
this.serverName="AppRepoServer"
super.start(handler)
} catch (URISyntaxException e) {
logger.error("启动异常",e)
throw e
}
}
AppRepoServer(int port){
this()
this.port=port
}
}
网友评论