通常來說蜂挪,如果要執(zhí)行一個定時任務(wù)耐量,基本上的操作是這樣的:
public class Task3 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
// 第二個參數(shù)為首次執(zhí)行的延時時間,第三個參數(shù)為定時執(zhí)行的間隔時間
service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS);
}
}
這種情況要main方法中顯式的調(diào)用scheduleAtFixedRate方法才能運(yùn)行蛤虐。但是在Spring環(huán)境中或者Web環(huán)境中沒有main方法获印,或者沒有顯式的調(diào)用入口,該如何調(diào)用調(diào)用這個方法呢胆建?
1.使用InitializingBean烤低,當(dāng)類被初始化時scheduleAtFixedRate被顯式調(diào)用:
public class ScheduleTask implements InitializingBean,Runnable{
@Override
public void run() {
System.out.println( new Date() +"任務(wù)執(zhí)行");
}
@Override
public void afterPropertiesSet() throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 第二個參數(shù)為首次執(zhí)行的延時時間,第三個參數(shù)為定時執(zhí)行的間隔時間
service.scheduleAtFixedRate(this, 10, 1, TimeUnit.SECONDS);
}
}
beans.xml:
<bean id="task" class="com.alibaba.test.scheduling.ScheduleTask"/>
2. 使用 @Scheduled注解:
public class ScheduleTask{
@Scheduled(fixedRate=1)
public void run() {
System.out.println( new Date() +"任務(wù)執(zhí)行");
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:sofa="http://schema.alipay.com/sofa/schema/service"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://schema.alipay.com/sofa/schema/service http://schema.alipay.com/sofa/sofa-service-4-0-0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="task" class="com.alibaba.test.scheduling.ScheduleTask"/>
<task:annotation-driven>
</task:annotation-driven>
</beans>
當(dāng)然也可以指定定時任務(wù)線程池·笆载。
3.只使用使用XML配置:
public class ScheduleTask{
public void run() {
System.out.println( new Date() +"任務(wù)執(zhí)行");
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:sofa="http://schema.alipay.com/sofa/schema/service"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://schema.alipay.com/sofa/schema/service http://schema.alipay.com/sofa/sofa-service-4-0-0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="task" class="com.alibaba.test.scheduling.ScheduleTask"/>
<task:scheduler id="scheduler" pool-size="10" /> <!--配置定時器-->
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="task" method="run" fixed-delay="1000" />
</task:scheduled-tasks>
</beans>