update md

This commit is contained in:
jy.hu
2014-12-10 16:29:18 +08:00
parent 01e68ec835
commit 0353554718
14 changed files with 72 additions and 19 deletions
+4
View File
@@ -17,6 +17,10 @@ hs_err_pid*
/.classpath
/.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore
target/*
@@ -26,7 +26,8 @@ import com.foxinmy.weixin4j.xml.XStream;
* @since JDK 1.7
* @see <a
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">微信公众平台获取token说明</a>
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号获取token说明</a>
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号获取token说明</a>
* @see com.foxinmy.weixin4j.model.Token
*/
public class FileTokenHolder extends AbstractTokenHolder {
@@ -54,9 +55,9 @@ public class FileTokenHolder extends AbstractTokenHolder {
@Override
public Token getToken() throws WeixinException {
String id = weixinAccount.getId();
if (StringUtils.isBlank(id) || StringUtils.isBlank(weixinAccount.getSecret())) {
throw new IllegalArgumentException(
"id or secret not be null!");
if (StringUtils.isBlank(id)
|| StringUtils.isBlank(weixinAccount.getSecret())) {
throw new IllegalArgumentException("id or secret not be null!");
}
File token_file = new File(String.format("%s/token_%s.xml",
ConfigUtil.getValue("token_path"), id));
@@ -67,13 +68,11 @@ public class FileTokenHolder extends AbstractTokenHolder {
if (token_file.exists()) {
token = XStream.get(new FileInputStream(token_file),
Token.class);
long expise_time = token.getTime()
+ (token.getExpiresIn() * 1000) - 3;
if (expise_time > now_time) {
long expire_time = token.getTime()
+ (token.getExpiresIn() * 1000) - 2;
if (expire_time > now_time) {
return token;
}
} else {
token_file.createNewFile();
}
Response response = request.get(weixinAccount.getTokenUrl());
token = response.getAsObject(new TypeReference<Token>() {
@@ -81,7 +80,7 @@ public class FileTokenHolder extends AbstractTokenHolder {
token.setTime(now_time);
XStream.to(token, new FileOutputStream(token_file));
} catch (IOException e) {
throw new WeixinException("-1", "IO ERROR");
throw new WeixinException(e.getMessage());
}
return token;
}
@@ -1 +1,7 @@
Token的实现
包含token的实现
FileTokenHolder.java 基于文件保存的token实现
RedisTokenHolder.java 基于redis保存的token实现(推荐)
当然自己也可实现token的存取,继承`AbstractTokenHolder`实现`getToken`方法
@@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.token;
import java.util.Calendar;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
@@ -70,19 +72,19 @@ public class RedisTokenHolder extends AbstractTokenHolder {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Calendar now = Calendar.getInstance();
String key = String.format("token:%s", id);
String accessToken = jedis.get(key);
if (StringUtils.isBlank(accessToken)) {
token = request.get(weixinAccount.getTokenUrl()).getAsObject(
new TypeReference<Token>() {
});
jedis.setex(key, token.getExpiresIn() - 3,
jedis.setex(key, token.getExpiresIn(),
token.getAccessToken());
token.setTime(System.currentTimeMillis());
token.setTime(now.getTimeInMillis());
} else {
token = new Token();
token.setAccessToken(accessToken);
token.setExpiresIn(jedis.ttl(key).intValue());
}
} catch (JedisException e) {
jedisPool.returnBrokenResource(jedis);