I’m trying to imitate Spring’s AspectJ @Async support but with a message bus.
The issue is I need to know if my Message Bus (RabbitMQ MessageListener) is calling the method or a normal (all others) caller where the method will return instantly.
My annotation is 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 the method parameters to the message bus.
//this logic isn't here for brevity.
return null;
}
}
The call pointcut will get me the caller context but that will not work as I will be calling the method with my message listener through reflection. The execution pointcut (commented out) will not tell me who is calling the method.
Is there a way to determine the caller class maybe through some sort of stack dump analysis?
You can determine which class is invoking the current method with the following call. Note that you’ll have to catch
ClassNotFoundException(unless you’re satisfied simply retrieving the name as aString).Why the third element? Because the stack is ordered like so when the stack trace method is invoked:
Thread#getStackTrace()CurrentClass.currentMethod()ParentClass.parentMethod()