我有一个 java Rest 客户端,它会将发布数据保存到 java Rest 服务
我已经开发了 jersey 客户端代码来将数据发布到 Rest 服务。 当我执行我的客户端应用程序时
- 它将连接到数据库并执行选择查询
- 它将连接到 Rest 服务 URL 并发布数据
我的代码:
select * from Workorder
for(int i =0;i<workorderList.size(); i++){
WorkOrder workorder=workorderLit.get(i);
WebResource webResource = client
.resource("http://localhost:8013/Workorderrest/rest/inbound/update");
ClientResponse response = webResource.accept("application/json")
.type("application/json")
.header("Authorization", "Basic " + authStringEnc)
.post(ClientResponse.class, jsonData);
}
我想在收到来自 Rest Service 的响应时将表列中的状态标志更改为已处理
任何人都可以吗。我想使用多线程发布数据 任何建议
最佳答案
我试过了,下面的一个对我有用...
ClientThread.java
package com.test;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class ClientThread extends Thread{
Client client = null;
WebResource resource = null;
public ClientThread()
{
client = Client.create();
resource = client.resource("http://localhost:9080/RestfulHelloExample/rest/hello/kumar");
}
public void run()
{
ClientResponse response = resource.type(MediaType.APPLICATION_XML).get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
System.out.println("Output from Server .... \n");
String output = response.getEntity(String.class);
System.out.println(output);
}
}
TestClient.java
public class TestClient {
public static void main(String[] args) {
ClientThread t1 = new ClientThread();
ClientThread t2 = new ClientThread();
t1.start();
t2.start();
}
}
如果您有任何问题,请告诉我。
关于使用多线程的 Java Rest 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588338/