Sending JsonArray of JsonArrays Over Event Bus

62 views
Skip to first unread message

PrimeSoftware

unread,
Aug 28, 2016, 12:16:30 PM8/28/16
to vert.x
Hi all,
I have created a worker verticle that runs my JDBC queries and I submit jobs to the JDBC verticle by way of the Event Bus. The problem I am having with batch queries though is the "batchWithParams" call requires a List<JsonArray>. I initially tried sending the List<JsonArray> over the event bus in a JsonObject but when I cast the message in the worker verticle I get an unchecked assignment warning. I thought I could then do a JsonArray with each element being a JsonArray then call the .getList but I still get the same warning.

//getJsonArray("params") is JsonArray of JsonArrays
List<JsonArray> jsonBatchParams = message.body().getJsonArray("params").getList();

Julien Viet

unread,
Aug 29, 2016, 2:40:19 AM8/29/16
to ve...@googlegroups.com
Hi,

which version of Vert.x are you using ?

--
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.
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/7d8807bb-e431-40d9-b700-80e89fabfd70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

llfbandit

unread,
Aug 29, 2016, 5:11:45 AM8/29/16
to vert.x
Hi,

This isn't really related to Vertx. You must know what's in your JsonArray. The only thing to do is to silence this warning on the call by putting this @SuppressWarnings("unchecked") before.
JsonArray is a list of Objects.

But you can serialize a list of JsonArray if you want, as a class member by implementing ClusterSerializable and its read and write methods.

Here's a sample with a Set<String>
public void writeToBuffer(Buffer buff) {
    buff
.appendInt(set == null ? 0 : set.size());
   
if (set != null) {
     
for (String entry : set) {
       
byte[] bytes = entry.getBytes(StandardCharsets.UTF_8);
        buff
.appendInt(bytes.length).appendBytes(bytes);
     
}
   
}
 
}

 
public int readFromBuffer(int pos, Buffer buffer) {
   
int num = buffer.getInt(pos);
    pos
+= 4;
   
for (int i = 0; i < num; i++) {
     
int len = buffer.getInt(pos);
      pos
+= 4;
     
byte[] bytes = buffer.getBytes(pos, pos + len);
      pos
+= len;
     
set.add(new String(bytes, StandardCharsets.UTF_8));
   
}
   
return pos;
 
}



Message has been deleted

PrimeSoftware

unread,
Aug 31, 2016, 5:15:02 PM8/31/16
to vert.x
Awesome! Thanks for pointing me in the right direction! I was being lazy and trying to avoid a loop, but what must be will be.
Reply all
Reply to author
Forward
0 new messages