update md
This commit is contained in:
parent
305738e832
commit
5cf71734a9
@ -303,4 +303,6 @@
|
||||
|
||||
* 2015-05-30
|
||||
|
||||
**大的调整:去掉httpclient依赖**
|
||||
+ **去掉httpclient依赖**
|
||||
|
||||
+ **去掉redis依赖**
|
||||
@ -22,10 +22,5 @@
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>2.6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@ -1,79 +0,0 @@
|
||||
package com.foxinmy.weixin4j.token;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* 用REDIS保存TOKEN
|
||||
*
|
||||
* @className RedisTokenHolder
|
||||
* @author jy
|
||||
* @date 2015年1月9日
|
||||
* @since JDK 1.7
|
||||
* @see com.foxinmy.weixin4j.token.TokenCreator
|
||||
*/
|
||||
public class RedisTokenHolder implements TokenHolder {
|
||||
|
||||
private JedisPool jedisPool;
|
||||
private final TokenCreator tokenCreator;
|
||||
|
||||
public final static int MAX_TOTAL = 50;
|
||||
public final static int MAX_IDLE = 5;
|
||||
public final static int MAX_WAIT_MILLIS = 2000;
|
||||
public final static boolean TEST_ON_BORROW = false;
|
||||
public final static boolean TEST_ON_RETURN = true;
|
||||
|
||||
public RedisTokenHolder(TokenCreator tokenCreator) {
|
||||
this("localhost", 6379, tokenCreator);
|
||||
}
|
||||
|
||||
public RedisTokenHolder(String host, int port, TokenCreator tokenCreator) {
|
||||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||
jedisPoolConfig.setMaxTotal(MAX_TOTAL);
|
||||
jedisPoolConfig.setMaxIdle(MAX_IDLE);
|
||||
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS);
|
||||
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
|
||||
jedisPoolConfig.setTestOnReturn(TEST_ON_RETURN);
|
||||
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
||||
this.tokenCreator = tokenCreator;
|
||||
}
|
||||
|
||||
public RedisTokenHolder(String host, int port,
|
||||
JedisPoolConfig jedisPoolConfig, TokenCreator tokenCreator) {
|
||||
this(new JedisPool(jedisPoolConfig, host, port), tokenCreator);
|
||||
}
|
||||
|
||||
public RedisTokenHolder(JedisPool jedisPool, TokenCreator tokenCreator) {
|
||||
this.jedisPool = jedisPool;
|
||||
this.tokenCreator = tokenCreator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Token getToken() throws WeixinException {
|
||||
Token token = null;
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
String cacheKey = tokenCreator.getCacheKey();
|
||||
String accessToken = jedis.get(cacheKey);
|
||||
if (StringUtil.isBlank(accessToken)) {
|
||||
token = tokenCreator.createToken();
|
||||
jedis.setex(cacheKey, (int) token.getExpiresIn(),
|
||||
token.getAccessToken());
|
||||
} else {
|
||||
token = new Token(accessToken);
|
||||
}
|
||||
} catch (JedisException e) {
|
||||
jedisPool.returnBrokenResource(jedis);
|
||||
} finally {
|
||||
jedisPool.returnResource(jedis);
|
||||
}
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@ -91,7 +91,7 @@ weixin.properties说明
|
||||
// weixinProxy = new WeixinProxy(weixinAccount);
|
||||
weixinProxy.getUser(openId);
|
||||
|
||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用`RedisTokenHolder`.
|
||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用(RedisTokenHolder)[https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token].
|
||||
>
|
||||
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenHolder());
|
||||
|
||||
|
||||
@ -58,8 +58,8 @@ weixin.properties说明
|
||||
// weixinProxy = new WeixinProxy(weixinAccount);
|
||||
weixinProxy.getUser(userid);
|
||||
|
||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用`RedisTokenHolder`.
|
||||
>
|
||||
> 针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`,默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用(RedisTokenHolder)[https://github.com/foxinmy/weixin4j/wiki/%E7%94%A8redis%E4%BF%9D%E5%AD%98token].
|
||||
|
||||
> WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenHolder());
|
||||
|
||||
> // weixinProxy = new WeixinProxy(new RedisTokenHolder(weixinAccount));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user