在使用spring task中,重写org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler实现接口org.springframework.scheduling.TaskScheduler中定时器的schedule系列方法。
TaskScheduler接口方法:
ScheduledFuture<?> schedule(Runnable task, Trigger trigger);
ScheduledFuture<?> schedule(Runnable task, Date startTime);
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period);
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period);
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay);
实现代码如下:
package com.zk.schedule;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
public class scheduleTest extends ThreadPoolTaskScheduler {
@Override
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
// TODO Auto-generated method stub
return super.schedule(this.runnableTest(task), trigger);
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
// TODO Auto-generated method stub
return super.schedule(this.runnableTest(task), startTime);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task,
Date startTime, long period) {
// TODO Auto-generated method stub
return super.scheduleAtFixedRate(this.runnableTest(task), startTime,
period);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
// TODO Auto-generated method stub
return super.scheduleWithFixedDelay(this.runnableTest(task), period);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task,
Date startTime, long delay) {
// TODO Auto-generated method stub
return super.scheduleWithFixedDelay(this.runnableTest(task), startTime,
delay);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
// TODO Auto-generated method stub
return super.scheduleWithFixedDelay(this.runnableTest(task), delay);
}
public Runnable runnableTest(Runnable task) {
return new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("==test==");
}
};
}
}
-----
配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean id="scheduletest" class="com.zk.schedule.scheduleTest" >
</bean>
<bean id="simpleTask" class="com.zk.schedule.SimpleTask"/>
<bean id="initBean" class="com.zk.schedule.InitBeanTest"></bean>
<task:scheduled-tasks scheduler="scheduletest">
<task:scheduled ref="simpleTask" method="print" fixed-rate="5000"/>
</task:scheduled-tasks>
</beans>
SimpleTask的源码:
package com.zk.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class SimpleTask {
private static int i = 0;
@Scheduled(fixedDelay = 1000)
public void print() {
System.out.println("===========start!=========");
System.out.println("I:" + i++);
System.out.println("=========== end !=========");
}
}
可以根据配置文件中的(下面是task:scheduled的不同配置方式)
<task:scheduled ref="simpleTask" method="print" fixed-rate="5000"/>
<task:scheduled ref="taskTest" method="test" cron="0 * * * * ?" />
cron express配置:
* * * * * * * 从左到右依次是:秒、分、小时、日、月、周、年
配置方式对应不同的Scheduled方法,执行方法runnableTest中的操作。