Spring的@schedule注解实现定时任务

2/13/2017来源:iOS开发人气:734

SPRing配置文件:springmvc-servlet.xml

xmlns加入

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation中加入

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


spring扫描注解的配置:即扫描base-package包下所有注解

<context:component-scan base-package="com.imwoniu.*" />


任务扫描注解

<task:executor id="executor" pool-size="5" />  
<task:scheduler id="scheduler" pool-size="10" />  
<task:annotation-driven executor="executor" scheduler="scheduler" />

在实现中:
	 /*每天16点38分执行一次这个方法。*/
        @Scheduled(cron = "0 38 16 * * ?")
	 public void addAwardPool(){
		GiftUtil.AWARD_POOL[0]=GiftUtil.AWARD_POOL[0]+10;
		GiftUtil.AWARD_POOL[1]=GiftUtil.AWARD_POOL[1]+50;
		GiftUtil.AWARD_POOL[2]=GiftUtil.AWARD_POOL[2]+30;
		
		GiftUtil.writeAwardMessageToFile(GiftUtil.AWARD_POOL,GiftUtil.AWARD_COUNT);//写到文件
	}