Spring中的scheduler 示例

有些时候在我们的应用中需要每隔一定的时间执行一次,提供了方便的时间调度表达式。

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;


public class TaskSchedulerTest {
    public static void main(String args[]){
    	 Runnable runnable = new Runnable() {
             public void run() {
                 
                 Date date = new Date(System.currentTimeMillis());
                 System.out.println("TEST");
             }
         };
         
         ScheduledExecutorService localExecutor =  Executors.newSingleThreadScheduledExecutor();;
         TaskScheduler taskScheduler =  new ConcurrentTaskScheduler(localExecutor);
         
         ScheduledFuture<?>   scheduledFuture = taskScheduler.schedule(runnable, new CronTrigger("*/5 * * * * *"));
    }
}



你可能感兴趣的:(spring,scheduler)