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