Scheduling a Task in Spring MVC

11,184

Solution 1

You should try TaskScheduler, see the javadoc here:

private TaskScheduler scheduler = new ConcurrentTaskScheduler();

@PostConstruct
private void executeJob() {
    scheduler.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            // your business here
        }
    }, INTERVAL);
}

Solution 2

Refer Spring Task Execution and Scheduling

Example Annotations

@Configuration
@EnableAsync
@EnableScheduling
public class MyComponent {

    @Async
    @Scheduled(fixedDelay=5000, repeatCount=0)
    public void doSomething() {
       // something that should execute periodically
    }
}

I think the repeatCount=0 will make the function execute only once (yet to test)

Full example with Quartz scheduler http://www.mkyong.com/spring/spring-quartz-scheduler-example/

You need to introduce XML configuration as follows

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>}
Share:
11,184
anand mishra
Author by

anand mishra

Updated on June 04, 2022

Comments

  • anand mishra
    anand mishra almost 2 years

    In my Spring MVC application i need to schedule a task with specific date & Time. Like- i have to schedule to send a email which will be configured dynamically by customer. In Spring @Schedule annotation is there but how can i change value dynamically every time with any date & Time.

    Any help is appreciated.