|
/

使用Java调用OpenAPI 时,ApiExecutor不能是单例吗?

Yy****************55
发布于 1 年前
1204来自安徽省
移动统计 U-App
SDK集成与升级
API调用

import com.alibaba.ocean.rawsdk.ApiExecutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UmengConfig {

@Value("${umeng.openApi.appKey}")
private String appKey;

@Value("${umeng.openApi.appSecret}")
private String appSecret;

@Value("${umeng.openApi.serverHost}")
private String serverHost;

@Bean(name = "umengApiExecutor")
public ApiExecutor apiExecutor() {
// 请替换apiKey和apiSecurity
ApiExecutor apiExecutor = new ApiExecutor(appKey, appSecret);
apiExecutor.setServerHost(serverHost);
return apiExecutor;
}

}



private List<UmengUappAppInfoData> remoteListApps(String traceId) {
UmengUappGetAppListParam param = new UmengUappGetAppListParam();
// 每页10条数据
param.setPerPage(10);
// 设置AccessToken(如果需要)
param.setAccessToken("");
// 存储所有页的数据
List<UmengUappAppInfoData> allApps = new ArrayList<>();
int currentPage = 1;
// 初始化为1,第一次请求后更新
int totalPage = 1;
try {
do {
// 设置当前页码
param.setPage(currentPage);
UmengUappGetAppListResult result = umengApiExecutor.execute(param);
// 第一次请求时获取总页数
if (currentPage == 1) {
totalPage = result.getTotalPage();
}
// 如果当前页有数据,则添加到总列表
if (result.getAppInfos() != null && result.getAppInfos().length > 0) {
allApps.addAll(Arrays.asList(result.getAppInfos()));
}
// 查询下一页
currentPage++;
} while (currentPage <= totalPage); // 循环直到所有页都查询完毕

return allApps;
} catch (OceanException e) {
LOGGER.error("[{}]-[UMengServiceImpl]-[remoteListApps] errorCode={}, errorMessage={}", traceId, e.getErrorCode(), e.getErrorMessage());
return new ArrayList<>();
}
}

我使用如上方式调用OpenAPI ,每天早上9点定时任务执行调用, 报错信息如下所示,难道OpenAPI不支持单例模式,亦或者需要定期刷新Token?


errorCode=gw.SignatureMissing,errorMessage=Requestneedsignature
1个回答
 
1 年前
来自浙江省

亲,您好,关于使用Java调用OpenAPI时,`ApiExecutor`是否可以是单例的问题,目前材料中没有明确说明。但根据一般的开发实践,`ApiExecutor`可以设计为单例模式,前提是需要确保其线程安全性。

如果`ApiExecutor`在多线程环境下被共享使用,建议检查其实现是否对共享资源进行了适当的同步处理,以避免潜在的并发问题。

具体实现细节和最佳实践,请参考SDK使用说明或相关文档。

感谢您的支持与厚爱!

Loading ...
登录社区,分享你的经验