手动创建Http服务器(集群Http服务器)

来源:互联网 发布:v版4s支持什么网络 编辑:程序博客网 时间:2024/06/05 02:35
try {
            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);
        }
    }

}


原创粉丝点击