切面使用示例

import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.common.message.MessageExt;
import java.util.List;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 1.@Pointcut()切点路径填错会导致切面失效
 * 2.@Order(1)排序重复会导致失效
 * 3.被切面的方法只能是以protect和public修饰(重点)
 *
 */
@Aspect
@Component
public class MqSwitchAspect {

    @Pointcut("execution(* com.taobao.metaq.client.MetaProducer.send(com.alibaba.rocketmq.common.message.Message)))")
    public void metaProducerSendPointCut() {
    }

    @Pointcut(
            "execution(* com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently.consumeMessage("
                    + "java.util.List,"
                    + "com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)))")
    public void metaConsumerConsumeMessagePointCut() {
    }

    @Around("metaProducerSendPointCut() && args(message)")
    public Object metaProducerSendAdvice(final ProceedingJoinPoint joinPoint,
            com.alibaba.rocketmq.common.message.Message message) throws Throwable {
        System.out.println("metaProducerSendAdvice");
        return joinPoint.proceed();
    }

    @Around("metaConsumerConsumeMessagePointCut() && args(msgs, context)")
    public Object metaConsumerConsumeMessageAdvice(final ProceedingJoinPoint joinPoint,
            final List msgs,
            final ConsumeConcurrentlyContext context) throws Throwable {
        System.out.println("metaConsumerConsumeMessageAdvice");
        return joinPoint.proceed();
    }

}

你可能感兴趣的:(java)