fetching db data: sending string or json over event bus

224 views
Skip to first unread message

qsys

unread,
Jul 20, 2013, 7:23:15 AM7/20/13
to ve...@googlegroups.com
Hey all, I'm trying out vert.x and so far, I like it a lot. I'm just writing some test applications in java... and like most applications, I have a verticle to fetch some data from a database and sending back the response, in this case the db being postgresql. In the most common approach, data from the db is mapped to java classes.
However, since (1) json is the prefered format for communicating over the vert.x event bus and (2) postgresql 9.2 comes with native json support, I just want to skip the orm step. This could avoid a whole bunch of hardly used model classes, which are basically only used to construct a JsonObject afterwards, this means more cpu and memory usage.

Thus, I'm fetching the data from the db returns a json-string, so I'm actually reading a String result in java. There are still two possibilities here: or I send the (json-formatted) String as is over the event bus or I turn the String into a JsonObject (or JsonArray) before sending over the event bus. In the latter case, there is some (unnecessary) overhead for object creation, serialization and deserialization.

What would be the best strategy here? The question might also be: is there any advantage of constructing a JsonObject on the server side before sending the data back to the client? My gut feeling points me to "just send the string and don't bother about creating a JsonObject". I just want to be sure I'm not missing something crucial.

Just for clarity: no extra data handling is necessary after fetching it from the database (or if data handling is necessary, I might choose to handle it on the client). I just want to fetch data and send it to the client. So the client (javascript) have something like:
eventbus.send("fetchdata", somerequestdata, function(replydata) { doSomethingWithTheDataProbablyShowIt() })


On the server side I have something like (simplified):
public class FetchData extends BusModBase {
    @Override
    public void start() {
        vertx.eventBus().registerHandler("fetchdata", new Handler<Message<String>>(){
            public void handle(Message<JsonObject> requestData) {
                String fetchedData = fetchDataFromPostgresql(requestData);
// convert fetchedData to json or not?
  requestData.reply(fetchedData); } }); } [...] }

Nate McCall

unread,
Jul 20, 2013, 5:16:22 PM7/20/13
to ve...@googlegroups.com
Don't bother encoding to JsonObject, just send back the String. SInce
the source (postgres) can be assumed as syntactically valid, the extra
encoding step wont buy you anything.

The only other point is to make sure you do this as a worker verticle
given you are using a blocking JDBC API (there is another thread going
currently on about porting a non-blocking postgres driver which you
may want to watch:
https://groups.google.com/forum/#!topic/vertx/Q6_n57-dV_o). You are
pretty much there by using BusMod though. The rest is really just
config.

Relevant worker references:
http://vertx.io/manual.html#writing-blocking-code-introducing-worker-verticles
http://vertx.io/manual.html#the-background-pool
http://vertx.io/mods_manual.html#worker
> --
> 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+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

qsys

unread,
Jul 21, 2013, 3:23:37 PM7/21/13
to ve...@googlegroups.com
Allright, that's pretty straight forward... And yeah, I should configure the jdbc verticle as a worker verticle. I will definitely check out the other threat (and code).
However, I still have one other problem, but that's for another thread... (I can't seem to get the vertx event bus properly working).

Thx, Kurt


Op zaterdag 20 juli 2013 23:16:22 UTC+2 schreef Nate McCall het volgende:
Reply all
Reply to author
Forward
0 new messages