优化并发获取微信token

2018-12-06  本文已影响0人  linjiajiam
背景:
public class WeChatUtil {

    public static String APPID;

    public static String SECRET;

    private static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

    @Value("${wechat.appid}")
    public void setAPPID(String APPID) {
        WeChatUtil.APPID = APPID;
    }

    @Value("${wechat.secret}")
    public void setSECRET(String SECRET) {
        WeChatUtil.SECRET = SECRET;
    }

    public static long EXPIRETIME = 7100;

   
    /**
     * 获取token
     * @return
     */
    public static String getWeixinToken(){
        //从redis中获取token
        String token = (String)RedisUtil.get("vbpWxToken");
        //token不存在或者已经过期,重新调用微信接口获取token
        if(StringUtils.isBlank(token)){
            try {
                String response = HttpUtil.requestGet(String.format(TOKEN_URL, APPID, SECRET), null);
                JSONObject jsonobject = JSONObject.parseObject(response);
                //如果取得token,那么就返回
                if(jsonobject.getString("access_token") != null){
                    token = jsonobject.getString("access_token");
                    //将token写入redis
                    RedisUtil.set("vbpWxToken", token, EXPIRETIME);
                }
                System.out.println(DateUtil.getCurrentFormatDate(null) +  ",获取新token:" + token);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return token;
    }
}
解决方案:
1. 模拟单例双重检查
    public static String getWeixinToken(){
        String token = (String)RedisUtil.get("vbpWxToken");
        //第一重检查
        if(StringUtils.isBlank(token)){
            //加锁
            synchronized (token){
                //第二重检查
                if(StringUtils.isBlank(token)){
                    try {
                        String response = HttpUtil.requestGet(String.format(TOKEN_URL, APPID, SECRET), null);
                        JSONObject jsonobject = JSONObject.parseObject(response);
                        //如果取得token,那么就返回
                        if(jsonobject.getString("access_token") != null){
                            token = jsonobject.getString("access_token");
                            RedisUtil.set("vbpWxToken", token, EXPIRETIME);
                        }
                        System.out.println(DateUtil.getCurrentFormatDate(null) +  ",获取新token:" + token);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return token;
    }
2. 独立定时JOB刷新token
@EnableScheduling
@Component
public class WeChatSchedule {

    private static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";

    /**
     * 刷新微信token
     * 第一次延迟1秒执行,当执行完后6500秒再执行
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 6500*1000 )
    public void setWeixinToken(){
            try {
                String response = HttpUtil.requestGet(String.format(TOKEN_URL, APPID, SECRET), null);
                JSONObject jsonobject = JSONObject.parseObject(response);
                //如果取得token,那么就写入redis
                if (jsonobject.getString("access_token") != null) {
                    String token = jsonobject.getString("access_token");
                    RedisUtil.set("vbpWxToken", token, EXPIRETIME);
                    System.out.println(DateUtil.getCurrentFormatDate("yyyy-MM-dd HH:mm:ss") + "=====获取token成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}
    public static String getWeixinToken(){
        String token = (String)RedisUtil.get("vbpWxToken");
        return token;
    }
上一篇下一篇

猜你喜欢

热点阅读