The English version of is the official project site. Translated sites are community supported on a best-effort basis.

Getting Started to Quarkus Messaging with RabbitMQ

This guide demonstrates how your Quarkus application can utilize Quarkus Messaging to interact with RabbitMQ.


preview(预览) 中,不保证向后兼容和在生态系统中的存在。具体的改进可能需要改变配置或API,并且正在计划变得 稳定 。欢迎在我们的 邮件列表 中提供反馈,或在我们的 GitHub问题列表 中提出问题。

For a full list of possible statuses, check our FAQ entry.



  • 大概15分钟

  • 编辑器

  • JDK 17+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.9.6

  • Docker and Docker Compose or Podman, and Docker Compose

  • 如果你愿意的话,还可以选择使用Quarkus CLI

  • 如果你想构建原生可执行程序,可以选择安装Mandrel或者GraalVM,并正确配置(或者使用Docker在容器中进行构建)


In this guide, we are going to develop two applications communicating with a RabbitMQ broker. The first application sends a quote request to the RabbitMQ quote requests exchange and consumes messages from the quote queue. The second application receives the quote request and sends a quote back.


The first application, the producer, will let the user request some quotes over an HTTP endpoint. For each quote request, a random identifier is generated and returned to the user, to put the quote request on pending. At the same time the generated request id is sent to the quote-requests exchange.

Producer App UI

The second application, the processor, in turn, will read from the quote-requests queue put a random price to the quote, and send it to an exchange named quotes.

最后, producer 将读取报价,并使用服务器发送事件将其发送给浏览器。因此,用户将实时看到报价从 pending 更新为收到的价格。



克隆 Git 仓库: git clone ,或下载一个 存档

The solution is located in the rabbitmq-quickstart directory.


首先,我们需要创建两个项目: producerprocessor

要创建 producer 项目,请在终端中运行:

quarkus create app org.acme:rabbitmq-quickstart-producer \
    --extension='messaging-rabbitmq,rest-jackson' \

创建Grade项目,请添加 --gradle 或者 --gradle-kotlin-dsl 参数。

For more information about how to install and use the Quarkus CLI, see the Quarkus CLI guide.

mvn io.quarkus.platform:quarkus-maven-plugin:3.11.3:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=rabbitmq-quickstart-producer \
    -Dextensions='messaging-rabbitmq,rest-jackson' \

创建Grade项目,请添加 -DbuildTool=gradle 或者 -DbuildTool=gradle-kotlin-dsl 参数。

For Windows users:

  • If using cmd, (don’t use backward slash \ and put everything on the same line)

  • If using Powershell, wrap -D parameters in double quotes e.g. "-DprojectArtifactId=rabbitmq-quickstart-producer"


  1. The Reactive Messaging RabbitMQ connector

  2. Quarkus REST (formerly RESTEasy Reactive) and its Jackson support to handle JSON payloads

If you already have your Quarkus project configured, you can add the messaging-rabbitmq extension to your project by running the following command in your project base directory:

quarkus extension add messaging-rabbitmq
./mvnw quarkus:add-extension -Dextensions='messaging-rabbitmq'
./gradlew addExtension --extensions='messaging-rabbitmq'

这将在你的 pom.xml 中添加以下内容:


要创建 processor 项目,请在同一目录下运行:

quarkus create app org.acme:rabbitmq-quickstart-processor \
    --extension='messaging-rabbitmq' \

创建Grade项目,请添加 --gradle 或者 --gradle-kotlin-dsl 参数。

For more information about how to install and use the Quarkus CLI, see the Quarkus CLI guide.

mvn io.quarkus.platform:quarkus-maven-plugin:3.11.3:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=rabbitmq-quickstart-processor \
    -Dextensions='messaging-rabbitmq' \

创建Grade项目,请添加 -DbuildTool=gradle 或者 -DbuildTool=gradle-kotlin-dsl 参数。

For Windows users:

  • If using cmd, (don’t use backward slash \ and put everything on the same line)

  • If using Powershell, wrap -D parameters in double quotes e.g. "-DprojectArtifactId=rabbitmq-quickstart-processor"


├── rabbitmq-quickstart-processor
│  ├──
│  ├── mvnw
│  ├── mvnw.cmd
│  ├── pom.xml
│  └── src
│     └── main
│        ├── docker
│        ├── java
│        └── resources
│           └──
└── rabbitmq-quickstart-producer
   ├── mvnw
   ├── mvnw.cmd
   ├── pom.xml
   └── src
      └── main
         ├── docker
         ├── java
         └── resources



