java - Try to use Agent in Webapplication for bytecode Manupulation -
i'm not in java have webapplication running on wildfly. have 3 threads call function insert logs in in , function saves logs database , after every thread sends time how long did takes this. send data programm wrote has 3 threads call 1 of 3 server threads.
so try bytecode manipulation every thread on server saves datetime call log function waits 1 second , returns time needed.
1 thread write in logfile before or after waitet 1 second. part wait second , call log function want inject every 3 threads bytecode manipulation.
public class mytransformer implements classfiletransformer { @override public byte[] transform(classloader loader, string classname, class redefiningclass, protectiondomain protectiondomain, byte[] bytes) throws illegalclassformatexception { return transformclass(redefiningclass, bytes); } private byte[] transformclass(class classtotransform, byte[] b) { classpool pool = classpool.getdefault(); ctclass cl = null; try { cl = pool.get("de.soptim.ws.myapplication"); } catch (javassist.notfoundexception e) { e.printstacktrace(); } try { assert cl != null; ctmethod[] methods = cl.getmethods(); (int = 0; < methods.length; i++) { if (methods[i].isempty() == false) { changemethod(methods[i]); } } b = cl.tobytecode(); } catch (exception e) { e.printstacktrace(); } { if (cl != null) { cl.detach(); } } return b; } private void changemethod(ctmethod method) throws notfoundexception, cannotcompileexception { if (method.hasannotation(loggable.class)) { method.insertbefore("threadlogger.info(\"added bytecode !!!\");"); method.insertafter("threadlogger.info(\"added bytecode !!!\");"); } }}
thats transformer class should increase code methods need checks method has @loggable annotation adds code it("at moment it's log statments checking if works")
my biggest problem don't know how call agent ... googled hwo call agent @ runtime agentmain() think dind't understood how works.
agent class
public class logagent { public static void agentmain(string agentargs, instrumentation inst) { system.out.println("starting agent"); inst.addtransformer(new mytransformer()); }}
hope understand problem :) , if answere pleas try stay noob friendly :d.
you don't call agent explicitly, should specify additional argument jvm :
java -javaagent:jarpath[=options]
where jarpath
path jar containing agent. jvm invoke premain
method before main
method of java program.
and transform
method called before classloading jvm (you don't call explicitly).
last remark : should implement premain
method, not agentmain
. agentmain
used during attaching running vm, while premain
used when start jvm -javaagent
method.
and make sure jar have valid manifest, described : https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html
i haven't using javaassit cannot code valid, instrumenting webapp server wildfly harder normal java app (mostly due classloaders visibility , hierarchy).
see :
http://www.tomsquest.com/blog/2014/01/intro-java-agent-and-bytecode-manipulation/ tutorials javaagents
Comments
Post a Comment