运用spring搞了个简单是定时任务

项目经理说让我写个监听器 每隔多少时间调一次
但监听不会 就搞了任务调度不知道行不行

package temp;

import java.util.TimerTask;

public class TimerTest extends TimerTask{
int i;
public void setI(int i) {
this.i = i;
}
@Override
public void run() {
System.out.println("==================");
System.out.println(++i);
}
}
建一个timertest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" default-autowire="no">
<bean id="timertest" class="temp.TimerTest">
<property name="i" value="1"></property>
</bean>
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- delay是第一次启动延迟多少毫秒 -->
<property name="delay">
<value>10000</value>
</property>
<!-- 5秒运行一次 -->
<property name="period">
<value>5000</value>
</property>
<property name="timerTask">
<ref local="timertest"/>
</property>
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref local="scheduledTask"/>
</list>
</property>
</bean>
</beans>
在application.xml加上这句就可以跑起来了
<import resource="temp/timertest.xml"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值