0

I am trying to connect to a website ABC.com ( HTTP API endpoint). However , due to some problem in the website's end I am not getting a response.

  val httpclient: CloseableHttpClient = HttpClients.custom()
  .setSSLSocketFactory(sslsf)
  .build()
  val getSlowQueryRequest: HttpGet = new HttpGet(mySQLHost + "cgi-bin/" + slowQueryLink)
  val slowQueryResponse: CloseableHttpResponse = httpclient.execute(getSlowQueryRequest)

The program gets stuck after httpclient.execute() . I tried manually opening the website and even then it was not giving any response. Because of this , the rest of the program gets hung up! Is there some way I can set up some timer to wait for sometime and in case I do not get a response , move on after skipping the test?

EDIT:

Also could it be because of using SSL? Does CloseableHttpClient not provide any default timeout value?

Thanks in advance!

joanOfArc
  • 467
  • 1
  • 6
  • 16

2 Answers2

1

You can use HttpAsyncClient for your request

https://hc.apache.org/httpcomponents-asyncclient-dev/quickstart.html

So your call will be:

 val futureResponse: Future<HttpResponse> = httpclient.execute(request1, null);

And you can make further processing using onComplete for a Future.

Another option is to use some Scala native library, like http://dispatch.databinder.net/Bargaining+with+futures.html

import dispatch._, Defaults._
val svc = url("http://api.hostip.info/country.php")
val country = Http(svc OK as.String)
val length = for (c <- country) yield c.length

The length value is a future of integer.

Basically, Scala suggests nonblocking processing, as soon as you start waiting here and there , you loose all the benefits.

Here is some similar question, which can give you more clues

Simple and concise HTTP client library for Scala

Community
  • 1
  • 1
mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • Isn't there a more direct method which CloseableHttpClient provides to set a timeout without involving Future as I am not very familiar with the concept ? – joanOfArc Nov 17 '14 at 07:03
  • I think, there is a default timeout in CloseableHttpRequest, but you saw what it does to your system. You will have smaller outages, woth smaller timeouts, but problem will remain. – mavarazy Nov 17 '14 at 07:34
0
val requestConfig: RequestConfig = RequestConfig.custom()
      .setSocketTimeout(30 * 1000) //30 sec timeout
      .setConnectTimeout(30 * 1000)
      .build()
    val sslsf: SSLConnectionSocketFactory = Utility.getTrustAllSSLFactory
    val httpclient: CloseableHttpClient = HttpClients.custom()
      .setDefaultRequestConfig(requestConfig)
      .setSSLSocketFactory(sslsf)
      .build()

This works fine for me!

joanOfArc
  • 467
  • 1
  • 6
  • 16