Vertx set periodic runs for all threads.

825 views
Skip to first unread message

Idob

unread,
Aug 10, 2017, 10:31:36 AM8/10/17
to vert.x
Hello,

My machine runs with 16 vertx threads. That is great.

I want a single thread to run periodically and clean something. 

vertx.setPeriodic(TEN MINUTES, id -> {
   
if (!queue.isEmpty()) {
        vertx.executeBlocking(future -> { do stuff}
   
}
});

Now,  I see that this code runs 16 times - for all threads. only one blocking thread will excute because of my if clause. 

Why is this?  Can I some how define a single periodical?

Reagrds,

Ido

Paulo Lopes

unread,
Aug 10, 2017, 11:54:25 AM8/10/17
to vert.x
I believe the easiest for you would be to use vertx shared data a get a counter, then do a compareAndSet to only allow one timer to run and run your code.

http://vertx.io/docs/apidocs/io/vertx/core/shareddata/Counter.html
http://vertx.io/docs/apidocs/io/vertx/core/shareddata/SharedData.html

javadevmtl

unread,
Aug 10, 2017, 12:07:55 PM8/10/17
to vert.x
If the timer is launched in 16 instances of the same verticle, even a static AtomicBoolean will do the trick.

class MyVerticle extends AbstractVerticle {
    static AtomicBoolean queueEmpty = new AtomicBoolean(true or false); depending on the state you want to start with...

    public void start() {
    vertx.setPeriodic(TEN MINUTES, id -> {
    //
Do logic based on AtomicBoolean.
});

Paulo Lopes

unread,
Aug 11, 2017, 4:54:29 AM8/11/17
to vert.x
the static approach assumes your 16 instances are all on the same JVM/Classloader, Vert.x is distributed by nature so you could in theory have 16 instances over several machines and in that case the atomic would not suffice. But yes for a single JVM is should also do the trick.

id...@bluerbn.com

unread,
Aug 12, 2017, 12:10:02 PM8/12/17
to vert.x
"Vert.x is distributed by nature so you could in theory have 16 instances over several machines and in that case the atomic would not suffice"

So what should we do within cluster mode? as thats probably the most popular use-case..

Thanks,
Idan.

Blake

unread,
Aug 16, 2017, 1:14:02 PM8/16/17
to vert.x
You'd have to use ClusterWideMap (vertx.sharedData().getClusterWideMap()) if you're actually distributed.

id...@bluerbn.com

unread,
Aug 16, 2017, 4:22:34 PM8/16/17
to vert.x
And how you going to set the periodic cluster using this? boolean flag?

Blake

unread,
Aug 16, 2017, 5:55:12 PM8/16/17
to vert.x
If you only want one verticle doing it then you could do that; but, keep in mind, that that verticle/node could go down for whatever reason - so a better approach would be to register what verticle the timer is on and then other verticles that are started up check to see if the deployed verticle is still in the cluster if a verticle id is already registered in the map.

E.g., (you should actually check if the async results succeeded too, but i did not to save space...)
vertx.sharedData().getClusterWideMap("timermap", asyncResult -> {
    asyncResult.result().putIfAbsent("some timer name", this.deploymentID(), putAsyncResult -> { 
        if (putAsyncResult.result() != null) {  
            // a previous verticle set the id, check the cluster to see if it's still up
        } else {
            // successfully set to this verticle deployment ID. setup your timer 
        }
    })
})

Also note that code probably wont work verbatim as I havent tested it and it's just something to give you an idea real quick.

Thomas SEGISMONT

unread,
Aug 28, 2017, 11:42:16 AM8/28/17
to ve...@googlegroups.com
In cluster mode, you could use a cluster wide lock: http://vertx.io/docs/vertx-core/java/#_cluster_wide_locks

When the verticle instance gets the lock, it should check if the task was executed less than ten minutes ago.
Yes -> Do nothing
No -> Update the timestamp and do the task

The timestamp would be an entry in a shared data map.

--
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/fd165d8c-3234-47ca-9f6e-5613070d95b8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages