During application development we require some task should be executed automatically based on particular time interval. for example for banking application credit card bill should be generated by 1st date of month and send email notification to user. or send automatic reminder to user about pay bill so in this case we need one job which will execute after time particulate time period.

To complete this requirement spring provide cron scheduler which will executed based on specified time interval expression.

The @Scheduled annotation can be added to a method along with trigger metadata.

@EnableScheduling           // This annotation is require to enable cron scheduling


package com.javadeveloperzone.configs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
 * Created by Subhash Lamba on 19-01-2017.
@EnableScheduling           // This annotation is require to enable cron scheduling
public class SpringWebConfig extends WebMvcConfigurerAdapter {
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        return viewResolver;



package com.javadeveloperzone.controller;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class CronController {
    @RequestMapping(value = "demo",method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    If a fixed rate execution is desired, simply change the property name specified within the annotation.
    The following would be executed every 5 seconds measured between the successive start times of each invocation.
    public void ScheduledFixedRate(){
        System.out.println("I will execute after evey 5 seconds");
    For fixed-delay and fixed-rate tasks,
    an initial delay may be specified indicating the number of milliseconds to wait before the first execution of the method.
    @Scheduled(initialDelay=1000, fixedRate=5000)
    public void ScheduledInitialDelay(){
        System.out.println("I will execute after evey 5 seconds but after initial Delay 1 second");
    If simple periodic scheduling is not expressive enough, then a cron expression may be provided.
    For example, the following will only execute on weekdays.
    @Scheduled(cron="*/5 * * * * MON-FRI")
    public void ScheduledWeekEnd() {
        System.out.println("I will execute should execute on weekdays only");

 Note  : We can generate cron job from here http://www.cronmaker.com/ and use in our framework.

Was this post helpful?

1 comment. Leave new

subhash lamba
March 11, 2017 8:34 pm

nice. working fine.

Leave a Reply

Your email address will not be published. Required fields are marked *