SpringMvc 定时任务

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.定时任务demo

package org.rgdata.task;

import java.io.IOException;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

import org.rgdata.cache.CacheAccessToken;

import org.rgdata.util.HttpUtil;

import com.alibaba.fastjson.JSONObject;

@Component

public class AccessTokenTask {

// @Scheduled(cron = "0 */2 * * * ?")

//long time= 6600000;1小时50分钟=6600000ms

@Scheduled(fixedRate = 6600000)

	    public void getAccessToken() {

	    	System.out.println("每1小时50分钟执行一次"+ new Date());

	    	String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx413b867cd2957212&secret=c39b607e5550562a79e2674815808679";

               String result = null;

	    	try {
	    		result = HttpUtil.http(url, null);
			} catch (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);
	    }	    
}

猜你喜欢

转载自blog.csdn.net/shumuqinghua/article/details/79788693