RESTLET response time getting too slow when deployed in server

261 views Asked by At

I created a Restlet-based web service which uses POST/Json.

Problem is that when testing it on local machine,

it works perfectly well. But when it is deployed on a server, it

takes about 10 seconds more than when tested on my local development

machine to get response even though there is no other processes using

server's resources.

1

There are 1 answers

0
Calia Kim On

I solved the problem by using Apache Http Client instead of

ClientResource from RESTlet.

I tested both ClientResource and Apache HTTP for the same RESTlet-based web service on remote Linux server. The former took about 10 seconds and the latter less than 1 second. And I don't know why.

public class Test 
{
    public static void main(String [] args)
    {
 
        HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead 

        try {
            HttpPost request = new HttpPost("http://hostname:port/testService");
            StringEntity params =new StringEntity("{\"key\" : \"value\"}");
            request.addHeader("content-type", "application/json");
            request.addHeader("Accept", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            String json_string = EntityUtils.toString(response.getEntity());
            
            System.out.println(json_string);
        }catch (Exception ex) {
            // handle exception here
        } finally {
            httpClient.getConnectionManager().shutdown(); //Deprecated
        }
    }
}