java - Knowing caller class with AspectJ -
i'm trying imitate spring's aspectj @async support message bus.
the issue need know if message bus (rabbitmq messagelistener) calling method or normal (all others) caller method return instantly.
my annotation called @mqasync instead of springs @async.
package com.snaphop.mqueue; import org.apache.log4j.logger; import com.snaphop.mqueue.mqasync; public aspect mqasyncaspect { //pointcut asynctypemarkedmethod() : execution(@mqasync void *(..)); pointcut asynctypemarkedmethod() : call(@mqasync void *(..)); private static final logger log = logger.getlogger("mqasync"); object around() : asynctypemarkedmethod() { if (listeneriscaller) { return proceed(); } //send method parameters message bus. //this logic isn't here brevity. return null; } }
the call pointcut me caller context not work calling method message listener through reflection. execution pointcut (commented out) not tell me calling method.
is there way determine caller class maybe through sort of stack dump analysis?
you can determine class invoking current method following call. note you'll have catch classnotfoundexception
(unless you're satisfied retrieving name string
).
class.forname(thread.currentthread().getstacktrace()[2].getclassname());
why third element? because stack ordered when stack trace method invoked:
thread#getstacktrace()
currentclass.currentmethod
()
parentclass.parentmethod
()
Comments
Post a Comment