0

I'm building a simple app that checks repeatedly changed made in certain file in a server. I'm using HttpURLConnection instance like so:

HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("https://www.aSite/alerts.json").openConnection();

try (InputStream inputStream = httpURLConnection.getInputStream())
{
    ...
}

I want to use this one connection to receive a response, but there is not a built in method for repeating the request. I fear that if I use the code block in a loop it will create new connection every time.

Is there a simple solution to this?

Roy Ash
  • 1,078
  • 1
  • 11
  • 28
  • 1
    Javadoc says: _"Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances."_ Maybe avoid creating multiple URL instances. – Rocco May 14 '21 at 19:45
  • Actually I have only 1 `URL` instance and in my code I hold it in a separate variable, so you’re saying that I just don’t need to `disconnect()` the connection? – Roy Ash May 14 '21 at 19:48
  • Since the topic is interesting I found an older similar question here https://stackoverflow.com/questions/35208950/java-httpurlconnection-and-pooling – Rocco May 14 '21 at 21:04
  • So do I need to do `openConnection()` every time I want to send a `GET` but the underlying connection will be the same one, or is it enough to loop over the `try` block? – Roy Ash May 14 '21 at 21:15
  • 1
    I think you have to do `openConnection()` every time and also set `setUseCaches(false)` to avoid page caching. – Rocco May 14 '21 at 21:42
  • 1
    I just tested it using nc, and the connection is kept alive across multiple requests. – Rocco May 14 '21 at 22:04
  • Amazing, thanks! Just what is nc? – Roy Ash May 14 '21 at 22:09
  • 1
    netcat, a network utility that I used to simulate a single-connection HTTP server – Rocco May 14 '21 at 22:14
  • So you can write an answer and I'll mark it as solved – Roy Ash May 14 '21 at 22:16
  • As long as you don't `disconnect()` the `HttpURLConnection` and do your request frequently enough (before the keep-alive timeout expires on any of the client or server side), and no error that would force the connection to close occurs, the same connection should be reused, as long as no request to the same host is being made concurrently. In case of multiple concurrent requests, then multiple connections may be opened. – daniel May 17 '21 at 11:26

1 Answers1

0

It's better to write an api at server side to push the file change notification into reddis queue and try to get those notification from your client app.

I hope this link will work for you. https://www.baeldung.com/spring-data-redis-pub-sub

And if you wanna handle at client side only then you can use spring webclient along with spring scheduler and implement it in asynchronous manner.

Vivek Jain
  • 317
  • 3
  • 12
  • 1
    You’re absolutely right, but the server isn’t mine… I don’t have control over the API – Roy Ash May 14 '21 at 19:52