Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DA356200BB8 for ; Sat, 12 Nov 2016 15:46:52 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D8DBD160B00; Sat, 12 Nov 2016 14:46:52 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 2D63C160AF4 for ; Sat, 12 Nov 2016 15:46:52 +0100 (CET) Received: (qmail 34062 invoked by uid 500); 12 Nov 2016 14:46:51 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 34040 invoked by uid 99); 12 Nov 2016 14:46:50 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 12 Nov 2016 14:46:50 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 6C8E1C1506 for ; Sat, 12 Nov 2016 14:46:50 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2.648 X-Spam-Level: ** X-Spam-Status: No, score=2.648 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, HTML_MESSAGE=2, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_SORBS_SPAM=0.5, SPF_PASS=-0.001] autolearn=disabled Authentication-Results: spamd4-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id Gcwdk2h8HcNN for ; Sat, 12 Nov 2016 14:46:49 +0000 (UTC) Received: from mail-qk0-f171.google.com (mail-qk0-f171.google.com [209.85.220.171]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTPS id ED4A15FCFA for ; Sat, 12 Nov 2016 14:46:48 +0000 (UTC) Received: by mail-qk0-f171.google.com with SMTP id n21so51370140qka.3 for ; Sat, 12 Nov 2016 06:46:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=nnqKq8DzohI8I81TqMfGRv6NEsbOI+SDSobR8ngGlyE=; b=ug6a1KS66elBX8iaopQZGFIOosIMaTWxPlg8jT5GpAkcknpexM3WAYtHAIB7ffprx/ aqZOq5b4Gi8VbSp4nd0qMvOkJWqKtxgmS77YwkAiag80+PdNxDaSIPEJiOhf2SqSC0x4 1yzV4vM0DmawuVm2Gx37wqdUK8FAnnSSBM+v5H3Z1VHZWR2YbQkTYSm/hAm0kjapzpno UJjT9wNQvv5BeZ6j1E7U3N76XBtHdaAM8IexQKSmtGUfzlHU6IyH4WNpKwlz0VI/zsEb WfQg+/PnypUKaFz8ThJDzm2fgAfEU6WEjei0tWY59YxaSW6Ru5yuxG0wuBC+wRxipWS7 QMzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=nnqKq8DzohI8I81TqMfGRv6NEsbOI+SDSobR8ngGlyE=; b=A5X0kJBir/bBhiH5npPqC2/lIOdnIYEM/5p+0KiC+4u1S0BzMZsh9OyS87DLL6Zjz7 fNg8DLISQBGv0tCstLVJrFppvq1/aIq8XasWfBRCZtXcb5lpwthWVV29MzUtgI7ZNDiE 2kzbQmiq+b7mmVhd7eGmnLGIg5ha0b4G4Kjn+QsPr1GNRWr/Ue3rSiqGaMQFVJBwrSW7 J0ShqRZzAKtKUsdwMqXk59sZgTWz/QT9MiN31PuBx0nhHvURKUBzwn1Zj07+Cq5egFKb 9BCLaJL75qGj46ZfwMemFy2+4yiRETldcHGYBxYwQ5sYwmGjKzVtuu2DRr0MEDiWiAe6 vBgQ== X-Gm-Message-State: ABUngveGKRaINtfmy3lG/rZA+RHNUQtgqBkhKZwLG66LzdFD1/zPeodJxFG4PyFWmkoMOTYRL8jzEFiYTYdZPw== X-Received: by 10.55.160.136 with SMTP id j130mr8126335qke.108.1478962006123; Sat, 12 Nov 2016 06:46:46 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.164.4 with HTTP; Sat, 12 Nov 2016 06:46:45 -0800 (PST) From: Nishant Kumar Date: Sat, 12 Nov 2016 20:16:45 +0530 Message-ID: Subject: scala: apache httpclient in multi-threaded environment. confusion on correct way of use To: httpclient-users@hc.apache.org Content-Type: multipart/alternative; boundary=001a114fa61602e88705411bade1 archived-at: Sat, 12 Nov 2016 14:46:53 -0000 --001a114fa61602e88705411bade1 Content-Type: text/plain; charset=UTF-8 Hi, I am writing a singleton class (Object in scala) which uses apache httpclient(4.5.2) to send some file or some json content to a server and return status to caller. Here is my class - object HttpUtils{ protected val retryHandler = new HttpRequestRetryHandler() { def retryRequest(exception: IOException, executionCount: Int, context: HttpContext): Boolean = { //retry logic true } } private val connectionManager = new PoolingHttpClientConnectionManager() // Reusing same client for each request that might be coming from different threads . // Is it correct ???? val httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setRetryHandler(retryHandler) .build() def restApiCall (url : String, rDD: RDD[SomeMessage]) : Boolean = { // Creating new context for each request val httpContext: HttpClientContext = HttpClientContext.create val post = new HttpPost(url) // convert RDD to text file using rDD.collect // add this file as MultipartEntity to post var response = None: Option[CloseableHttpResponse] // Is it correct way of using it ??? try { response = Some(httpClient.execute(post, httpContext)) val responseCode = response.get.getStatusLine.getStatusCode EntityUtils.consume(response.get.getEntity) // Is it require ??? if (responseCode == 200) true else false } finally { if (response.isDefined) response.get.close post.releaseConnection() // Is it require ??? } } def onShutDown = { connectionManager.close() httpClient.close() } } Multiple threads (More specifically from spark streaming context) are calling `restApiCall` method. I am relatively new to `scala` and `apache httpClient`. I have to make frequent connections to only few fixed server (i.e. 5-6 fixed URL's with different request parameters). I have also written comments in above code as question. Please address them as well. I went through multiple online resource but still not confident about it. - Is it the best way to use http client in multi-threaded environment? - Is it possible to keep live connections and use it for various requests ? Will it be beneficial in this case ? - Am i using/releasing all resources efficiently ? If not please suggest. - Is it good to use it in Scala or there exist some better library ? Thanks for your help in advance. -- Nishant Kumar Bangalore, India Mob: +91 80088 42030 Email: nishantkumar35@gmail.com --001a114fa61602e88705411bade1--