@Scheduled:定时任务的注解
定时任务的配置:
1.xml配置
添加task的xml的命名空间
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
2.启用注解驱动的定时任务
<
task
:annotation-driven
scheduler
="threadPoolTaskScheduler"
/>
3.配置定时任务的线程池(如果不配线程池,默认定时任务为单线程)
<
bean
id
="threadPoolTaskScheduler"
class
="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"
>
<
property
name
="poolSize"
value
="16"
/>
<
property
name
="threadNamePrefix"
value
="taskScheduler_"
/>
<
property
name
="awaitTerminationSeconds"
value
="10"
/>
</
bean
>
代码:
package
com.caocao.web.job;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
/**
* 用户黑名单封号Task
* Created by tangchenglong on 2016/8/24.
*/
@Component
public class
CustomerFreezeTask {
private
Logger
logger
= LoggerFactory.
getLogger
(CustomerFreezeTask.
class
);
@Scheduled
(cron =
"* 0/2 * * * ?"
)
public void
queryCustomerBlackListToFreeze(){
//操作
}
}