First, the regular tasks
1, which increases the injection start classes
@SpringBootApplication //@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan @Configuration @ServletComponentScan // scan filters servlet, filter annotations @MapperScan ( "net.Eleven.demo.Mapper") // scan file corresponding Mapper @EnableScheduling // scheduled tasks scheduled task annotations, scan package which all subclasses of @EnableAsync // enables asynchronous tasks public class XdclassApplication the extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(XdclassApplication.class); } public static void main(String[] args){ SpringApplication.run(XdclassApplication.class,args); } }
2, the timing of a new task class
package net.Eleven.demo.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.Date; /** * Function: regular tasks business class * */ @Component public class TestTask { // @Scheduled (fixedRate = 2000) // every two seconds to perform a @Scheduled (the cron = "* / * * * * *. 3") // every three seconds to perform a public void the sendEmail () { System.out.println ( "Current Time:" + new new a Date ()); } }
3, the timing of several configuration tasks
3.1, cron timer task expression @Scheduled (cron = "* / 1 * * * * *") denotes per
3.2, fixedRate: How often performed once the timing (after the last execution start time point is performed again xx seconds;)
3.3, fixedDelay: xx seconds after the last execution end point of time to perform again
3.4, fixedDelayString: a string, you can specify a configuration file
Second, asynchronous tasks
1, increase startup class notes (@EnableAsync // enables asynchronous task)
2, a new asynchronous task class
package net.Eleven.demo.task; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component @Async // class methods are all asynchronous public class the AsyncTask { @Async // methods are marked asynchronous public void Task1 () throws InterruptedException { Long the begin = System.currentTimeMillis (); Thread.sleep(1000L); long end = System.currentTimeMillis(); System.out.println ( "time-consuming task 1:" + (END- the begin)); } public void task2() throws InterruptedException{ long begin = System.currentTimeMillis(); Thread.sleep(2000L); long end = System.currentTimeMillis(); System.out.println ( "time-consuming task 2:" + (END- the begin)); } public void task3() throws InterruptedException{ long begin = System.currentTimeMillis(); Thread.sleep(3000L); long end = System.currentTimeMillis(); System.out.println ( "time-consuming task 3:" + (END- the begin)); } }
3, the controller which invoked this task
@Autowired private AsyncTask asyncTask; @GetMapping("/api/async") public JsonData doTask() throws InterruptedException{ long begin = System.currentTimeMillis(); asyncTask.task1(); asyncTask.task2(); asyncTask.task3(); long end = System.currentTimeMillis(); long totalTime = end-begin; System.out.println ( "time-consuming:" + totalTime); return JsonData.buildSuccess (totalTime); }
4, the results