我有一个Thread Pool示例代码如下
public class RunThreads{
static final int MAX_TASK = 3;
public static void main(String[] args)
{
Runnable r1 = new Task("task 1");
Runnable r2 = new Task("task 2");
Runnable r3 = new Task("task 3");
Runnable r4 = new Task("task 4");
Runnable r5 = new Task("task 5");
ExecutorService pool = Executors.newFixedThreadPool(MAX_TASK);
pool.execute(r1);
pool.execute(r2);
pool.execute(r3);
pool.execute(r4);
pool.execute(r5);
pool.shutdown();
}}
和
class Task implements Runnable{
private String name;
public Task(String s)
{
name = s;
}
public void run()
{
try
{
for (int i = 0; i<=5; i++)
{
if (i==0)
{
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
System.out.println("Initialization Time for"
+ " task name - "+ name +" = " +ft.format(d));
//prints the initialization time for every task
}
else
{
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
System.out.println("Executing Time for task name - "+
name +" = " +ft.format(d));
// prints the execution time for every task
}
Thread.sleep(1000);
}
System.out.println(name+" complete");
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}}
我为仪器java ThreadPoolExecutor
创建了一个小代理,如下所示
public class Agent {
public static void premain(String arguments, Instrumentation instrumentation) {
new AgentBuilder.Default()
.with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
.type((ElementMatchers.nameContains("ThreadPoolExecutor")))
.transform(
new AgentBuilder.Transformer.ForAdvice()
.include(MonitorInterceptor.class.getClassLoader())
.advice(ElementMatchers.any(), MonitorInterceptor.class.getName())
).installOn(instrumentation);
}}
我们可以使用Byte Buddy来设置类似ThreadPoolExecutor的java类。当我调试ThreadPoolExecutor类工作。但当我尝试使用代理ThreadPoolExecutor类永远不会工作。
编辑这是我的MonitorInterceptor
public class MonitorInterceptor {
@Advice.OnMethodEnter
static void enter(@Advice.Origin String method) throws Exception {
System.out.println(method);
}
编辑
new AgentBuilder.Default()
.with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
.with(AgentBuilder.Listener.StreamWriting.toSystemError())
.ignore(none())
.type((ElementMatchers.nameContains("ThreadPoolExecutor")))
.transform((builder, typeDescription, classLoader, module) -> builder
.constructor(ElementMatchers.any())
.intercept(Advice.to(MyAdvice.class))
.method(ElementMatchers.any())
.intercept(Advice.to(MonitorInterceptor.class))
).installOn(instrumentation);
除非您明确配置它,否则Byte Buddy不会使用核心Java类。您可以通过显式设置不排除此类的忽略匹配器来更改它。
在此上下文中,使用Advice时不必配置初始化策略。
您可能还想限制建议的范围,现在您可以拦截任何方法或构造函数。
要找出问题所在,您还可以定义AgentBuilder.Listener以通知错误。
使用Rafael Winterhalter回答我解决了这个问题。我按如下方式创建代理,
new AgentBuilder.Default()
.ignore(ElementMatchers.none())
.type(ElementMatchers.nameContains("ThreadPoolExecutor"))
.transform((builder, type, classLoader, module) -> builder
.visit(Advice.to(ThreadPoolExecutorAdvice.class).on(ElementMatchers.any()))
).installOn(instrumentation);
使用这个,我们可以使用Java class.Form这个构造函数给出这样的
java.util.concurrent.ThreadPoolExecutor$Worker(java.util.concurrent.ThreadPoolExecutor,java.lang.Runnable)
但是在代码构造函数中,不是那样的,它是
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
所以我期待类似的东西
java.util.concurrent.ThreadPoolExecutor(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue)
我使用javassist来获取构造函数,并将其作为byte-buddy给出的内容。因此,使用.ignore(ElementMatchers.none())
和visit(Advice.to(ThreadPoolExecutorAdvice.class).on(ElementMatchers.any()))
we可以获得Java级别的所有构造函数和方法