手动创建Http服务器(集群Http服务器)
来源:互联网 发布:v版4s支持什么网络 编辑:程序博客网 时间:2024/06/05 02:35
clusterServer = HttpServer.create(new InetSocketAddress(hostname, port), 1);
} catch (IOException e) {
logger.error("Create cluster http server failed:");
logger.error(e);
for (StackTraceElement ste : e.getStackTrace()) {
logger.error(ste.toString());
}
System.exit(0);
}
clusterServer.createContext("/", new ClusterHttpHandler());
clusterServer.start();
package server;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
import com.cmreadcommon.util.RedisConnectionPool;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import common.HttpConstAndEnum;
public class ClusterHttpHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
String response = null; // 响应
/*
* 解析请求并处理
*/
String parameters = httpExchange.getRequestURI().getQuery();
// 解析出参数列表
String[] split = parameters.split("\\&");
/*
* 解析出首个参数的键值,其中该键固定为Operation,即该请求的操作
* 根据操作行为决定后续参数的解析
*/
String operation = split[0].split("=")[1];
switch (operation) {
case HttpConstAndEnum.CLUSTER_COMMAND_BECOME_MASTER: // 尝试成为Master
switch (StatisticManagementServer.serverIdentity) {
case StatisticManagementServer.IDENTITY_NONE:
response = HttpConstAndEnum.RESULT_YES;
StatisticManagementServer.serverIdentity = StatisticManagementServer.IDENTITY_SLAVE;
break;
case StatisticManagementServer.IDENTITY_MASTER:
response = HttpConstAndEnum.RESULT_NO;
break;
case StatisticManagementServer.IDENTITY_SLAVE:
response = HttpConstAndEnum.RESULT_YES;
break;
}
break;
case HttpConstAndEnum.CLUSTER_COMMAND_CHECK_REACHABLE: // 检查可达性
response = HttpConstAndEnum.RESULT_YES;
break;
case HttpConstAndEnum.SERVER_COMMAND_SHUTDOWN: // 关机
StatisticManagementServer.serverState = StatisticManagementServer.SERVERSTATE_CLOSE;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/*
* 打断所有线程
*/
StatisticManagementServer.timer.interrupt();
StatisticManagementServer.identityManager.interrupt();
StatisticManagementServer.versionListUpdate.interrupt();
/*
* 关闭Redis连接池
*/
Iterator<Entry<String, RedisConnectionPool>> iterator = StatisticManagementServer.connections.entrySet().iterator();
while (iterator.hasNext()) {
iterator.next().getValue().shutdown();
}
iterator = StatisticManagementServer.kernelConnections.entrySet().iterator();
while (iterator.hasNext()) {
iterator.next().getValue().shutdown();
}
response = "Server Stoped!";
break;
}
/*
* 写回响应并断开连接
*/
// 设置200协议头
httpExchange.sendResponseHeaders(200, response.getBytes().length);
// 写入数据
httpExchange.getResponseBody().write(response.getBytes());
// 关闭输入输出流
httpExchange.getRequestBody().close();
httpExchange.getResponseBody().close();
httpExchange.close();
/*
* 如果是关闭服务器命令,则退出程序(HttpServer.stop方法似乎仍有遗留未关闭线程)
*/
if (operation.equals(HttpConstAndEnum.SERVER_COMMAND_SHUTDOWN)) {
System.exit(0);
}
}
}
- 手动创建Http服务器(集群Http服务器)
- 创建 nodejs HTTP服务器
- pomelo服务器集群中集成http服务器
- java 手动写http静态网页服务器
- java 手动写http静态网页服务器
- Node.js 创建HTTP服务器
- Node.js 创建HTTP服务器
- Node.js创建HTTP服务器
- 使用nodejs创建http服务器
- JBOSS4.0.2 HTTP集群配置:服务器配置
- http服务器(nodejs)
- (2)http服务器
- http服务器
- HTTP 服务器:
- http服务器
- http服务器
- http服务器
- http服务器
- nginx+tomcat简单负载均衡配置
- 如何使用php判断服务器是否是HTTPS连接
- bootstrap-警告框/进度条/列表组/面板
- SaltStack实战之SaltStack快速入门
- Spark性能调优(六)
- 手动创建Http服务器(集群Http服务器)
- Kotlin之类和对象
- 网易统计回文
- 【原创】将数据生成json文件下载以及json中文乱码和优化json文件格式的方法
- MD5+salt加密
- mysql EXISTS和NOT EXISTS
- [Android]DDMS查看app保存的Sqlite数据库db文件和db升级
- Python之import方法引入模块详解
- ffmpeg视频学习网站