gpt4 book ai didi

java - 使用 AspectJ 了解调用者类

转载 作者:搜寻专家 更新时间:2023-11-01 01:04:09 25 4
gpt4 key购买 nike

我正在尝试模仿 Spring 的 AspectJ @Async support但是有一个消息总线。

问题是我需要知道是我的消息总线 (RabbitMQ MessageListener) 正在调用该方法还是调用方法将立即返回的普通(所有其他)调用者。

我的注释称为 @MQAsync 而不是 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;
}
}

call 切入点将为我提供调用者上下文,但这将不起作用,因为我将通过反射使用我的消息监听器调用该方法。 执行 切入点(已注释掉)不会告诉我谁在调用该方法。

有没有办法通过某种堆栈转储分析来确定调用者类?

最佳答案

实际上,cheeken 的回答很好,但是对于 AspectJ call() 切入点,您可以更轻松地获得调用类,并且没有丑陋的反射:

thisEnclosingJoinPointStaticPart.getSignature().getDeclaringType()

如果您认为这个答案比另一个更好,请考虑接受这个答案,否则请享受 AspectJ 的强大功能。 ;-)

关于java - 使用 AspectJ 了解调用者类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com