bytebuddy 홈페이지의 샘플을 기반으로 간단한 자바 에이전트를 작성하려고합니다. 에이전트가 작동하지만 OSGI 런타임으로 실행하면 java.lang.NoClassDefFoundError가 발생합니다.
어떤 포인터?
java.lang.ClassNotFoundException: com.foo.javaagent.TimingInterceptor cannot be found by ..
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.Instrumentation;
public class TimerAgent {
public static void premain(String arguments,
Instrumentation instrumentation) {
new AgentBuilder.Default()
.type(ElementMatchers.nameEndsWith("World"))
.transform((builder, type, classLoader, module) ->
builder.method(ElementMatchers.any())
.intercept(MethodDelegation.to(TimingInterceptor.class))
).installOn(instrumentation);
}
}
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
public class TimingInterceptor {
@RuntimeType
public static Object intercept(@Origin Method method,
@SuperCall Callable<?> callable) throws Exception {
long start = System.currentTimeMillis();
try {
return callable.call();
} finally {
System.out.println(method + " took " + (System.currentTimeMillis() - start));
}
}
}
TimingInterceptor
클래스는 계측 클래스에 의해 참조 때문에 볼 수 있어야합니다. OSGi는 클래스 로더로 클래스를 분리하고 시스템 클래스 로더를 에이전트가로드되는 상위로 설정하지 않습니다. 이를 우회하려면 클래스를 보편적으로 볼 수있는 부트 스트랩 클래스 로더에 삽입해야합니다. 이렇게하려면 차단 논리를 별도의 jar에 분리 Instrumentation
하고 에이전트를 설치하는 데 사용 하는 인스턴스를 통해이 jar를 부트 스트랩 클래스 로더 검색 경로에 연결합니다 . 에이전트를 설치하기 전에이를 수행해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다