无为,猎豹wifi,online-网络性价比漫谈,让人人都能消费得起

频道:小编推荐 日期: 浏览:355

布景描绘

javaagent是在JDK5之后供给的新特性,也能够叫java署理。开发者经过这种机制(Instrumentation)能够在加载class文件之前修正办法的字节码(此刻字节码没有参加JVM),动态更改类办法完结AOP,供给监控服务如;办法调用时长、可用率、内存等。

开发简述

经过完结ClassFileTransformer接口办法,动态更改办法的字节码。在办法前后加上时刻戳,最终履行完结输出履行时长。

环境预备

1、IntelliJ IDEA Community Edition 2018.3.1 x64

2、jdk1.8 64位

装备信息(途径相关修正为自己的)

1、java调试时装备

2.1、装备方位:Run/Debu晏斯泰g Configurations ->VM options

2.2、装备内容(编译后的jar放到根目录下):-javaagent:E:\itstack-demo-javaagent-1.0-SNAPSHOT.jar=agentargs

代码示例

pom.xml

 
防爆墙做法图集
org.javassist
javassist
3.22.0-GA


MyAgent.java

MyTransformer.java

public class MyTransformer implements ClassFileTransformer {
private final static String prefix = "\nlo陈伟霆为什么叫陈令郎ng startTime = S快瞄ystem.currentTimeMillis();\n";
private final static String postfix = "\nlong endTime = System.currentTimeMillis();\n";
// 被处理的方无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起法列表
private final static Map> methodMap = new HashMap>();
public MyTransformer() {
//对指定办法监控
add("org.itstack.demo.test.AgentTest.queryUserAge");
add("org.itstack.demo.test.AgentTest.queryUserName");
}
private v无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起oid add(String methodString) {
String className = methodString.substring(0, methodString.lastIndexOf("."));
String methodName = methodSt石川纱彩ring.substring(methodString.lastI于筱诺ndexOf(".") + 1);
List list = methodMap.get(className);
if (list == null) {
list = new ArrayList(月饼歌);
methodMap.put(className, list);
}
list.add(methodName);
}
@Override
public byte[] transform(C雪域金翅lassLoader loader, String className, Class
ProtectionDomain protectionDomain, byte[] cl无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起assfileBuffer) throws IllegalClassFormatException {
className = className.repl无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起ace于宏勤("/", ".");
// 判别加载的class的包途径是不何新批温是需求监控的类
if (methodMap.containsKey(className)) {
CtClass ctclass = null;
try {
ctclass = ClassPool.getDefault().get(cl吴浈保护伞assName);// 运用全称,用于获得字节码类<运用javassist>
for (String methodName : methodMap.get(className)) {
String outputStr = "\nSystem.out.println(\"监控信息(履行耗时):" + className + "." + methodName + " => \" +(endTime - startTime) +\"毫秒\");";
CtMethod ct申雨颖method = ctclass.getDeclaredMethod(methodName);/乔诗晗/ 得到这办法实例
String newMethodName = methodName + "$new";// 新界说一个办法叫做比方queryUserAge$new
ctmethod.setName(newMethodName);// 将本来的办法姓名修正
// 创立新的办法,仿制本来的办法,姓名为本来的姓名
CtMethod newMethod = CtNewMethod.copy(ctmethod, methodName, ctclass, null);
// 构建新的办法体
StringBuilder methodBodyStr = new StringBuilder();
methodBodyStr.append("{");
methodBodyStr.append(prefix);
methodBodyStr.append(newMethodName + "($$);\n");// 调用原有代码,类似于method();($$)表明一切的参数
methodBodyStr.append(postfix);
methodBodyStr.append(outputStr);
methodBodyStr.append("}");
newMethod.setBody(methodBodyStr.toString());// 替换新办法
ctclass.addMethod(newMethod); // 添加新办法
}
return ctclass.toBytecode();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}

MANIFEST.MF

Manifest-Version: 1.0
Premain-Class: org.itstack.demo.agent.MyAgent
Can-Redefine-Classes: true

AgentTest.java

package org.itstack.demo.test;
import java.util.logging.Logger;
/**
* vm options = -javaagent:E:\itstack-demo-javaagent-1.0-SNAPSHOT.jar=agentargs
*/
public class AgentTest {
private static Logger logger = Logger.getLogger("AgentTest");
public static void ma无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起in(String[] args) {
String userId = "100001";
queryUserAge(userId);
queryUserName(userId);
}
private static void queryUserAge(String userId) 哆拾惠{
try {
Thread.sleep(300);
logger.info("hello userId:" + userId +" age 18");
} catch (InterruptedE为卿狂xception e) {
e.printStackTrace();
}
}
priv姜宏波老公ate static 无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起void queryUserName无为,猎豹wifi,online-网络性价比闲谈,让人人都能消费得起(String userId) {
try {
Thread.sleep(100);
logger.info("hello userId:" + userId +" name agent");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

测验成果

2019-4-1履冰险7 11:34:33 org.itstack.demo.test.AgentTest queryUserAge$new
信息: hello userId:100001西汇农商 age 18
监控信息(履行耗慕晚瑜时):org.itstack.demo.test.AgentTest.queryUserAge => 316毫秒
2019-4-17 11:34:33 org.itstack.demo.test.AgentTest queryUserName$new
信息: hello userId:100001 name agent
监控信息(履行耗时):org.itstack.demo.test.AgentTest.queryUserName => 100毫秒