Skip to content

Commit 7d63739

Browse files
Copilotbinarywang
andcommitted
修复代码审查反馈:完善SDK自动销毁逻辑和测试代码
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent 6e3039c commit 7d63739

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/WxCpConfigStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public interface WxCpConfigStorage {
304304

305305
/**
306306
* 减少会话存档SDK的引用计数
307-
* 当引用计数降为0时,自动销毁SDK
307+
* 当引用计数降为0时,自动销毁SDK以释放资源
308308
*
309309
* @param sdk sdk id
310310
* @return 减少后的引用计数,如果返回0表示SDK已被销毁

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpDefaultConfigImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.config.impl;
22

3+
import com.tencent.wework.Finance;
34
import me.chanjar.weixin.common.bean.WxAccessToken;
45
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
56
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -497,7 +498,14 @@ public synchronized int incrementMsgAuditSdkRefCount(long sdk) {
497498
@Override
498499
public synchronized int decrementMsgAuditSdkRefCount(long sdk) {
499500
if (this.msgAuditSdk == sdk && this.msgAuditSdkRefCount > 0) {
500-
return --this.msgAuditSdkRefCount;
501+
int newCount = --this.msgAuditSdkRefCount;
502+
// 当引用计数降为0时,自动销毁SDK以释放资源
503+
if (newCount == 0) {
504+
Finance.DestroySdk(sdk);
505+
this.msgAuditSdk = 0;
506+
this.msgAuditSdkExpiresTime = 0;
507+
}
508+
return newCount;
501509
}
502510
return 0;
503511
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisConfigImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.config.impl;
22

3+
import com.tencent.wework.Finance;
34
import me.chanjar.weixin.common.bean.WxAccessToken;
45
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
56
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -515,7 +516,14 @@ public synchronized int incrementMsgAuditSdkRefCount(long sdk) {
515516
@Override
516517
public synchronized int decrementMsgAuditSdkRefCount(long sdk) {
517518
if (this.msgAuditSdk == sdk && this.msgAuditSdkRefCount > 0) {
518-
return --this.msgAuditSdkRefCount;
519+
int newCount = --this.msgAuditSdkRefCount;
520+
// 当引用计数降为0时,自动销毁SDK以释放资源
521+
if (newCount == 0) {
522+
Finance.DestroySdk(sdk);
523+
this.msgAuditSdk = 0;
524+
this.msgAuditSdkExpiresTime = 0;
525+
}
526+
return newCount;
519527
}
520528
return 0;
521529
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,15 +803,23 @@ public void testNewSafeApi() throws Exception {
803803
suffix = "." + decryptData.getFile().getFileExt();
804804
sdkFileId = decryptData.getFile().getSdkFileId();
805805
break;
806+
default:
807+
// 未知消息类型,跳过处理
808+
continue;
806809
}
807810

808811
// 测试新的downloadMediaFile方法 - 不需要传入SDK
809812
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
810813
String targetPath = path + "testfile-new/" + md5Sum + suffix;
811814
File file = new File(targetPath);
815+
816+
// 确保父目录存在
812817
if (!file.getParentFile().exists()) {
813818
file.getParentFile().mkdirs();
814-
} else {
819+
}
820+
821+
// 删除已存在的文件
822+
if (file.exists()) {
815823
file.delete();
816824
}
817825

0 commit comments

Comments
 (0)