String
String

Reputation: 3728

Java Rest Client using Multiple Threads

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

  1. It will connect to the database and it will execute select query
  2. It will connect to the Rest service URL and it will post the data

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

Answers (2)

Pavan Kumar K
Pavan Kumar K

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

Muhammad Suleman
Muhammad Suleman

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

Related Questions