What is the @Async equivilant for Quarkus application?

Viewed 3659

In one class I want to call a method, but not have to wait until the method finishes. Normally in a spring application I would use @Async, but what is the way to go about in a Quarkus application?

Beneath is a simple example to get started. In the 'StartWork' class the 'Work' gets started. (I left out the Work-interface, but you can see one of its implementations: WorkA). After calling 'work.do()' the startWork() method should proceed without waiting for the work.do() to finish.

@ApplicationScoped
public class WorkA implements Work {
    public void do() {
        System.out.println("Starting work A.");
        try {
            Thread.sleep(1000l);
            System.out.println("Finished work A.");
        } catch(InterruptedException ex) {
            System.out.println("My work got interrupted.");
        }
    }
}

@ApplicationScoped
public class StartWork {

@Inject
Work work;

    public void startWork() {
        work.do();
        System.out.println("I dont' care when and if the work finished, but it has started.");
    }
}

Here is the same example, but now I've tried to use Mutiny:

@ApplicationScoped
public class WorkA implements Work {
    public void do() {
        Uni.createFrom().voidItem().invoke(Runnable -> {
            System.out.println("Starting work A.");
            try {
                Thread.sleep(1000l);
                System.out.println("Finished work A.");
            } catch(InterruptedException ex) {
                System.out.println("My work got interrupted.");
            }
        }
    });
}

@ApplicationScoped
public class StartWork {

@Inject
Work work;

    public void startWork() {
        work.do();
        System.out.println("I dont' care when and if the work finished, but it has started.");
    }
}

When running this example I do not see the lines being printed. So I guess the anonymous runnable is not invoked?

Minimal reproducible product: https://gitlab.com/rmvanderspek/quarkus-multithreading

1 Answers

Kudo's to Turing85 for finding the answer.

As it turns out Quarkus works with an EventBus for asynchronous actions. A producer is created, but works lazily and thus won't be invoked until a consumer is subscribed to this producer.

A working example: https://gitlab.com/rmvanderspek/quarkus-multithreading

In short:

@ApplicationScoped
public class WorkA implements Work {

    @Override
    public void doWork() {
        log.info("Do work");

        Uni.createFrom()
        .item(UUID::randomUUID)
        .emitOn(Infrastructure.getDefaultWorkerPool())
        .subscribe()
        .with(this::worker, Throwable::printStackTrace);
    }

    private Uni<Void> worker(UUID uuid) {
        log.info("Starting work: " + uuid);
        try {
            Thread.sleep((long) Math.random() * 1000);
        } catch (InterruptedException ex) {
            log.info("Could not finish work: " + uuid);
            throw new RuntimeException(ex);
        }
        log.info("Finish work: {}.", uuid);
        return Uni.createFrom().voidItem();
    }
}

@ApplicationScoped
public class StartWork {

    @Inject
    Work work;

    public void startWork() {
        work.do();
        System.out.println("I dont' care when and if the work finished, but it has started.");
    }
}
Related