首页 课程 师资 教程 报名

SpringMVC定时任务配置

  • 2022-03-25 09:37:34
  • 1907次 星辉

1.配置spring-service.xml

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"/>

2.配置示例:

3.定时任务演示

包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教程,里面有更丰富的知识等着大家去学习,希望对大家能够有所帮助哦。

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交