Java方式:
Main Class 上添加@EnableScheduling注解,Controller的run方法上添加@Scheduled(cron = "${specify.scheduler.expression}", zone = "GMT+8")
XML方式:(举公司的一个例子)
定时任务用到了Spring 的@Scheduled注解,如果是采用xml配置文件的形式,需要加入task的命名空间,即xmlns:task,并且在xsi:schemaLocation中加上spring-task的xsd:
<?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/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- omit others -->
</beans>
配置线程池:
<task:scheduler id="taskScheduler" pool-size="10" />
配置定时任务:?
<task:scheduled-tasks scheduler="taskScheduler">
<task:scheduled ref="housekeepDB" method="run" cron="xxx"/>
<task:scheduled ref="checkInactiveAccount" method="run" fixed-delay="xxx" initial-delay="yyy"/>
</task:scheduled-tasks>
Bean配置的例子:
<bean id="housekeepDB" class="xxx.yyy.zzz.HousekeepDB">
<property name="filePath" value="${pgsql.housekeep.path}"/>
</bean>
<bean id="checkInactiveAccount" class="xxx.yyy.zzz.CheckInactiveAccount">
<property name="queueManager" value="#{'${daemon.instance}'.equals('1') ? '${jms.connection.factory.queueManager.1}' : '${jms.connection.factory.queueManager.2}'}"/>
<property name="queue" value="xxx"/>
<property name="host" value="#{'${daemon.instance}'.equals('1') ? '${jms.connection.factory.hostName.1}' : '${jms.connection.factory.hostName.2}'}"/>
<property name="port" value="#{'${daemon.instance}'.equals('1') ? '${jms.connection.factory.port.1}' : '${jms.connection.factory.port.2}'}"/>
<property name="channel" value="#{'${daemon.instance}'.equals('1') ? '${jms.connection.factory.channel.1}' : '${jms.connection.factory.channel.2}'}"/>
</bean>
<bean id="checkXXX" class="xxx.yyy.zzz.CheckXXX"></bean>
另外,可配置任务执行器详细参数如下:?? task:executor/@pool-size:可以指定执行线程池的初始大小、最大大小? task:executor/@queue-capacity:等待执行的任务队列的容量? task:executor/@rejection-policy:当等待队列爆了时的策略,分为丢弃、由任务执行器直接运行等方式
<task:executor id="taskExecutor" pool-size="1" />
<!-- or another example -->
<task:executor id="taskExecutor" keep-alive="3600" pool-size="1" queue-capacity="500" rejection-policy="CALLER_RUNS" />
Bean的例子:
定时任务的应用场景经常是housekeep,和后台任务去检查某种需求,下面就举check inactive account和housekeep的例子:
1.?check inactive account
public class CheckInactiveAccount implements Runnable, Lifecycle {
// omit logger
private MQQueueManager queueManager;
private String qmgr, queue, host, port, channel;
private volatile boolean stop, isRunning = false;
private TaskExecutor taskExecutor;
public void start() {
if (!this.isRunning) {
this.taskExecutor.execute(this);
}
}
public void stop() {
this.stop = true;
}
public void run() {
checkInactiveAccount();
}
public boolean isRunning() {
return this.isRunning;
}
// omit checkInactiveAccount() details
}
checkInactiveAccount涉及到读取PostgreSQL的查删改和通过MQ发邮件通知用户的操作。
2. housekeep db的scheduler job
读取配置文件xml的设计是:
<?xml version="1.0" encoding="UTF-8" ?>
<housekeep>
<daily-db-housekeeping>
<table name="xxx">
<day>111</day>
</table>
<table name="yyy">
<app_name>zzz</app_name>
<day>222</day>
</table>
<!-- ... -->
</daily-db-housekeeping>
<weekly-db-housekeeping>
<table name="xxx">
<month>333</month>
</table>
<month>444</month>
</table>
<!-- ... -->
</weekly-db-housekeeping>
</housekeep>
|