How to use periodic timers to invoke blocking code?

468 views
Skip to first unread message

mathias

unread,
May 28, 2018, 5:02:22 AM5/28/18
to vert.x

Is it a bad idea to initialize a periodic timer within a worker verticle? 

I have the following concerns about this idea:
1. Assuming that the periodic timer gets cancelled after 10 minutes, doe the worker thread get completely blocked for 10 minutes?
2. Assuming that I have 4 worker threads on my machine, does it mean that I can have most 4 (uncancelled) timers while more timers would lead to blocking exceptions?
3. Is the concept of vertx timers only designed for the eventloop and should be avoided to be used within the worker context?

How to use  timers to invoke blocking code?
4. Is it a better idea to setup the periodic timer within the eventloop and use executeBlocking inside the handler implementation to run invoke the blocking code?
5. Is vertx able to cancel a timer by the timer-ID passed as an argument to the handler from inside a executeBlocking code although the timer runs in the eventloop?

Thomas SEGISMONT

unread,
May 28, 2018, 10:50:30 AM5/28/18
to ve...@googlegroups.com
2018-05-28 11:02 GMT+02:00 mathias <roleba...@gmail.com>:

Is it a bad idea to initialize a periodic timer within a worker verticle? 

No
 

I have the following concerns about this idea:
1. Assuming that the periodic timer gets cancelled after 10 minutes, doe the worker thread get completely blocked for 10 minutes?

Not sure what you mean.
 
2. Assuming that I have 4 worker threads on my machine, does it mean that I can have most 4 (uncancelled) timers while more timers would lead to blocking exceptions?

No, you can create many timers within a worker verticle.
 
3. Is the concept of vertx timers only designed for the eventloop and should be avoided to be used within the worker context?

No
 

How to use  timers to invoke blocking code?
4. Is it a better idea to setup the periodic timer within the eventloop and use executeBlocking inside the handler implementation to run invoke the blocking code?

No, it's fine to create a worker verticle and then setup a timer in its start method.
 
5. Is vertx able to cancel a timer by the timer-ID passed as an argument to the handler from inside a executeBlocking code although the timer runs in the eventloop?

It may but I'm not sure why you would do this.
 

--
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+unsubscribe@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/4afdc7e4-5bcc-4691-bc1d-30667ba08d0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mathias

unread,
May 29, 2018, 6:43:42 AM5/29/18
to vert.x
Thank you for clarification!

Here is a small example for question number 5:
class TimerTest extends AbstractVerticle{

 
@Override
 
public void start() throws Exception {
 
       
//****************************
       
//Original context (eventloop)
       
//****************************
 
        vertx
.setPeriodic(10000, timerId -> {
            vertx
.executeBlocking(future -> {

               
//********************************
               
//Worker thread but which context?
               
//********************************    
                           
               
//Invoke some blocking: myBlockingCode();                
               
                vertx
.cancelTimer(timerId); //<-- does this work even within the the worker thread?
           
}, false, res -> {...});
       
});
       
 
}
                   
}

I am not sure if java is able to guarantee that the variable "timerId" within the worker thread is correct after 10000 milliseconds waiting before the timer fires and executes the executeBlocking() method which tires to access "timerId" from another thread (the worker thread).

Julien Viet

unread,
May 29, 2018, 7:28:08 AM5/29/18
to ve...@googlegroups.com
you can totally cancel a periodic or a timer from any thread given you have its ID.

Julien

--
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.
Reply all
Reply to author
Forward
0 new messages