Vert.x Shared Data - Share Data Between Threads in Vert.x

342 views Asked by At

I am using vertx version - 3.5.1. I want to track my logs in ELK, For that I am using elastic.apm.

I am initialising my co.elastic.apm.api.Transaction object in the beginning of the method. I want to carry forward this object to the other threads which we are calling using event bus.

Below is my code.

SharedData sharedData = vertx.sharedData();
LocalMap<String, co.elastic.apm.api.Transaction> map1 = sharedData.getLocalMap("mymap1");
map1.put("transaction", transaction);

I am getting below exception.

java.lang.IllegalArgumentException: Invalid type for shareddata data structure: co.elastic.apm.api.NoopTransaction

I tried putting in JsonObject as well. But no luck.

SharedData sharedData = vertx.sharedData();
LocalMap<String, co.elastic.apm.api.Transaction> map1 = sharedData.getLocalMap("mymap1");
map1.put("transaction", transaction);
2

There are 2 answers

0
Sergey Afinogenov On

I suppose this source Vert.x code describes wich types in this case is allowed:

static void checkType(Object obj) {
if (obj instanceof String ||
    obj instanceof Integer ||
    obj instanceof Long ||
    obj instanceof Boolean ||
    obj instanceof Double ||
    obj instanceof Float ||
    obj instanceof Short ||
    obj instanceof Byte ||
    obj instanceof Character ||
    obj instanceof byte[] ||
    obj instanceof Buffer ||
    obj instanceof JsonObject ||
    obj instanceof JsonArray ||
    obj instanceof Shareable) {
} else {
  throw new IllegalArgumentException("Invalid type for shareddata data structure: " + obj.getClass().getName());
 }
}
2
Asad Awadia On

I want to carry forward this object to the other threads which we are calling using event bus.

There is no way to do this.

The best way is to put them in a ConcurrentHashMap with a transaction id - that gets passed over the eventbus

But the fact that the pieces of work that need to be in the same transaction are across eventbus handlers show that the distribution and partitioning was done incorrectly i.e. things that are to be done in a transaction should be kept together