Spring定时器

<!-- 配置定时器 --> <bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="battleTimerTask"/> <property name="period" value="10000" /> <!-- 间隔,毫秒 --> <property name="delay" value="60000" /> <!-- 多久后启动Spring定时器,毫秒 --> </bean> <!-- Spring的TimerFactoryBean负责启动定时任务 --> <bean class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduleReportTask"/><!-- 定时器列表 --> </list> </property> </bean>

 

 

package cn.vicky.web.action.battle.timmer; import java.util.TimerTask; import javax.annotation.Resource; import org.springframework.stereotype.Component; import cn.vicky.model.po.Battle; import cn.vicky.service.battle.BattleService; @Component("battleTimerTask") public class BattleTimerTask extends TimerTask { @Resource private BattleService battleService; @Override public void run() { for (Battle battle : battleService.findAll()) { if (battle.isBegin()) { System.out.println(battle.getTeamID()); System.out.println(battle.getTargetTeamID()); System.out.println(battle.getTargetPlayerID()); System.out.println(battle.getBeginTime()); System.out.println(battle.getTime()); System.out.println("----------"); battleService.delete(battle.getId()); } } } }

你可能感兴趣的:(Spring定时器)