更新時間:2022-03-25 09:37:34 來源:動力節點 瀏覽2210次
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
配置掃描任務
<task:annotation-driven/>
配置掃描位置
<context:component-scan base-package="org.rgdata.task"/>
包org.rgdata.task;
導入 java.io.IOException;
導入 java.util.Date;
導入 org.springframework.scheduling.annotation.Scheduled;
導入 org.springframework.stereotype.Component;
導入 org.rgdata.cache.CacheAccessToken;
導入 org.rgdata.util.HttpUtil;
導入 com.alibaba.fastjson.JSONObject;
@零件
公共類 AccessTokenTask {
//@Scheduled(cron = "0 */2 * * * ?")
//長時間 = 6600000; 1 小時 50 分鐘 = 6600000 毫秒
@Scheduled(fixedRate = 6600000)
公共無效getAccessToken(){
System.out.println("每1小時50分鐘執行一次" + new Date());
字符串網址="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx413b867cd2957212&secret=c39b607e5550562a79e2674815808679";
字符串結果 = null;
嘗試 {
結果 = HttpUtil.http(url, null);
} 捕捉(IOException e){
e.printStackTrace();
}
System.out.println("返回值為:"+result);
JSONObject json = JSONObject.parseObject(result);
String access_token = json.getString("access_token");
System.out.println("access_token::"+access_token);
CacheAccessToken.getCache().setAccess_token(access_token);
String cache_token = CacheAccessToken.getCache().getAccess_token();
System.out.println("緩存值::"+cache_token);
}
}
通過上述相信大家對SpringMVC定時任務配置方法已經有所了解,大家如果對此比較感興趣,想了解更多相關知識,不妨來關注一下動力節點的SpringMVC教程,里面有更豐富的知識等著大家去學習,希望對大家能夠有所幫助哦。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習