NutzCN Logo
问答 JsonRpc 里面的客户端怎么使用?想使用里面的批量调用
发布于 2826天前 作者 qq_93384aa3 1926 次浏览 复制 上一个帖子 下一个帖子
标签:

```
/**
* 客户端. 用于生成一个代理接口的实例,透明访问json-rpc服务
* @param klass 需要代理的接口
* @param endpoint jsonrpc URL入口
* @param namespace 命名空间,非json-rpc标准,扩展用,不需要就传null
* @param timeout 超时设置,若永不超时,设置为-1
* @return 代理实例
*/
public static T mapper(Class klass, final String endpoint, final String namespace, final int timeout) {
return (T)Proxy.newProxyInstance(klass.getClassLoader(), new Class<?>[]{klass}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
NutMap jreq = new NutMap();
jreq.setv(JSONRPC, Version).setv(ID, R.UU32()).setv(METHOD, method.getName());
if (!Strings.isBlank(namespace)) {
jreq.put(NAMESPACE, namespace);
}
jreq.setv(PARAMS, args);
Request req = Request.create(endpoint, org.nutz.http.Request.METHOD.POST);
req.setData(Json.toJson(jreq));
Response resp = Sender.create(req).setTimeout(timeout).send();
if (resp.isOK()) {
if (method.getReturnType() == Void.class)
return null;
NutMap re = Json.fromJson(NutMap.class, resp.getReader());
Object error = re.get(ERROR);
if (error != null) {
throw new JsonRpcException(Json.toJson(error));
}
Object result = re.get(RESULT);
return Castors.me().castTo(result, method.getReturnType());
}
throw new JsonRpcException("resp code="+resp.getStatus());
}
});
}

```
1 回复

仔细看了一下代码, JsonRpc类做的主要是 服务器端的映射客户端的Mapper

因为客户端是mapper,所以**批量**执行时没实现的.

添加回复
请先登陆
回到顶部