首页 > 代码库 > Spring @Scheduled

Spring @Scheduled

Spring @Scheduled是Spring计划任务的一种很简洁的实现。用来替代Quartz的方案。


要使用此特性,需要Spring3.2以上版本。用法:


1、在xml的配置中,需要加入:

            http://www.springframework.org/schema/task

            http://www.springframework.org/schema/task/spring-task-3.2.xsd"


2、写一个简单例子:

import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by leizhimin on 2014/8/16.
 *
 * @author leizhimin 2014/8/16 17:46
 */
@Component
@Lazy(false)
public class TestJob {
    public static SimpleDateFormat sdf_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");


    @Scheduled(cron = "0/5 * * * * ?")
    public void exejob() {
        System.out.println(sdf_yyyyMMddHHmmss.format(new Date()) + " :执行中。。。");
    }
}


每隔5秒中就会输出一条信息。



本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/1541841