0
votes

I was confused by vertx instance. The first time seeing the docs, i think the instance means the the number of event-looping threads.
As i dig into the source code(vertx 2.1.2), i found the verticle instance means a task in the event-loop thread group. The event-loop thread always waits on the selector and run tasks.

Then the first Question comes:

Is it necessary to have verticle instances in the vertx? Since the vertcle run only once by one event loop. To be more precise, the event-loop thread run the Verticle start method and throw it away, it works like an entry and that is all.

My second Question is:

How to collect the results of multiple events?

Scenario
  1. send multiple queries on the event bus with the same handler instance
  2. the handler waits for every callback and modify flags
  3. if the flags reach the threadhold, do some jobs
problem

when multiple events callback, it has a chance that multiple event-loop threads will execute the handler, thus there is a race condition that the jobs will be run multiple times. How can i avoid it?

Any solutions will be apperciated.

1

1 Answers

1
votes

Is it necessary to have verticle instances in the vertx?

No. This would not be required. You do not have to create any instances of the Verticle class.

How to collect the results of multiple events?

problem

when multiple events callback, it has a chance that multiple event-loop threads will execute the handler, thus there is a race condition that the jobs will be run multiple times. How can i avoid it?

Each query which is sent over the event bus will have a corresponding Handler object. They will not share the same Handler instance. For every response to a query, it's corresponding Handler object's handle() method is called. Hence, there would be no place for a race condition over a specific Handler object.