Reputation: 3728
I have a java Rest Client which will keep post data to java Rest Service
I have developed jersey client code to post data to Rest Service. when i execute My Client application
My code:
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);
}
I want to change the status flag in my table column to processed when I receive response from Rest Service
Could any one please.I want to post data using Multiple threads any suggestions
Upvotes: 1
Views: 6662
Reputation: 1376
I tried and below one is working for me...
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();
}
}
let me know if you have any questions.
Upvotes: 2
Reputation: 2932
after getting response
ClientResponse response = webResource.accept("application/json")
.type("application/json")
.header("Authorization", "Basic " + authStringEnc)
.post(ClientResponse.class, jsonData);
try this
new Thread(new Runnable() {
@Override
public void run() {
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}else{
// your code for updating status flag in table
}
}
}).start();
Upvotes: 1