AmoebaProxyServer作为Amoeba的入口类存在于amoeba子工程的com.meidusa.amoeba.server包下。AmoebaProxyServer的main方法提供了完整的启动流程和方法。
AmoebaProxyServer提供了两个日志记录对象,一个用于基本的日志记录,一个用于对报告进行日志记录。
1 首先来看main方法的第一段代码(用于判断并处理启动或停止命令)。代码中加入少量注释,便于理解:
[java]
String level = System.getProperty("benchmark.level", "warn");
System.setProperty("benchmark.level", level);
if(args.length>=1){
ShutdownClient client = new ShutdownClient(MonitorConstant.APPLICATION_NAME);
MonitorCommandPacket packet = new MonitorCommandPacket(); //监控命令报文对象
//通过第一个命令行参数进行处理判断
if("start".equalsIgnoreCase(args[0])){
//处理启动amoeba命令
packet.funType = MonitorCommandPacket.FUN_TYPE_PING;
if(client.run(packet)){
//如果已经启动,则提示amoeba server启动信息,并异常退出
System.out.println("amoeba server is running with port="+client.getPort());
System.exit(-1);
}
}else{
//处理停止amoeba命令
packet.funType = MonitorCommandPacket.FUN_TYPE_AMOEBA_SHUTDOWN;
if(client.run(packet)){
//停止成功提示
System.out.println("amoeba server shutting down with port="+client.getPort());
}else{
//停止失败提示(原因为amoeba server并未启动)
System.out.println("amoeba server not running with port="+client.getPort());
}
System.exit(0);
}
}else{
//没有传入参数,则提示amoeba命令行用法
System.out.println("amoeba start|stop");
System.exit(0);
}
String level = System.getProperty("benchmark.level", "warn");
System.setProperty("benchmark.level", level);
if(args.length>=1){
ShutdownClient client = new ShutdownClient(MonitorConstant.APPLICATION_NAME);
MonitorCommandPacket packet = new MonitorCommandPacket(); //监控命令报文对象
//通过第一个命令行参数进行处理判断
if("start".equalsIgnoreCase(args[0])){
//处理启动amoeba命令
packet.funType = MonitorCommandPacket.FUN_TYPE_PING;
if(client.run(packet)){
//如果已经启动,则提示amoeba server启动信息,并异常退出
System.out.println("amoeba server is running with port="+client.getPort());
System.exit(-1);
}
}else{
//处理停止amoeba命令
packet.funType = MonitorCommandPacket.FUN_TYPE_AMOEBA_SHUTDOWN;
if(client.run(packet)){
//停止成功提示
System.out.println("amoeba server shutting down with port="+client.getPort());
}else{
//停止失败提示(原因为amoeba server并未启动)
System.out.println("amoeba server not running with port="+client.getPort());
}
System.exit(0);
}
}else{
//没有传入参数,则提示amoeba命令行用法
System.out.println("amoeba start|stop");
System.exit(0);
}以上代码中处理命令的功能主要通过ShutdownClient.run()方法来执行处理。作者在方法注释里提示“return false if server not running”,不管命令是start或者stop均可利用该含义返回值进行统一处理。如果为start命令,返回false表示之前未启动,返回true表示amoeba已经启动过;如果为stop命令,返回false表示amoeba并未启动,无法停止,返回true则表示停止成功。ShutsownClient.run()方法完整代码(加注释)如下所示:
[java]
/**
* 第一次启动时,由于监控服务并未启动,程序将在socket建立时抛出异常返回false
* @param command
* @return false if server not running
*/
public boolean run(MonitorCommandPacket command) {
if(port <=0){
//加载.Amoeba.shutdown.port(文件内容形如127.0.0.1:22334),从文件中获取监控IP和端口
socketInfoFile = new File(ConfigUtil.filter("${amoeba.home}"),appplicationName+".shutdown.port");
if(!socketInfoFile.exists()){