更新時(shí)間:2022-03-25 09:37:34 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2312次
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
配置掃描任務(wù)
<task:annotation-driven/>
配置掃描位置
<context:component-scan base-package="org.rgdata.task"/>
包org.rgdata.task;
導(dǎo)入 java.io.IOException;
導(dǎo)入 java.util.Date;
導(dǎo)入 org.springframework.scheduling.annotation.Scheduled;
導(dǎo)入 org.springframework.stereotype.Component;
導(dǎo)入 org.rgdata.cache.CacheAccessToken;
導(dǎo)入 org.rgdata.util.HttpUtil;
導(dǎo)入 com.alibaba.fastjson.JSONObject;
@零件
公共類 AccessTokenTask {
//@Scheduled(cron = "0 */2 * * * ?")
//長(zhǎng)時(shí)間 = 6600000; 1 小時(shí) 50 分鐘 = 6600000 毫秒
@Scheduled(fixedRate = 6600000)
公共無(wú)效getAccessToken(){
System.out.println("每1小時(shí)50分鐘執(zhí)行一次" + new Date());
字符串網(wǎng)址="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx413b867cd2957212&secret=c39b607e5550562a79e2674815808679";
字符串結(jié)果 = null;
嘗試 {
結(jié)果 = 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);
}
}
通過(guò)上述相信大家對(duì)SpringMVC定時(shí)任務(wù)配置方法已經(jīng)有所了解,大家如果對(duì)此比較感興趣,想了解更多相關(guān)知識(shí),不妨來(lái)關(guān)注一下動(dòng)力節(jié)點(diǎn)的SpringMVC教程,里面有更豐富的知識(shí)等著大家去學(xué)習(xí),希望對(duì)大家能夠有所幫助哦。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)