一段客户端往服务端传送数据的代码,谁来帮我解读下,感激不尽 private static void Post(ServiceClient client, String url,
JSONObject jobj, boolean allowRetry) {
Request request = new Request(Method.POST, client.getDefaultRequestRoot()
+ url);
request.setEntity(new JsonRepresentation(jobj));
long start = System.currentTimeMillis();
try {
Response response = null;
if (apps != null) {
request.setResourceRef(url);
response = new Response(request);
for (Application app : apps) {
Router router = (Router) app.getInboundRoot();
Restlet restlet = router.getNext(request, response);
if (restlet != null) {
response.setStatus(Status.SUCCESS_OK);
restlet.handle(request, response);
break;
}
}
} else {
response = client.handle(request);
}
long end = System.currentTimeMillis();
if (g_Log.isInfoEnabled() && (end - start) > 500) {
g_Log.info("POST " + url + " in " + (end - start) + "ms");
}
Status st = response.getStatus();
switch (st.getCode()) {
case 404: {
g_Log.error("POST " + url + " - code : 404");
return;
}
case 200:
case 204:
removeCache(client.getName(), url);
return;
case 1002: {
if (allowRetry) {
try {
g_Log.info("链接丢失,重试一次...");
Thread.sleep(300);
client.release();
// 再试一次,如果还不能连上,就算了。
Post(client, url, jobj, !allowRetry);
} catch (InterruptedException e) {
}
}
}
default:
throw new RuntimeException("code : " + st.getCode(), st.getThrowable());
}
} finally {
client.release();
}
}
javaurlapplicationnullstring
JSONObject jobj, boolean allowRetry) {
Request request = new Request(Method.POST, client.getDefaultRequestRoot()
+ url);
request.setEntity(new JsonRepresentation(jobj));
long start = System.currentTimeMillis();
try {
Response response = null;
if (apps != null) {
request.setResourceRef(url);
response = new Response(request);
for (Application app : apps) {
Router router = (Router) app.getInboundRoot();
Restlet restlet = router.getNext(request, response);
if (restlet != null) {
response.setStatus(Status.SUCCESS_OK);
restlet.handle(request, response);
break;
}
}
} else {
response = client.handle(request);
}
long end = System.currentTimeMillis();
if (g_Log.isInfoEnabled() && (end - start) > 500) {
g_Log.info("POST " + url + " in " + (end - start) + "ms");
}
Status st = response.getStatus();
switch (st.getCode()) {
case 404: {
g_Log.error("POST " + url + " - code : 404");
return;
}
case 200:
case 204:
removeCache(client.getName(), url);
return;
case 1002: {
if (allowRetry) {
try {
g_Log.info("链接丢失,重试一次...");
Thread.sleep(300);
client.release();
// 再试一次,如果还不能连上,就算了。
Post(client, url, jobj, !allowRetry);
} catch (InterruptedException e) {
}
}
}
default:
throw new RuntimeException("code : " + st.getCode(), st.getThrowable());
}
} finally {
client.release();
}
}
javaurlapplicationnullstring
解决方案 »
- hibernate 无法自动创建表hibernate用的ant+xdoclet自动生成配置文件,生成了,可是起数据库无法自动建表。
- SSH写一个消息的后台管理空指针问题
- hibernate到底有多少实用价值???
- 一个spring+ibatis集成后的手动事务问题
- [Struts]异常:NullPointerException,JAVA方向的东西,怎么这么麻烦
- 父页面刷新问题(struts)
- java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]将截断字符串或二进制数据。
- 解释XML的问题
- 是用ejb还是XML
- Springmvc整合Jackson服务器报错
- 请教 我的程序为什么从控制台读取而不停止 ?IO
- 请大家推荐一个压力测试工具
ServiceClient client, String url,JSONObject jobj, boolean allowRetry
这几个参数都知道吧?(最后一个我也不知道,继续往下看)
new JsonRepresentation(jobj))我猜这个类应该是转换json串的,应该是对象转换成json串的形式吧、
最后根据应用的数量挨个的发送一个请求是么?
没仔细看,如果说错了请见谅。
JSONObject jobj, boolean allowRetry) 使用client 给url 发送 请。并根据url(该地址的服务器)返回的状态码输出日志记录ServiceClient 这个类你百度一个吧。