title: How to set TCP keep Alive from HttpClient?
body: <p>My Java application which resides in AWS private subnet connects to an http server via AWS Nat gateway. I am calling a POST request via <code>HttpClient</code> to the HTTP server. That request will take more than 10 minutes to complete. I have configured a socket time out and connection timeout of 1 hour as this this a background task . But the intermediate AWS NAT gateway will send back a RST packet after 300 secs [5 mins] and cause the connection to get resetted , there is no way i can increase the NAT gateway timeout. So i need to handle the problem from my application side.</p>
  <p>My strategy is to use a TCP keep alive time which will send a packet say every 240 secs to keep the connection active. I have configured this
  as below</p>
  <pre><code>CloseableHttpClient httpClient = HttpClients.createDefault()
  HttpParams params = httpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 3600000); //connection Timeout
  HttpConnectionParams.setSoTimeout(params, 3600000); // Socket Time out
  HttpConnectionParams.setSoKeepalive(params, true); //Enable Socket level keep alive time
  </code></pre>
  <p>and then call the post request via execute method</p>
  <pre><code>HttpPost post = new HttpPost(&quot;http://url&quot;);
  HttpResponse response = httpClient.execute(post);
  </code></pre>
  <p>Since I am using a Linux system I have configured the server with following sysctl values:</p>
  <pre><code>sysctl -w net.ipv4.tcp_keepalive_time=240 
  sysctl -w net.ipv4.tcp_keepalive_intvl=240
  sysctl -w net.ipv4.tcp_keepalive_probes=10
  </code></pre>
  <p>But while executing the program the keep alive is not enabled and connection fails as previous.</p>
  <p>I have checked this with netstat -o option and as shown below keep alive is off</p>
  <pre><code>tcp        0      0 192.168.1.141:43770     public_ip:80          ESTABLISHED 18134/java           off (0.00/0/0)
  </code></pre>
  <p>Is there any way i can set TCP keep alive from java code using httpclient . Also I can see <code>HttpConnectionParams</code> are deprecated. But I couldn't find any new class which can set keep alive</p>