Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointcut for methods with @Scheduled Spring annotation

I want to have a AspectJ pointcut for methods annotated with @Scheduled. Tried different approaches but nothing worked.

1.)

@Pointcut("execution(@org.springframework.scheduling.annotation.Scheduled * * (..))")
public void scheduledJobs() {}

@Around("scheduledJobs()")
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable {
    LOG.info("testing")
}

2.)

@Pointcut("within(@org.springframework.scheduling.annotation.Scheduled *)")
public void scheduledJobs() {}

@Pointcut("execution(public * *(..))")
public void publicMethod() {}

@Around("scheduledJobs() && publicMethod()")
public Object profileScheduledJobs(ProceedingJoinPoint joinPoint) throws Throwable {
    LOG.info("testing")
}

Can anyone suggest any other way to have around/before advice on @Scheduled annotated methods?

like image 273
Rahul Avatar asked Jun 01 '13 18:06

Rahul


People also ask

What is @scheduled annotation in Spring?

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.

What is @scheduled in Spring?

We can turn any method in a Spring bean for scheduling by adding the @Scheduled annotation to it. The @Scheduled is a method-level annotation applied at runtime to mark the method to be scheduled. It takes one attribute from cron , fixedDelay , or fixedRate for specifying the schedule of execution in different formats.

What is Pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

What method does the Pointcut expression below reference?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")


1 Answers

The pointcut that you are looking for can be specified as below:

@Aspect
public class SomeClass {

    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public void doIt(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("before");
        pjp.proceed();
        System.out.println("After");
    }
}

I am not sure whether that's all you require or not. So I'm going to post the other parts of the solution as well.

First of all, notice the @Aspect annotation on the class. It is required for the methods in this class to be applied as advice.

Also, you need to make sure that the class that has the @Scheduled method is detectable via scanning. You can do so by annotation that class with @Component annotation. For ex:

@Component
public class OtherClass {
    @Scheduled(fixedDelay = 5000)
    public void doSomething() {
        System.out.println("Scheduled Execution");
    }
}

Now, for this to work, the required parts in your spring configuration would be as follows:

<context:component-scan base-package="com.example.mvc" />
<aop:aspectj-autoproxy />   <!-- For @Aspect to work -->    
<task:annotation-driven />  <!-- For @Scheduled to work -->
like image 134
Bhashit Parikh Avatar answered Oct 02 '22 21:10

Bhashit Parikh