Instructions for use and it works:
Package com.example.spring.async; Import org.springframework.scheduling.annotation.Scheduled; Import org.springframework.stereotype.Service; Import com.example.spring.MyLog; / ** * timer task uses examples * 1. Start class adds annotation @EnableScheduling * 2. respective service class declaration @Service * 3. the above method of increasing @Scheduled, specify parameters for running a timing different tasks in different ways * Note: * in @Scheduled of parameters: * fixedRate: expressed in fixed time intervals (time interval between the beginning of the last execution and this started), is not concerned about the actual implementation of the method of execution time * fixedDelay: expressed as a fixed delay execution (the end of the last execution and this started time interval), is affected by the method execution time * cron = "2 * * * * *": cron expression configured to perform the method. A total of six, representing the sun and the moon seconds timeshare week * spring underlying actuator :( default is single-threaded thread pool) Executors.newSingleThreadScheduledExecutor this.localExecutor = * (); * = this.taskScheduler new new ConcurrentTaskScheduler (this.localExecutor); * loading process: * ScheduledAnnotationBeanPostProcessor -> ScheduledTaskRegistrar -> afterPropertiesSet () * @Scheduled method must be empty and no return value reference method, this method is called directly synchronized * @DESC * @author guchuang * * / @Service public class ScheduleMethod { public ScheduleMethod () { MyLog.info ( "-------------- ScheduleMethod the init -------------------------- --------- " ); } / * @Scheduled (fixedRate = 2000) void foo1 public () { MyLog.info ( "performed once every 2 seconds, regardless of the time of last execution is completed, fixedRate = 2000"); } @Scheduled (FIXEDDELAY = 2000) public void foo2 () { MyLog.info ( "Last execution is completed, the execution 2s, this recurrence, FIXEDDELAY = 2000 "); } * / @Scheduled (fixedRate = 2000 ) public void foo3 () { MyLog.info ( " performed once every 2 seconds, regardless of the previous complete execution time, fixedRate = 2000 " ); MyLog.sleep ( 1000 ); } / * @Scheduled (FIXEDDELAY = 10000) public void foo4 () { //MyLog.info (" last executed, executed after 2s, this delivery push, FIXEDDELAY = 3000 "); MyLog.info (" last executed, executed after 2s, this recurrence,fixedDelay=3000 ----start"); MyLog.sleep (1000); MyLog.info ( "last executed, executed after 2s, this recurrence, FIXEDDELAY = 3000 ---- End"); } * / @Scheduled (the cron = "* / 2 * * * * * " ) public void foo5 () { MyLog.info ( " Test - the cron executed once every two seconds " ); } }
Configuration class:
package com.example.spring.async.config; import java.util.concurrent.Executors; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import com.example.spring.MyThreadFactory; @Configuration public class ScheduleConfig the implements SchedulingConfigurer { / ** * TaskScheduler injected into the spring containers thread pool, for performing the method @Scheduled annotations marked. * type TaskScheduler.class, name of the taskExecutor1 bean (using injection type spring, not bean name) * If there is no injection or TaskScheduler ScheduledExecutorService, the default single-threaded thread pool as an underlying support * @return TaskScheduler instance * / @Bean public TaskScheduler, taskExecutor () { return new new ConcurrentTaskScheduler (Executors.newScheduledThreadPool (. 3, new new MyThreadFactory ( "Scheduled" ))); } @Bean @Qualifier ( "Test-123") Public the TaskScheduler taskExecutor2 () { return new new ConcurrentTaskScheduler (Executors.newScheduledThreadPool (. 3, new new MyThreadFactory ( "scheduled2" ))); } / ** * may be used to perform scheduled tasks, and the like provided taskScheduler * / @Override public void configureTasks ( taskRegistrar ScheduledTaskRegistrar) { // taskRegistrar.setScheduler (taskExecutor1 ()); an actuator for setting the thread pool displayed taskRegistrar.addFixedDelayTask (() -> System.out.println ( "SchedulingConfigurer Test"), 5000 ); } }
Test category
package com.example.spring.async; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.example.spring.BaseDemoApplicationTest; import com.example.spring.MyLog; import com.example.spring.async.ScheduleMethod; public class ScheduleMethodTest extends BaseDemoApplicationTest { @Autowired ScheduleMethod schedule; @BeforeClass public static void setUpBeforeClass() throws Exception { } @Before public void setUp() throws Exception { } @Test public void test() { MyLog.sleep(1000 * 30); } }