adam bien's blog

Simplest Possible Quarkus Scheduler / Timer 📎

quarkus.io does not support EJBs and so EJB Timers (like e.g.: Simplest Possible EJB 3.1 Timer). The extension quarkus-scheduler (to install use: mvn quarkus:add-extension -Dextensions=quarkus-scheduler) offers adequate functionality:

import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicLong;

import javax.enterprise.context.ApplicationScoped;

import io.quarkus.scheduler.Scheduled;

@ApplicationScoped
public class Scheduler {

    private AtomicLong COUNTER = new AtomicLong();

    @Scheduled(every="1s")
    public void increase() {
        System.out.println("+"+COUNTER.incrementAndGet() + " " + LocalDateTime.now());
    }

    @Scheduled(every="2s")
    public void decrease() {
        System.out.println("-"+COUNTER.decrementAndGet() + " " + LocalDateTime.now());
    }
}    
Output:

2019-07-12 06:16:48,316 INFO  [io.qua.dep.QuarkusAugmentor] (main) Beginning quarkus augmentation
2019-07-12 06:16:48,855 INFO  [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 539ms
2019-07-12 06:16:49,053 INFO  [io.quarkus] (main) Quarkus 0.19.0 started in 0.808s. Listening on: http://[::]:8080
2019-07-12 06:16:49,055 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy, scheduler]
+1 2019-07-12T06:16:49.064741
-0 2019-07-12T06:16:49.064740
+1 2019-07-12T06:16:50.055152
-0 2019-07-12T06:16:51.053965
+1 2019-07-12T06:16:51.054133
+2 2019-07-12T06:16:52.058805

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.