Spring : Schedule a task which takes a parameter
Solution 1
According to the docs you cant.
Notice that the methods to be scheduled must have void returns and must not expect any arguments.
Solution 2
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)
Solution 3
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));
}
Related videos on Youtube
Shashank Vashistha
Updated on June 04, 2022Comments
-
Shashank Vashistha almost 2 years
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?