Spring AOP - 判断方法是否被@Scheduled调用

问题描述 投票:0回答:3

我有一个运行时注释@MyAnnotation,我想编写一个Aspect来确定下面的test()方法是否被调用:

  • Spring 的@Scheduled 框架
  • 普通方法调用
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Scheduled(cron = "*/1 * * * * *") // scheduled to invoke every second
    @MyAnnotation
    public void test() {
        // business logic
    }
}

方面代码(切入点+建议)

    @Around(value="@annotation(myAnnotation)")
    public Object featureToggle(ProceedingJoinPoint joinPoint, MyAnnotation myAnnotation) throws Throwable {
        Boolean isInvoked = // TODO - is invoked by @Scheduled or not
    }
spring aop aspectj spring-aop spring-scheduled
3个回答
0
投票

检查堆栈跟踪总是很难看,但你当然可以做到:

package de.scrum_master.spring.q65397019;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}
package de.scrum_master.spring.q65397019;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
  @Scheduled(fixedRate = 1000)
//  @Async
  @MyAnnotation
  public void doSomething() {}
}
package de.scrum_master.spring.q65397019;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@Configuration
@EnableScheduling
@EnableAsync
public class DemoApplication {
  public static void main(String[] args) throws InterruptedException {
    try (ConfigurableApplicationContext appContext = SpringApplication.run(DemoApplication.class, args)) {
      doStuff(appContext);
    }
  }

  private static void doStuff(ConfigurableApplicationContext appContext) throws InterruptedException {
    MyComponent myComponent = appContext.getBean(MyComponent.class);
    myComponent.doSomething();
    Thread.sleep(1000);
    myComponent.doSomething();
  }
}
package de.scrum_master.spring.q65397019;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class MyAspect {
  @Around("@annotation(myAnnotation)")
  public Object advice2(ProceedingJoinPoint joinPoint, MyAnnotation myAnnotation) throws Throwable {
    if (
      Arrays.stream(new Exception().getStackTrace())
        .map(StackTraceElement::toString)
        .anyMatch(string -> string.contains("scheduling.support.ScheduledMethodRunnable.run("))
    )
      System.out.println(joinPoint + " -> scheduled");
    else
      System.out.println(joinPoint + " -> normal");
    return joinPoint.proceed();
  }
}

这将打印类似以下内容:

(...)
2020-12-22 10:00:59.372  INFO 1620 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
execution(void de.scrum_master.spring.q65397019.MyComponent.doSomething()) -> scheduled
2020-12-22 10:00:59.456  INFO 1620 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-22 10:00:59.456  INFO 1620 --- [           main] d.s.spring.q65397019.DemoApplication     : Started DemoApplication in 6.534 seconds (JVM running for 8.329)
execution(void de.scrum_master.spring.q65397019.MyComponent.doSomething()) -> normal
execution(void de.scrum_master.spring.q65397019.MyComponent.doSomething()) -> scheduled
execution(void de.scrum_master.spring.q65397019.MyComponent.doSomething()) -> normal
2020-12-22 10:01:00.475  INFO 1620 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
2020-12-22 10:01:00.477  INFO 1620 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
(...)

在 Java 9+ 上,您可以使用堆栈遍历 API,这比从异常实例创建完整堆栈跟踪或从当前运行的线程查询它们更有效。

警告: 如果您还使用

@Async
注释您的计划方法,那么这将不再起作用,因为异步运行的方法没有堆栈跟踪,您可以在其中识别它是由
ScheduledMethodRunnable
或应用程序类。


0
投票

也许你想实现这样的目标:

@Slf4j
@Component
public class ScheduledTask {

    @Scheduled(cron = "0/1 * * * * *")
    @ScheduledTaskAnnotation(message = "ScheduledTaskMessage", number = 10)
    public void doAction() {
        log.debug("Task scheduled");
    }

}
@Slf4j
@Aspect
@Component
public class ScheduledTaskAspect {

    @Around("execution(public * *(..)) && @annotation(hu.gaszabo.sample.schedule.ScheduledTaskAnnotation)")
    public void logScheduledTaskAction(final ProceedingJoinPoint p) {
        log.debug("Aspect");

        parameters(p).ifPresent(a -> {
            log.debug("message: {}", a.message());
            log.debug("number: {}", a.number());
        });

        try {
            p.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private Optional<ScheduledTaskAnnotation> parameters(final ProceedingJoinPoint p) {
        final Method method = ((MethodSignature) p.getSignature()).getMethod();
        return Optional.ofNullable(AnnotationUtils.findAnnotation(method, ScheduledTaskAnnotation.class));
    }

}
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.METHOD })
public @interface ScheduledTaskAnnotation {

    String message() default "Message";

    long number() default 0L;

}

0
投票

可以设置schedule线程名称前缀,然后通过线程名称来确定。

spring
  task:
    scheduling:
      thread-name-prefix: xxxx-scheduling-
@Aspect
@Component
@RequiredArgsConstructor
public class ScheduledTaskAspect {
    private final TaskSchedulingProperties taskSchedulingProperties;

    @Around(value="@annotation(myAnnotation)")
    public Object featureToggle(ProceedingJoinPoint joinPoint, MyAnnotation myAnnotation) throws Throwable {
        String currentThreadName = Thread.currentThread().getName();
        Boolean isInvoked = currentThreadName.startsWith(taskSchedulingProperties.getThreadNamePrefix());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.