Skip to content

Commit dc821ff

Browse files
authored
Merge pull request #10 from Wechat-Group/develop
同步
2 parents 95d8723 + 4758440 commit dc821ff

File tree

24 files changed

+956
-182
lines changed

24 files changed

+956
-182
lines changed

.travis.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
language: java
2-
jdk:
3-
- oraclejdk7
4-
5-
script: "mvn clean package -Dmaven.test.skip=true"
2+
sudo: false
3+
install: true
4+
addons:
5+
sonarqube:
6+
token:
7+
secure: "834110c7191f97ecb226970c46dcaff8e681da5a"
68

9+
jdk:
10+
- oraclejdk8
11+
#script: "mvn clean package -Dmaven.test.skip=true"
12+
13+
script:
14+
- mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar
15+
716
branches:
817
only:
918
- develop
19+
20+
cache:
21+
directories:
22+
- '$HOME/.m2/repository'
23+
- '$HOME/.sonar/cache'
1024

1125
notifications:
1226
email:

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Weixin Java Tools 微信公众号/企业号开发Java SDK
22
=====================================
33
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.binarywang/weixin-java-parent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.binarywang/weixin-java-parent)
4-
[![Build Status](https://travis-ci.org/wechat-group/weixin-java-tools.svg?branch=develop)](https://travis-ci.org/wechat-group/weixin-java-tools)
4+
[![Build Status](https://travis-ci.org/Wechat-Group/weixin-java-tools.svg?branch=develop)](https://travis-ci.org/Wechat-Group/weixin-java-tools)
55

66
### 注意:
77
1. ***本项目Fork自chanjarster/weixin-java-tools,但由于原项目已停止维护,故单独维护和发布,且发布到maven上的groupId也会不同,详细信息见下文。***
@@ -13,7 +13,7 @@ Weixin Java Tools 微信公众号/企业号开发Java SDK
1313

1414
## 开发交流方式及注意事项:
1515
1. QQ群:343954419(推荐点击按钮入群: [![Join QQ Group](http://pub.idqqimg.com/wpa/images/group.png)](http://shang.qq.com/wpa/qunwpa?idkey=731dc3e7ea31ebe25376cc1a791445468612c63fd0e9e05399b088ec81fd9e15)[![Join QQ Group](http://pub.idqqimg.com/wpa/images/group.png)](http://jq.qq.com/?_wv=1027&k=40lRskK),如果无反应,可以自行搜索群号进行添加 )
16-
1. 由于群容量有限即将爆满,现开启付费入群模式,并不定期清理长时间不活跃人士
16+
1. 由于群容量有限,即将爆满,故开启付费入群模式以保证只有真实交流需求的人进入,并为保证群的活跃度,将不定期清理长时间不活跃的同学
1717
1. 微信群: 因微信群已达到100人限制,故如有想加入微信群的,请入QQ群后联系管理员,提供微信号以便邀请加入;
1818
1. 新手提问前,请先阅读此文章:http://t.cn/RV93MRB
1919
1. 寻求帮助时需贴代码或大长串异常信息的,请利用http://paste.ubuntu.com
@@ -25,12 +25,12 @@ Weixin Java Tools 微信公众号/企业号开发Java SDK
2525
===========
2626

2727
## 版本说明
28-
1. 本项目定为每月发布一次正式版,版本号格式为X.X.0(如2.1.0,2.2.0等),月初或月底发布新版本,遇到重大问题需修复会及时提交新版本,欢迎大家随时提交Pull Request;
28+
1. 本项目定为每两个月发布一次正式版,版本号格式为X.X.0(如2.1.0,2.2.0等),月底发布新版本,遇到重大问题需修复会及时提交新版本,欢迎大家随时提交Pull Request;
2929
1. BUG修复和新特性一般会先发布成小版本作为临时版本(如2.0.1,2.0.2等,即尾号不为0,以区别于正式版);
3030
1. 目前最新版本号为 [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.binarywang/weixin-java-parent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.binarywang/weixin-java-parent) ,也可以通过访问链接 [【公众号】](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.github.binarywang%22%20AND%20a%3A%22weixin-java-mp%22)[【企业号】](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.github.binarywang%22%20AND%20a%3A%22weixin-java-cp%22)
3131
分别查看所有最新的版本。
3232

33-
## Maven & Gradle
33+
## Maven & Gradle 最新正式版本
3434

3535
* 公众号(订阅号、服务号):
3636

weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/result/WxError.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public class WxError implements Serializable {
2020
private String json;
2121

2222
public static WxError fromJson(String json) {
23-
WxError error = WxGsonBuilder.create().fromJson(json, WxError.class);
24-
return error;
23+
return WxGsonBuilder.create().fromJson(json, WxError.class);
2524
}
2625

2726
public static Builder newBuilder() {

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public class StandardSession implements WxSession, InternalSession {
1111
/**
1212
* The string manager for this package.
1313
*/
14-
protected static final StringManager sm =
15-
StringManager.getManager(Constants.Package);
14+
protected static final StringManager sm = StringManager.getManager(Constants.Package);
1615
/**
1716
* Type array.
1817
*/
19-
protected static final String EMPTY_ARRAY[] = new String[0];
18+
private static final String[] EMPTY_ARRAY = new String[0];
19+
2020
// ------------------------------ WxSession
2121
protected Map<String, Object> attributes = new ConcurrentHashMap<>();
2222
/**
@@ -71,20 +71,23 @@ public StandardSession(InternalSessionManager manager) {
7171
@Override
7272
public Object getAttribute(String name) {
7373

74-
if (!isValidInternal())
74+
if (!isValidInternal()) {
7575
throw new IllegalStateException
76-
(sm.getString("sessionImpl.getAttribute.ise"));
76+
(sm.getString("sessionImpl.getAttribute.ise"));
77+
}
7778

78-
if (name == null) return null;
79+
if (name == null) {
80+
return null;
81+
}
7982

80-
return (this.attributes.get(name));
83+
return this.attributes.get(name);
8184
}
8285

8386
@Override
8487
public Enumeration<String> getAttributeNames() {
85-
if (!isValidInternal())
86-
throw new IllegalStateException
87-
(sm.getString("sessionImpl.getAttributeNames.ise"));
88+
if (!isValidInternal()) {
89+
throw new IllegalStateException(sm.getString("sessionImpl.getAttributeNames.ise"));
90+
}
8891

8992
Set<String> names = new HashSet<>();
9093
names.addAll(this.attributes.keySet());
@@ -94,9 +97,9 @@ public Enumeration<String> getAttributeNames() {
9497
@Override
9598
public void setAttribute(String name, Object value) {
9699
// Name cannot be null
97-
if (name == null)
98-
throw new IllegalArgumentException
99-
(sm.getString("sessionImpl.setAttribute.namenull"));
100+
if (name == null) {
101+
throw new IllegalArgumentException(sm.getString("sessionImpl.setAttribute.namenull"));
102+
}
100103

101104
// Null value is the same as removeAttribute()
102105
if (value == null) {
@@ -105,9 +108,9 @@ public void setAttribute(String name, Object value) {
105108
}
106109

107110
// Validate our current state
108-
if (!isValidInternal())
109-
throw new IllegalStateException(sm.getString(
110-
"sessionImpl.setAttribute.ise", getIdInternal()));
111+
if (!isValidInternal()) {
112+
throw new IllegalStateException(sm.getString("sessionImpl.setAttribute.ise", getIdInternal()));
113+
}
111114

112115
this.attributes.put(name, value);
113116

@@ -121,8 +124,7 @@ public void removeAttribute(String name) {
121124
@Override
122125
public void invalidate() {
123126
if (!isValidInternal())
124-
throw new IllegalStateException
125-
(sm.getString("sessionImpl.invalidate.ise"));
127+
throw new IllegalStateException(sm.getString("sessionImpl.invalidate.ise"));
126128

127129
// Cause this session to expire
128130
expire();
@@ -131,12 +133,11 @@ public void invalidate() {
131133

132134
@Override
133135
public WxSession getSession() {
134-
135136
if (this.facade == null) {
136137
this.facade = new StandardSessionFacade(this);
137138
}
138-
return (this.facade);
139139

140+
return this.facade;
140141
}
141142

142143
/**
@@ -185,12 +186,14 @@ public void setValid(boolean isValid) {
185186

186187
@Override
187188
public String getIdInternal() {
188-
return (this.id);
189+
return this.id;
189190
}
190191

191192
protected void removeAttributeInternal(String name) {
192193
// Avoid NPE
193-
if (name == null) return;
194+
if (name == null) {
195+
return;
196+
}
194197

195198
// Remove this attribute from our collection
196199
this.attributes.remove(name);
@@ -202,19 +205,22 @@ public void expire() {
202205
// Check to see if session has already been invalidated.
203206
// Do not check expiring at this point as expire should not return until
204207
// isValid is false
205-
if (!this.isValid)
208+
if (!this.isValid) {
206209
return;
210+
}
207211

208212
synchronized (this) {
209213
// Check again, now we are inside the sync so this code only runs once
210214
// Double check locking - isValid needs to be volatile
211215
// The check of expiring is to ensure that an infinite loop is not
212216
// entered as per bug 56339
213-
if (this.expiring || !this.isValid)
217+
if (this.expiring || !this.isValid) {
214218
return;
219+
}
215220

216-
if (this.manager == null)
221+
if (this.manager == null) {
217222
return;
223+
}
218224

219225
// Mark this session as "being expired"
220226
this.expiring = true;
@@ -230,9 +236,9 @@ public void expire() {
230236
this.expiring = false;
231237

232238
// Unbind any objects associated with this session
233-
String keys[] = keys();
234-
for (int i = 0; i < keys.length; i++) {
235-
removeAttributeInternal(keys[i]);
239+
String[] keys = keys();
240+
for (String key : keys) {
241+
removeAttributeInternal(key);
236242
}
237243
}
238244

@@ -273,13 +279,15 @@ public void setMaxInactiveInterval(int interval) {
273279

274280
@Override
275281
public void setId(String id) {
276-
if ((this.id != null) && (this.manager != null))
282+
if ((this.id != null) && (this.manager != null)) {
277283
this.manager.remove(this);
284+
}
278285

279286
this.id = id;
280287

281-
if (this.manager != null)
288+
if (this.manager != null) {
282289
this.manager.add(this);
290+
}
283291
}
284292

285293
/**
@@ -295,21 +303,41 @@ protected String[] keys() {
295303

296304
@Override
297305
public boolean equals(Object o) {
298-
if (this == o) return true;
299-
if (!(o instanceof StandardSession)) return false;
306+
if (this == o) {
307+
return true;
308+
}
309+
if (!(o instanceof StandardSession)) {
310+
return false;
311+
}
300312

301313
StandardSession session = (StandardSession) o;
302314

303-
if (this.creationTime != session.creationTime) return false;
304-
if (this.expiring != session.expiring) return false;
305-
if (this.isValid != session.isValid) return false;
306-
if (this.maxInactiveInterval != session.maxInactiveInterval) return false;
307-
if (this.thisAccessedTime != session.thisAccessedTime) return false;
308-
if (!this.accessCount.equals(session.accessCount)) return false;
309-
if (!this.attributes.equals(session.attributes)) return false;
310-
if (!this.facade.equals(session.facade)) return false;
311-
if (!this.id.equals(session.id)) return false;
312-
return this.manager.equals(session.manager);
315+
if (this.creationTime != session.creationTime) {
316+
return false;
317+
}
318+
if (this.expiring != session.expiring) {
319+
return false;
320+
}
321+
if (this.isValid != session.isValid) {
322+
return false;
323+
}
324+
if (this.maxInactiveInterval != session.maxInactiveInterval) {
325+
return false;
326+
}
327+
if (this.thisAccessedTime != session.thisAccessedTime) {
328+
return false;
329+
}
330+
if (this.accessCount.get() != session.accessCount.get()) {
331+
return false;
332+
}
333+
if (!this.attributes.equals(session.attributes)) {
334+
return false;
335+
}
336+
if (!this.facade.equals(session.facade)) {
337+
return false;
338+
}
339+
340+
return this.id.equals(session.id) && this.manager.equals(session.manager);
313341

314342
}
315343

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,17 +538,24 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
538538
int retryTimes = 0;
539539
do {
540540
try {
541-
return executeInternal(executor, uri, data);
541+
T result = this.executeInternal(executor, uri, data);
542+
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}",uri, data, result);
543+
return result;
542544
} catch (WxErrorException e) {
545+
if (retryTimes + 1 > this.maxRetryTimes) {
546+
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
547+
//最后一次重试失败后,直接抛出异常,不再等待
548+
throw new RuntimeException("微信服务端异常,超出重试次数");
549+
}
550+
543551
WxError error = e.getError();
544552
/*
545553
* -1 系统繁忙, 1000ms后重试
546554
*/
547555
if (error.getErrorCode() == -1) {
548556
int sleepMillis = this.retrySleepMillis * (1 << retryTimes);
549557
try {
550-
this.log.debug("微信系统繁忙,{}ms 后重试(第{}次)", sleepMillis,
551-
retryTimes + 1);
558+
this.log.debug("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1);
552559
Thread.sleep(sleepMillis);
553560
} catch (InterruptedException e1) {
554561
throw new RuntimeException(e1);
@@ -557,8 +564,9 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
557564
throw e;
558565
}
559566
}
560-
} while (++retryTimes < this.maxRetryTimes);
567+
} while (retryTimes++ < this.maxRetryTimes);
561568

569+
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
562570
throw new RuntimeException("微信服务端异常,超出重试次数");
563571
}
564572

@@ -572,8 +580,7 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
572580
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
573581

574582
try {
575-
return executor.execute(getHttpclient(), this.httpProxy,
576-
uriWithAccessToken, data);
583+
return executor.execute(getHttpclient(), this.httpProxy, uriWithAccessToken, data);
577584
} catch (WxErrorException e) {
578585
WxError error = e.getError();
579586
/*
@@ -586,11 +593,14 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
586593
this.configStorage.expireAccessToken();
587594
return execute(executor, uri, data);
588595
}
596+
589597
if (error.getErrorCode() != 0) {
598+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, error);
590599
throw new WxErrorException(error);
591600
}
592601
return null;
593602
} catch (IOException e) {
603+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage());
594604
throw new RuntimeException(e);
595605
}
596606
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBusyRetryTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import me.chanjar.weixin.common.bean.result.WxError;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.common.util.http.RequestExecutor;
6+
import org.testng.annotations.DataProvider;
7+
import org.testng.annotations.Test;
8+
39
import java.util.concurrent.ExecutionException;
410
import java.util.concurrent.ExecutorService;
511
import java.util.concurrent.Executors;
612
import java.util.concurrent.Future;
713

8-
import org.testng.annotations.DataProvider;
9-
import org.testng.annotations.Test;
10-
11-
import me.chanjar.weixin.common.bean.result.WxError;
12-
import me.chanjar.weixin.common.exception.WxErrorException;
13-
import me.chanjar.weixin.common.util.http.RequestExecutor;
14-
1514
@Test
1615
public class WxCpBusyRetryTest {
1716

@@ -23,6 +22,7 @@ public Object[][] getService() {
2322
protected synchronized <T, E> T executeInternal(
2423
RequestExecutor<T, E> executor, String uri, E data)
2524
throws WxErrorException {
25+
this.log.info("Executed");
2626
WxError error = new WxError();
2727
error.setErrorCode(-1);
2828
throw new WxErrorException(error);

0 commit comments

Comments
 (0)