forked from NotBadPad/easy-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEHServer.java
More file actions
60 lines (50 loc) · 1.66 KB
/
Copy pathEHServer.java
File metadata and controls
60 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.eh.core.http;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.Timer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eh.core.common.Constants;
import org.eh.core.task.SessionCleanTask;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;
/**
* 主服务类
* @author guojing
* @date 2014-3-3
*/
public class EHServer {
private final Log log = LogFactory.getLog(EHServer.class);
/**
* 初始化信息,并启动server
*/
public void startServer() throws IOException {
log.info("Starting EHServer......");
//设置路径
Constants.CLASS_PATH = this.getClass().getResource("/").getPath();
Constants.VIEW_BASE_PATH = "page";
Constants.STATIC_RESOURCE_PATH = "static";
//启动session过期清理定时器
Timer timer = new Timer();
SessionCleanTask sessionCleanTask = new SessionCleanTask();
log.info("Initializing SessionCleanTask,the session_out_time is " + Constants.SESSION_TIMEOUT * 2
+ " minute.");
timer.schedule(sessionCleanTask, new Date(), Constants.SESSION_TIMEOUT * 60 * 2 * 1000);
//设置端口号
int port = 8899;
// 启动服务器
HttpServerProvider provider = HttpServerProvider.provider();
HttpServer httpserver = provider.createHttpServer(new InetSocketAddress(port), 100);
httpserver.createContext("/", new EHHttpHandler());
httpserver.setExecutor(null);
httpserver.start();
log.info("EHServer is started, listening at 8899.");
}
/**
* 项目main
*/
public static void main(String[] args) throws IOException {
new EHServer().startServer();
}
}