Reputation: 6905
I've been experimenting with both Spring's RestTemplate
and Java's URLConnection
to make REST client calls to an external API. I'm dismayed that looping 100 times doing the same REST GET call (which returns a small JSON response) takes about 60 seconds with RestTemplate
and about 53 seconds with URLConnection
. I'm testing this because my web app needs to make quite a few calls to this external API via HTTP GET, PUT & POST so I'm trying to write these calls to be as efficient as possible.
Is there something I can do to improve the efficiency of either of the code snippets below? Or is there another library that is considered highly efficient as a REST client? I would like to use RestTemplate
but I'm also OK with going more low-level via URLConnection
if it will mean faster client-side REST calls.
Maybe it's a faulty assumption of mine but I figured that making 100 HTTP GET calls to an endpoint that returns a small JSON response would take no more than 10 seconds.
Here are the methods I've been experimenting with (credsProvider
, requestFactory
& restTemplate
are initialized outside of the method with the thought that "new'ing" these repeatedly might cause performance issues)
public static String performGetRestRequest(String host, int port, String user, String pass, String endPoint) throws Exception {
credsProvider.setCredentials(
new AuthScope(host, port),
new UsernamePasswordCredentials(user, pass));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
requestFactory.setHttpClient(httpclient);
return restTemplate.getForObject("http://" + host + ":" + port + endPoint, String.class);
}
public static String performGetRestRequestWithPureJava(String host, int port, String endPoint) throws Exception {
URLConnection connection = new URL("http://" + host + ":" + port + endPoint).openConnection();
connection.setRequestProperty("Accept-Charset", CHARSET);
connection.setRequestProperty("Accept", "application/json");
String inputLine;
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), CHARSET));
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
in.close();
return sb.toString();
}
Upvotes: 2
Views: 10473
Reputation: 86
Use below library . it is much faster and easier to use
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
</dependency>.
Upvotes: 0
Reputation: 6853
Before you start optimizing your client, you should make sure that the time is actually spent there. First you need to measure how much time the server takes to come up with the answer to a request. And what are your network roundtrip times? This will give you a more complete picture of where the time goes.
I cannot comment on Spring's REST template as I have never used it. But reading from the stream returned by URLConnection::getInputStream()
will block until the server side actually sends data. So measuring "client time" for a call to performGetRestRequestWithPureJava(..)
includes the network roundtrip as well as the time the server takes to come up with the response to the request.
A side note: If you make repeated requests to the same destination, do not repeatedly create new URLConnection instances and call openConnection()
. Instead re-use a single instance with the connection already open.
Upvotes: 1