/** * Provides access to <tt>Change Notifications</tt> API. * @return {@link Changes} */ public Changes changes() { return new Changes(this); }
/** * @return The next feed in the stream. */ public Row next() { return getNextRow(); }
/** * Checks whether a feed is available in the continuous stream, blocking * until a feed is received. * @return true If a feed is available */ public boolean hasNext() { return readNextRow(); }
void initChanges() { String since = endpoint.getSince(); if (since == null) { CouchDbInfo dbInfo = couchClient.context().info(); since = dbInfo.getUpdateSeq(); // get latest update seq LOG.debug("Last sequence [{}]", since); } changes = couchClient.changes().style(endpoint.getStyle()).includeDocs(true) .since(since).heartBeat(endpoint.getHeartbeat()).continuousChanges(); }
/** * Reads and sets the next feed in the stream. */ private boolean readNextRow() { boolean hasNext = false; try { if(!stop) { String row = ""; do { row = getReader().readLine(); } while(row.length() == 0); if(!row.startsWith("{\"last_seq\":")) { setNextRow(gson.fromJson(row, Row.class)); hasNext = true; } } } catch (Exception e) { terminate(); throw new CouchDbException("Error reading continuous stream.", e); } if(!hasNext) terminate(); return hasNext; }
@Override public void run() { while (changes.hasNext()) { // blocks until a feed is received ChangesResult.Row feed = changes.next(); if (feed.isDeleted() && !endpoint.isDeletes()) { continue; } if (!feed.isDeleted() && !endpoint.isUpdates()) { continue; } String seq = feed.getSeq(); JsonObject doc = feed.getDoc(); Exchange exchange = endpoint.createExchange(seq, feed.getId(), doc, feed.isDeleted()); if (LOG.isTraceEnabled()) { LOG.trace("Created exchange [exchange={}, _id={}, seq={}", exchange, feed.getId(), seq); } try { consumer.getProcessor().process(exchange); } catch (Exception e) { consumer.getExceptionHandler().handleException("Error processing exchange.", exchange, e); } } stopped = true; }
private void terminate() { httpGet.abort(); CouchDbUtil.close(getReader()); } }