Open Source Your Knowledge, Become a Contributor

Periodic Timers

Vert.x also supports scheduling periodic tasks. The timer fires periodically calling the handler every time. There
will be an initial delay equal to the period.

Periodic timer

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

packageio.vertx.playground;

importio.vertx.core.Vertx;

publicclassPeriodicExample {

privatestaticintcounter=0;

publicstaticvoidmain(String... args) {

Vertxvertx=Vertx.vertx();

longtimerId=vertx.setPeriodic(1000, id-> {

System.out.println("Hello "+++counter);

});

}

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The return value of setPeriodic is a unique timer id (long). This can be later used if the timer needs to be cancelled.
The argument passed into the timer event handler is also the unique timer id. So, the action can decide to cancel the
periodic task.

Keep in mind that the timer will fire on a periodic basis. If your periodic treatment takes a long amount of time to proceed, your timer events could run continuously or even worse : stack up. In this case, you should consider using setTimer instead. Once your treatment has finished, you can set the next timer: