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
.gitignore vendored
View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -26,7 +26,7 @@ weixin4j
2.API的成功调用依赖于正确的appid等数据,填写格式说明见API工程下的README.md文件. 2.API的成功调用依赖于正确的appid等数据,填写格式说明见API工程下的README.md文件.
3.如需使用netty服务,可以在相应的action中实现自己的具体业务,打包后放到`正确的目录`下解压`weixin-*-server-bin.zip`执行`sh startup.sh start`便可启动服务. 3.如需使用netty服务,可以在相应的action中实现自己的具体业务,打包后启动服务即可,具体详情可参考README.
如何获取 如何获取
------- -------

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -26,7 +26,8 @@ import com.foxinmy.weixin4j.xml.XStream;
* @since JDK 1.7 * @since JDK 1.7
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token">微信公众平台获取token说明</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 * @see com.foxinmy.weixin4j.model.Token
*/ */
public class FileTokenHolder extends AbstractTokenHolder { public class FileTokenHolder extends AbstractTokenHolder {
@ -54,9 +55,9 @@ public class FileTokenHolder extends AbstractTokenHolder {
@Override @Override
public Token getToken() throws WeixinException { public Token getToken() throws WeixinException {
String id = weixinAccount.getId(); String id = weixinAccount.getId();
if (StringUtils.isBlank(id) || StringUtils.isBlank(weixinAccount.getSecret())) { if (StringUtils.isBlank(id)
throw new IllegalArgumentException( || StringUtils.isBlank(weixinAccount.getSecret())) {
"id or secret not be null!"); throw new IllegalArgumentException("id or secret not be null!");
} }
File token_file = new File(String.format("%s/token_%s.xml", File token_file = new File(String.format("%s/token_%s.xml",
ConfigUtil.getValue("token_path"), id)); ConfigUtil.getValue("token_path"), id));
@ -67,13 +68,11 @@ public class FileTokenHolder extends AbstractTokenHolder {
if (token_file.exists()) { if (token_file.exists()) {
token = XStream.get(new FileInputStream(token_file), token = XStream.get(new FileInputStream(token_file),
Token.class); Token.class);
long expise_time = token.getTime() long expire_time = token.getTime()
+ (token.getExpiresIn() * 1000) - 3; + (token.getExpiresIn() * 1000) - 2;
if (expise_time > now_time) { if (expire_time > now_time) {
return token; return token;
} }
} else {
token_file.createNewFile();
} }
Response response = request.get(weixinAccount.getTokenUrl()); Response response = request.get(weixinAccount.getTokenUrl());
token = response.getAsObject(new TypeReference<Token>() { token = response.getAsObject(new TypeReference<Token>() {
@ -81,7 +80,7 @@ public class FileTokenHolder extends AbstractTokenHolder {
token.setTime(now_time); token.setTime(now_time);
XStream.to(token, new FileOutputStream(token_file)); XStream.to(token, new FileOutputStream(token_file));
} catch (IOException e) { } catch (IOException e) {
throw new WeixinException("-1", "IO ERROR"); throw new WeixinException(e.getMessage());
} }
return token; return token;
} }

View File

@ -1 +1,7 @@
Token的实现 包含token的实现
FileTokenHolder.java 基于文件保存的token实现
RedisTokenHolder.java 基于redis保存的token实现(推荐)
当然自己也可实现token的存取,继承`AbstractTokenHolder`实现`getToken`方法

View File

@ -1,5 +1,7 @@
package com.foxinmy.weixin4j.token; package com.foxinmy.weixin4j.token;
import java.util.Calendar;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@ -70,19 +72,19 @@ public class RedisTokenHolder extends AbstractTokenHolder {
Jedis jedis = null; Jedis jedis = null;
try { try {
jedis = jedisPool.getResource(); jedis = jedisPool.getResource();
Calendar now = Calendar.getInstance();
String key = String.format("token:%s", id); String key = String.format("token:%s", id);
String accessToken = jedis.get(key); String accessToken = jedis.get(key);
if (StringUtils.isBlank(accessToken)) { if (StringUtils.isBlank(accessToken)) {
token = request.get(weixinAccount.getTokenUrl()).getAsObject( token = request.get(weixinAccount.getTokenUrl()).getAsObject(
new TypeReference<Token>() { new TypeReference<Token>() {
}); });
jedis.setex(key, token.getExpiresIn() - 3, jedis.setex(key, token.getExpiresIn(),
token.getAccessToken()); token.getAccessToken());
token.setTime(System.currentTimeMillis()); token.setTime(now.getTimeInMillis());
} else { } else {
token = new Token(); token = new Token();
token.setAccessToken(accessToken); token.setAccessToken(accessToken);
token.setExpiresIn(jedis.ttl(key).intValue());
} }
} catch (JedisException e) { } catch (JedisException e) {
jedisPool.returnBrokenResource(jedis); jedisPool.returnBrokenResource(jedis);

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -40,9 +40,16 @@ weixin4j-mp-server
bill_path=/tmp/weixin/bill bill_path=/tmp/weixin/bill
ca_file=/tmp/weixin/xxxxx.p12 | xxxxx.pfx ca_file=/tmp/weixin/xxxxx.p12 | xxxxx.pfx
2.`mvn package`,得到一个zip的压缩包,解压到启动目录(见`src/main/startup.sh/APP_HOME`) 2.在对应的action中实现自己的具体业务 如 TextAction 则表示收到文本消息
3.启动netty服务(`com.foxinmy.weixin4j.mp.startup.WeixinMpServerBootstrap`) @Override
public ResponseMessage execute(TextMessage inMessage) {
return new ResponseMessage(new Text("Hello World!"), inMessage);
}
3.`mvn package`,得到一个zip的压缩包,解压到启动目录(见`src/main/startup.sh/APP_HOME`)
4.启动netty服务(`com.foxinmy.weixin4j.mp.startup.WeixinMpServerBootstrap`)
sh startup.sh start sh startup.sh start

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -17,6 +17,10 @@ hs_err_pid*
/.classpath /.classpath
/.tomcatplugin /.tomcatplugin
# idea ignore
/.idea
*.iml
# maven ignore # maven ignore
target/* target/*

View File

@ -30,9 +30,16 @@ weixin4j-qy-server
token_path=/tmp/weixin/token token_path=/tmp/weixin/token
media_path=/tmp/weixin/media media_path=/tmp/weixin/media
2.`mvn package`,得到一个zip的压缩包,解压到启动目录(见`src/main/startup.sh/APP_HOME`) 2.在对应的action中实现自己的具体业务 如 TextAction 则表示收到文本消息
3.启动netty服务(`com.foxinmy.weixin4j.mp.startup.WeixinQyServerBootstrap`) @Override
public ResponseMessage execute(TextMessage inMessage) {
return new ResponseMessage(new Text("Hello World!"), inMessage);
}
3.`mvn package`,得到一个zip的压缩包,解压到启动目录(见`src/main/startup.sh/APP_HOME`)
4.启动netty服务(`com.foxinmy.weixin4j.mp.startup.WeixinQyServerBootstrap`)
sh startup.sh start sh startup.sh start