Spring : Schedule a task which takes a parameter
Asked Answered
S

5

10

I have a class with the following function:

public class classA{

... 
...

void function_to_be_scheduled(String param){
    ...
    ...
}
}

I want to schedule the function using the scheduled-tasks element of the task namespace.

<task:scheduled-tasks>
    <task:scheduled ref="beanA" method="function_to_be_scheduled" cron="${cron}"/>
</task:scheduled-tasks>

How do i pass the parameter to the function which i want to schedule?

Sideling answered 1/4, 2015 at 10:33 Comment(0)
O
11

According to the docs you cant.

Notice that the methods to be scheduled must have void returns and must not expect any arguments.

Outdoors answered 1/4, 2015 at 10:40 Comment(0)
V
4

The Spring doc about scheduling says:

Notice that the methods to be scheduled must have void returns and must not expect any arguments

Since the parameter comes from the Spring config file you can declare a bean (es beanB which wraps beanA) in the spring file, inject the parameter you need in the bean and the schedule the execution of a method of the bean which knows the parameter (it could be a simple wrapper of your beanA)

Vertigo answered 1/4, 2015 at 10:43 Comment(0)
S
2

You can use TaskScheduler and encapsule your logic with a parameter in Runnable:

@Autowired
private TaskScheduler scheduler;

public void scheduleRules() {
    MyTask task = new MyTaskImpl(someParam);
    // new CronTrigger
    scheduler.scheduleAtFixedRate(task, Duration.ofMinutes(1));
}
Sassoon answered 23/3, 2019 at 16:36 Comment(0)
W
0

I've found that the only way to do this is to have a façade method that is @Scheduled and knows the default value required. The useful side-effect of this is that you can also provide an API through a @Controller to provide manual triggering with a specific parameter - useful if you need to re-run an activity.

@Scheduled(cron = "${myChronSchedule}")
public void generateActivities() {

    this.generateActivities(LocalDate.now());

}

public void generateActivities(LocalDate theDate) {
    // do the work
    ...
}

If you don't need the façade to be public, there's no reason why it can't be private and no-one is the wiser.

Whencesoever answered 3/10, 2022 at 13:53 Comment(0)
F
-1

The Task Scheduler did the trick for me

  1. First create a configuration class called ThreadPoolTaskScheduler class. Find details Here!

  2. Then create a class where the magic happens

    @Component
    public class ThreadPoolTaskSchedulerExample {
    
    @Autowired
    private ThreadPoolTaskScheduler taskScheduler;
    
    
         class EmailWatch implements Runnable{
    
             private String userEmail;
    
    
             public EmailWatch(String userEmail){
                 this.userEmail = userEmail;
             }
    
    
             @Override
             public void run() {
                  System.out.println("This is the email "+ userEmail);
             }
         }
    
    
         public void watchEmail(String userEmail)  {
    
       //refresh watch every minute
             CronTrigger cronTrigger = new CronTrigger("0 * * ? * *"); 
             taskScheduler.schedule(new EmailWatch(userEmail));
         }
     }
    
Frechette answered 9/6, 2021 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.