start a background process in Quarkus

1.2k views Asked by At

I need to consume messages from RabbitMQ in a microservice written using Quarkus. I tried to use the smallrye-reactive-messaging for Quarkus but faced two problems:

  1. it only supports AMQP 1.0 and doesn't work with RabbitMQ (even if I use the experimental AMQP 1.0 plugin).
  2. it works with ActiveMQ Artemis but there's another issue: The smallrye-reactive-messaging is... reactive which is nice but there's no time right now to rewrite my database code to be reactive. Processing the message means persisting tens of thousands of documents to the mongodb which can take several minutes and it seems to be blocking the whole server:

WARNING [io.ver.cor.imp.BlockedThreadChecker] (vertx-blocked-thread-checker) Thread Thread[vert.x-eventloop-thread-0,5,main]=Thread[vert.x-eventloop-thread-0,5,main] has been blocked for 212088 ms, time limit is 2000 ms: io.vertx.core.VertxException: Thread blocked

So my idea for a workaround is to start a thread for consuming and processing the messages somewhere in Quarkus when it boots up. There's a support for scheduling periodic tasks in Quarkus is there's an annotation for background processes or do I have to write my own extension?

2

There are 2 answers

0
kosmičák On BEST ANSWER

In the end I've solved my problem by using ActiveMQ Artemis and rewriting my database code using the reactive pattern. Another approach might have been using the io.vertx.rabbitmq.RabbitMQClient in Vert.x.

In case someone came here looking for how and where a background process can be started in Quarkus I found the answer in the book Quarkus Cookbook (Chapter 5.9). There's also a section on Application Life Cycle Events in the official documentation.

So to execute some code when Quarkus boots you observe the StartupEvent in your bean:

import io.quarkus.runtime.StartupEvent;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;

@ApplicationScoped                                  
public class AppLifeEventListener {
    void onStart(@Observes StartupEvent event) {    
        // start you background thread here
        
    }
}
0
swerts On

A process can be started in Quarkus Command Mode (https://quarkus.io/guides/command-mode-reference) using a external scheduler.

In MongoDB there is a bulk insert operation that improves performance by reducing the number of round trips.

Making a batch process using an external scheduler, Quarkus Command Mode and MongoDB Bulk Inserts can improve control over executions and yield better resource utilization.