The Quote class will be used in both producer and processor projects. For the sake of simplicity we will duplicate the class. In both projects, create the src/main/java/org/acme/rabbitmq/model/ file, with the following content:

package org.acme.rabbitmq.model;

import io.quarkus.runtime.annotations.RegisterForReflection;

public class Quote {

    public String id;
    public int price;

    * Default constructor required for Jackson serializer
    public Quote() { }

    public Quote(String id, int price) { = id;
        this.price = price;

    public String toString() {
        return "Quote{" +
                "id='" + id + '\'' +
                ", price=" + price +

JSON representation of Quote objects will be used in messages sent to the RabbitMQ queues and also in the server-sent events sent to browser clients.

Quarkus has built-in capabilities to deal with JSON RabbitMQ messages.


The @RegisterForReflection annotation instructs Quarkus to keep the class, its fields, and methods when creating a native executable. This is crucial when we later run our applications as native executables within containers. Without this annotation, the native compilation process would discard the fields and methods during the dead-code elimination phase, which would lead to runtime errors. More details about the @RegisterForReflection annotation can be found on the native application tips page.


Inside the producer project locate the generated src/main/java/org/acme/rabbitmq/producer/ file, and update the content to be:

package org.acme.rabbitmq.producer;

import java.util.UUID;


import org.acme.rabbitmq.model.Quote;
import org.eclipse.microprofile.reactive.messaging.Channel;
import org.eclipse.microprofile.reactive.messaging.Emitter;

import io.smallrye.mutiny.Multi;

public class QuotesResource {

    @Channel("quote-requests") Emitter<String> quoteRequestEmitter; (1)

     * Endpoint to generate a new quote request id and send it to "quote-requests" channel (which
     * maps to the "quote-requests" RabbitMQ exchange) using the emitter.
    public String createRequest() {
        UUID uuid = UUID.randomUUID();
        quoteRequestEmitter.send(uuid.toString()); (2)
        return uuid.toString();
1 注入一个响应式消息 Emitter ,来向 quote-requests 通道发送消息。
2 On a post request, generate a random UUID and send it to the RabbitMQ queue using the emitter.

This channel is mapped to a RabbitMQ exchange using the configuration we will add to the file. Open the src/main/resource/ file and add:

# Configure the outgoing RabbitMQ exchange `quote-requests`

All we need to specify is the smallrye-rabbitmq connector. By default, reactive messaging maps the channel name quote-requests to the same RabbitMQ exchange name.


Now let’s consume the quote request and give out a price. Inside the processor project, locate the src/main/java/org/acme/rabbitmq/processor/ file and add the following:

package org.acme.rabbitmq.processor;

import java.util.Random;

import jakarta.enterprise.context.ApplicationScoped;

import org.acme.rabbitmq.model.Quote;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;

import io.smallrye.reactive.messaging.annotations.Blocking;

 * A bean consuming data from the "quote-requests" RabbitMQ queue and giving out a random quote.
 * The result is pushed to the "quotes" RabbitMQ exchange.
public class QuoteProcessor {

    private Random random = new Random();

    @Incoming("requests")       (1)
    @Outgoing("quotes")         (2)
    @Blocking                   (3)
    public Quote process(String quoteRequest) throws InterruptedException {
        // simulate some hard-working task
        return new Quote(quoteRequest, random.nextInt(100));
1 表示该方法消费 requests 通道中的项
2 表示方法返回的对象被发送给 quotes 通道
3 表示该处理是 blocking ,不能在调用者线程上运行。

The process method is called for every RabbitMQ message from the quote-requests queue, and will send a Quote object to the quotes exchange.

As with the previous example we need to configure the connectors in the file. Open the src/main/resources/ file and add:

# Configure the incoming RabbitMQ queue `quote-requests`

# Configure the outgoing RabbitMQ exchange `quotes`

Note that in this case we have one incoming and one outgoing connector configuration, each one distinctly named. The configuration properties are structured as follows:


channel-name 片段必须与 @Incoming@Outgoing 注解中设定的值相匹配:

  • quote-requests → RabbitMQ queue from which we read the quote requests

  • quotes → RabbitMQ exchange in which we write the quotes


回到我们的 producer 项目。让我们修改 QuotesResource 来消费报价,将其绑定到一个HTTP端点,来向客户端发送事件:

import io.smallrye.mutiny.Multi;

@Channel("quotes") Multi<Quote> quotes;     (1)

 * Endpoint retrieving the "quotes" queue and sending the items to a server sent event.
@Produces(MediaType.SERVER_SENT_EVENTS) (2)
public Multi<Quote> stream() {
    return quotes; (3)
1 使用 @Channel 修饰符注入 quotes 通道
2 表示内容是使用 Server Sent Events 发送的
3 返回流 (Reactive Stream) 。

Again we need to configure the incoming quotes channel inside producer project. Add the following inside file:

# Configure the outgoing `quote-requests` queue

# Configure the incoming `quotes` queue



producer 项目中创建 src/main/resources/META-INF/resources/quotes.html 文件,内容如下:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Quotes</title>

    <link rel="stylesheet" type="text/css"
    <link rel="stylesheet" type="text/css"
<div class="container">
    <div class="card">
        <div class="card-body">
            <h2 class="card-title">Quotes</h2>
            <button class="btn btn-info" id="request-quote">Request Quote</button>
            <div class="quotes"></div>
<script src=""></script>
    $("#request-quote").click((event) => {
        fetch("/quotes/request", {method: "POST"})
        .then(res => res.text())
        .then(qid => {
            var row = $(`<h4 class='col-md-12' id='${qid}'>Quote # <i>${qid}</i> | <strong>Pending</strong></h4>`);
    var source = new EventSource("/quotes");
    source.onmessage = (event) => {
      var json = JSON.parse(;
      $(`#${}`).html(function(index, html) {
        return html.replace("Pending", `\$\xA0${json.price}`);




> mvn -f rabbitmq-quickstart-producer quarkus:dev


> mvn -f rabbitmq-quickstart-processor quarkus:dev

Quarkus starts a RabbitMQ broker automatically, configures the application and shares the broker instance between different applications. See Dev Services for RabbitMQ for more details.

在你的浏览器中打开 http://localhost:8080/quotes.html ,点击按钮来请求一些报价。


When not running in dev or test mode, you will need to start your RabbitMQ broker. You can follow the instructions from the RabbitMQ Docker website or create a docker-compose.yaml file with the following content:

version: '2'


    image: rabbitmq:3.12-management
      - "5672:5672"
      - rabbitmq-quickstart-network

    image: quarkus-quickstarts/rabbitmq-quickstart-producer:1.0-${QUARKUS_MODE:-jvm}
      context: rabbitmq-quickstart-producer
      dockerfile: src/main/docker/Dockerfile.${QUARKUS_MODE:-jvm}
      RABBITMQ_HOST: rabbit
      RABBITMQ_PORT: 5672
      - "8080:8080"
      - rabbitmq-quickstart-network

    image: quarkus-quickstarts/rabbitmq-quickstart-processor:1.0-${QUARKUS_MODE:-jvm}
      context: rabbitmq-quickstart-processor
      dockerfile: src/main/docker/Dockerfile.${QUARKUS_MODE:-jvm}
      RABBITMQ_HOST: rabbit
      RABBITMQ_PORT: 5672
      - rabbitmq-quickstart-network

    name: rabbitmq-quickstart

Note how the RabbitMQ broker location is configured. The rabbitmq-host and rabbitmq-port (AMQP_HOST and AMQP_PORT environment variables) properties configure location.


> mvn -f rabbitmq-quickstart-producer clean package
> mvn -f rabbitmq-quickstart-processor clean package

一旦打包完成,请运行 docker compose up --build 。UI在 http://localhost:8080/quotes.html


> mvn -f rabbitmq-quickstart-producer package -Dnative  -Dquarkus.native.container-build=true
> mvn -f rabbitmq-quickstart-processor package -Dnative -Dquarkus.native.container-build=true

-Dquarkus.native.container-build=true 指示Quarkus构建可以在容器中运行的64位Linux本地可执行文件。然后,使用以下命令运行系统:

> export QUARKUS_MODE=native
> docker compose up --build

与之前一样,UI也是在 http://localhost:8080/quotes.html


This guide has shown how you can interact with RabbitMQ using Quarkus. It utilizes SmallRye Reactive Messaging to build data streaming applications.

If you did the Kafka, you have realized that it’s the same code. The only difference is the connector configuration and the JSON mapping.

Related content