自定义timerTask继承java.util.TimerTask,并重写run方法;例如:
public class MyTimerTask extends TimerTask{ private static Logger logger = Logger.getLogger(MyTimerTask.class); @Override public void run() { // 主要内容 System.out.println("run in myTimerTask..."); } }
applicationContext.xml中配置如下:
<bean name="myTimerTask" class="com.copsec.railway.im.timerTask.MyTimerTask"></bean> <bean id="scheduledAmTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="myTimerTask" /> <property name="period"> <value>20000</value> </property> </bean> <bean class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduledAmTask" /> </list> </property> </bean> <!-- timer Task 配置结束 -->