The enabling of springSchedule of springMVC timing task

First, you need to understand cron expressions

1、second

2、minute

3、hours

4. day-of-mouth (date within the month)

5、mouth

6. day-of-week

7. year (optional)

Learn about special fields: , * - / L w C# usage

There are tools available online for cron expression generators

2. Add in applicationContext.xml

<!--Add timed task-->
    <task:annotation-driven/>


Note: Screenshots are important because there are multiple annotation-driven ones under /schema/task

3. Before creating a new task class, you need to add the @component annotation to inject the new class into the spring container (but adding the annotation has no effect when I test, so it is injected in the form of <bean> in applicationContext.xml. in the spring container) on the code:

 

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@ Slf4j
public class taskTest {
    @Scheduled(cron = "*/30 * * * * ?") //30 seconds update
    public void test(){
        log.info("Timer started");
        for(int i=0;i<1000;i++){
            if (i%100==0){
                log.info("i="+i+"");
            }
        }
        log.info("Timer is over");
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167609&siteId=291194637