How do you use CouchDB Change Notifications Continuous Changes with Java?

I try to use the couchdb (continuous) API with Java and find that after the list of current changes has been exhausted, the thread seems to be closed, and not stay open forever, as intended.

The code I'm using is below. I would expect to never exit the while loop, but do it as soon as the current changes are completed by the thread. I am relatively new to couchdb and Java, so something obvious might not be there. Can someone show me how to write this correctly?

URL url = new URL("[path to database here]/_changes?feed=continuous";); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while((line = reader.readLine()) != null){ // do something with the line here } // Should never get here under normal circumstances reader.close(); 
+6
couchdb
source share
3 answers

In fact, the default minimum timeout is 60,000 ms (60 seconds) unless a different timeout or pulse value is provided. I updated _ modifies the wiki page in October and included any default values ​​that I found in the code.

Setting the heartbeat basically means that you will be monitoring the timeout in the client, that is, there is no new line for the heartbeat period, which means that you definitely lost the connection. I believe that CouchDB disables timeout checking if there is a pulse.

In any case, you should probably expect the connection to be closed at some point and the code for this condition.

+7
source share

You can use &heartbeat=1000 to get couchdb by sending new lines over the cable every second. This will keep your connection open until you disconnect and / or CouchDB disconnected.

But you're right, I also expected that the connection would not be closed - it looks like conn.setReadTimeout(0); nothing helps.

+3
source share

This is just an assumption, as I don’t know enough about implementing continuous couchdb loading or implementing HttpUrlConnection. But there seems to be a ban on any errors in the code of the two: if your java connection client has a timeout set lower than the default default heartbeat for continuous couchdb changes, the connection can end using the java client.

Just a thought.

0
source share

All Articles