项目中调用微信公众平台的接口时,因为获取到的accessToken及jsapiTicket有效时长只有两个小时,需要不断更新。
所以做了个定时任务,记录一下。
.SpringTask实现有两种方式,一种是注解,一种是配置。我这个是配置xml实现的,实现步骤具体如下:
第一步:新建spring-task.xml,并配置定时任务。(先获取accessToken,在随即获取jsapiTicket)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
记得扫描.xml
contextConfigLocation
classpath:spring-task.xml
cron="0/2 * * * * ? //每两秒执行一次,测试的时候用。关于Spring Task 的 cron表达式,请参见另一篇博客:
第二步:接下来就是写任务类:(测试用)
public class TaskJob {
public void job1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date now = new Date();
System.out.println("******************任务进行中****************"+sdf.format(now));
}
public void job2() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date now = new Date();
System.out.println("******************任务进行中****************"+sdf.format(now));
}
}