All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.csp.ahas.switchcenter.AhasHttpRequestHandler Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package com.alibaba.csp.ahas.switchcenter;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
import java.util.concurrent.TimeUnit;

import com.alibaba.csp.ahas.switchcenter.util.HttpRequestEvent;

import com.taobao.csp.switchcenter.command.CommandHandler;
import com.taobao.csp.switchcenter.command.handler.AddCommondHandler;
import com.taobao.csp.switchcenter.command.handler.GetCommandHandler;
import com.taobao.csp.switchcenter.command.handler.ListCommandHandler;
import com.taobao.csp.switchcenter.command.handler.MonitorCommandHandler;
import com.taobao.csp.switchcenter.command.handler.RemoveCommondHandler;
import com.taobao.csp.switchcenter.command.handler.SetCommandHandler;
import com.taobao.csp.switchcenter.command.handler.VersionCommandHandler;
import com.taobao.csp.switchcenter.command.handler.ViewCommandHandler;
import com.taobao.csp.switchcenter.log.SwitchRecordLog;
import com.taobao.csp.switchcenter.util.StringUtil;

/**
 * @author lixin.lb
 */
public class AhasHttpRequestHandler {
    private static volatile boolean httpClientInitFlag = false;
    private static Map handlerMap = new HashMap<>();
    private static ExecutorService bizExecutor;
    private static final String CSP_PORT = "csp.port";
    private static String soTimeoutStr = System.getProperty("csp.server.socketSoTimeout", "3000");
    private static volatile int port = 8719;
    private static volatile boolean useCustomerPort = false;
    private static SocketThread socketThread = new SocketThread();

    public synchronized static void init(){
        if (!httpClientInitFlag) {
            registerHandler();
            socketThread.start();
        }
        httpClientInitFlag = true;
    }

    static class SocketThread extends Thread {
        @Override
        public void run() {
            int pocessNum = Runtime.getRuntime().availableProcessors();
            bizExecutor = new ThreadPoolExecutor(pocessNum, pocessNum,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue(100), new DiscardPolicy());
            initPort();
            ServerSocket serverSocket = null;
            while(serverSocket == null) {
                try {
                    serverSocket = new ServerSocket(port);
                } catch(Exception e) {
                    SwitchRecordLog.info("holding CommandCenter failed, port " + port, e);
                    adjustPort();
                    sleepSencond(5L);
                }
            }
            SwitchRecordLog.info("[CommandCenter] begin listening at port " + serverSocket.getLocalPort());
            while (true) {
                try {
                    Socket socket = serverSocket.accept();
                    if (socket != null) {
                        socket.setSoTimeout(Integer.valueOf(soTimeoutStr));
                    }
                    HttpRequestEvent eventTask = new HttpRequestEvent(socket);
                    bizExecutor.submit(eventTask);
                } catch (Exception e) {
                    SwitchRecordLog.info("server error!", e);
                    sleepSencond(1);
                }
            }
        }
    }

    private static void initPort(){
        String specPort = System.getProperty(CSP_PORT, "8719");
        if (StringUtil.isNotBlank(specPort)) {
            try {
                port = Integer.parseInt(specPort.trim());
                useCustomerPort = true;
            } catch(Exception e) {
                SwitchRecordLog.info("[CommandCenter] port format error: " + specPort);
            }
        }
    }

    private static void adjustPort() {
        if (useCustomerPort) {
            return;
        }
        port = port + 1;
    }

    private static void sleepSencond(long second){
        try {
            TimeUnit.SECONDS.sleep(second);
        } catch (Exception e) {

        }
    }

    private synchronized static void registerHandler(){
        handlerMap.put("switch.set", new SetCommandHandler());
        handlerMap.put("switch.list", new ListCommandHandler());
        handlerMap.put("switch.get", new GetCommandHandler());
        handlerMap.put("switch.view", new ViewCommandHandler());
        handlerMap.put("switch.monitor", new MonitorCommandHandler());
        handlerMap.put("switch.version", new VersionCommandHandler());
        handlerMap.put("switch.add", new AddCommondHandler());
        handlerMap.put("switch.remove", new RemoveCommondHandler());

    }

    public static CommandHandler getCommand(String target) {
        return handlerMap.get(target);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy