Vertx and couchdb

80 views
Skip to first unread message

EclipseTalk

unread,
Aug 12, 2017, 8:14:27 PM8/12/17
to vert.x

Hello,
I using the change apis to get couchdb document update as follow:


// feed type continuous
changes 
= dbClient.changes().includeDocs(true) .since('546').heartBeat(30000).continuousChanges();
 
// live access to continuous feeds
while (changes.hasNext()) { 
  
ChangesResult.Row feed = changes.next();
  
  
String docId = feed.getId()
  com
.google.gson.JsonObject doc = feed.getDoc()
  
String seq = feed.getSeq()
  
Gson gson = new Gson()
  
String json = gson.toJson(doc);
  
System.out.println(json);
  
System.out.println("Event docId : " + docId + " - Sequence: " + seq)
  
System.out.println("Event Json doc: " + json)

// Send event to the Vertx event bus....

}


I would like to send an event to the eventbus from the while loop above upon new documents changes and have a consumer verticle handle events from the event bus and respond. 
Where should I create the Vertx instance? Is there any sample code to get me started?
Any help is appreciated.

Thanks

Jez P

unread,
Aug 13, 2017, 2:19:32 PM8/13/17
to vert.x
Is this code non-blocking or blocking? If you deploy it within a verticle then, while you may need to wrap in executeBlocking, the verticle already has a reference to the vertx instance. If not in a verticle, then pass the vertx instance in. You'll need to create a vertx instance somewhere for access to the event bus (and on the other side for listeners).

Is this a distributed application or all in one JVM? Are you already deploying verticles? You'll need to tell us a bit more if you want more suggestions than what I've made above. 
Message has been deleted
Message has been deleted
Message has been deleted

EclipseTalk

unread,
Aug 14, 2017, 8:34:27 AM8/14/17
to vert.x
Thanks for the reply, this is blocking code. I do something like:

Invoke the verticle in a clustered vertx as:

Vertx.clusteredVertx(options,  { res ->
  
if (res.succeeded()) {
 
def vertx = res.result()
 
def eventBus = vertx.eventBus()
 
System.out.println("We now have a Vert.x instance as clustered")
 
 
getFeedFromCouchDb(vertx, eventBus)

 
  
} else {
 
System.out.println("Failed: " + res.cause());
  
}
 
})



static private void getFeedFromCouchDb(vertx, eventBus) {
........

// live access to continuous feeds
 
while (changes.hasNext()) {
 
ChangesResult.Row feed = changes.next();
 
  com
.google.gson.JsonObject doc = feed.getDoc()

 
Gson gson = new Gson()
 
String json = gson.toJson(doc)

 
JsonObject eventDoc = new JsonObject(json)
 
 
// Run vertx in blocking mode
  vertx
.executeBlocking( { future ->
 
  eventBus
.publish("feed", eventDoc);
 
System.out.println("Publish event message sent address 'couchdb.feed': " + eventDoc.encodePrettily())
 
  future
.complete(result);
 
},  { res ->
 
if (res.succeeded()) {
 
System.out.println("Success: "+ res.result());
 
}
 
});
 
}


The problem is that the eventloop thread will block eventually. How can I listen to the couchdb continuous feed without blocking the event loop?

WARNING: Thread Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 9470 ms, time limit is 2000

Thanks for the help

Thomas SEGISMONT

unread,
Aug 28, 2017, 11:34:21 AM8/28/17
to ve...@googlegroups.com
For that kind of use cases, I would schedule a task on a plain old Java executor service and send messages on the event bus from there. The event bus class is thread safe and you can send from any thread (including non Vert.x threads).

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/f73dbf7e-92ae-4e3a-b2d4-7f52603cfe80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages