在项目中,开发阶段监控程序可以使用断点完成调试,对于线上的项目,要监控时候:
1.日志分析,
2.JConsole工具
3.Btrace跟踪和监控线程
安装Btrace:在windows环境下,可以直接利用jdk自带的jvisualvm工具来 安装Btrace插件,具体操作如下:
1.进入jdk安装目录,进入bin目录,找到jvisualvm.exe ,打开之后,在工具栏——>工具——>插件 ,搜索插件BTrace Workbench,然后安装
2.启动你程序所在的Tomcat,在jvisualvm.exe 窗口你会发现有个Tomcat的菜单图标,右键——>选择Trace application
3.如果想监控程序执行时间,复制如下代码到Btrace窗体内
package my.app.btrace;
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.*;
@BTrace
public class TraceProductManageImpl {
@OnMethod(clazz = "/com.odianyun.+business.+/", method = "/.+/", location = @Location(Kind.RETURN))
public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,@Duration long time){
long durationTime = time/1000000;
if(durationTime > 0){
String output = strcat(name,".");
output = strcat(output,method);
output = strcat(output,"#");
output = strcat(output,str(probeLine()));
output = strcat(output,">>");
output = strcat(output,str(durationTime));
output = strcat(output,">>ThreadId:");
output = strcat(output,str(threadId(currentThread())));
println(output);
}
}
}
如果想监控程序的参数,运行call tree,换成以下代码即可:
ackage my.app.btrace;
import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.size;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import java.util.List;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import com.sun.btrace.annotations.Return;
import com.sun.btrace.annotations.Self;
@BTrace
/**
* 获取程序参数
* @author Michael
*/
public class TraceProductManageParam {
@OnMethod(clazz = "com.odianyun.frontier.guide.business.read.manage.impl.PromotionManageImpl", method = "getMerchantProductCurrentPromotionPriceBatch", location = @Location(Kind.RETURN))
public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,@Self Object self, List
String output = strcat(name,".");
output = strcat(output,method);
output = strcat(output,">inputItems:");
output = strcat(output, str(mpIds));
output = strcat(output,">outputSize:");
output = strcat(output, str(size(ret)));
println(output);
}
}