Skip to content

Spring RabbitMQ

Forage creates Spring RabbitMQ CachingConnectionFactory beans for AMQP messaging with the camel-spring-rabbitmq component.

Quick Start

forage.spring.rabbitmq.host=localhost
forage.spring.rabbitmq.port=5672
forage.spring.rabbitmq.username=guest
forage.spring.rabbitmq.password=guest
- to:
    uri: spring-rabbitmq:orders
    parameters:
      connectionFactory: "#rabbitConnectionFactory"

Properties

Property Description Type Default Required
forage.spring.rabbitmq.host The RabbitMQ broker host string localhost
forage.spring.rabbitmq.port The RabbitMQ broker port integer 5672
forage.spring.rabbitmq.virtual.host The RabbitMQ virtual host string /
forage.spring.rabbitmq.addresses Comma-separated list of host:port addresses for cluster failover string

Security

Property Description Type Default Required
forage.spring.rabbitmq.username The RabbitMQ username string guest
forage.spring.rabbitmq.password The RabbitMQ password password guest

Advanced

Property Description Type Default Required
forage.spring.rabbitmq.channel.cache.size The number of channels to maintain in cache integer 25
forage.spring.rabbitmq.cache.mode The cache mode (CHANNEL or CONNECTION) string CHANNEL
forage.spring.rabbitmq.connection.cache.size The number of connections to cache (CONNECTION mode only) integer 1
forage.spring.rabbitmq.channel.checkout.timeout Timeout in milliseconds when waiting for a channel from the cache long 30000
forage.spring.rabbitmq.requested.heartbeat Heartbeat interval in seconds for detecting dead connections integer 60
forage.spring.rabbitmq.connection.timeout Connection timeout in milliseconds integer 30000
forage.spring.rabbitmq.automatic.recovery.enabled Enable automatic connection recovery after failure boolean true
forage.spring.rabbitmq.network.recovery.interval Interval in milliseconds between recovery attempts long 5000

Multiple Brokers

Use prefixed configuration to create multiple named connection factories:

forage.primary.spring.rabbitmq.host=broker1.example.com
forage.primary.spring.rabbitmq.port=5672
forage.primary.spring.rabbitmq.username=admin
forage.primary.spring.rabbitmq.password=secret

forage.backup.spring.rabbitmq.host=broker2.example.com
forage.backup.spring.rabbitmq.port=5672
forage.backup.spring.rabbitmq.username=admin
forage.backup.spring.rabbitmq.password=secret
- to:
    uri: spring-rabbitmq:orders
    parameters:
      connectionFactory: "#primary"
- to:
    uri: spring-rabbitmq:notifications
    parameters:
      connectionFactory: "#backup"

Cluster Failover

Use the addresses property to connect to a RabbitMQ cluster. When set, it takes precedence for connection routing:

forage.spring.rabbitmq.addresses=broker1:5672,broker2:5672,broker3:5672
forage.spring.rabbitmq.automatic.recovery.enabled=true
forage.spring.rabbitmq.network.recovery.interval=5000

Health and Metrics

Forage automatically enables Spring Boot Actuator health indicators and Micrometer metrics for RabbitMQ when the corresponding dependencies are present.

Health Indicators

When spring-boot-starter-actuator is on the classpath, Forage registers health indicators for all RabbitMQ connection factories:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The health indicator checks broker connectivity and reports server version:

curl http://localhost:8080/actuator/health
{
  "status": "UP",
  "components": {
    "rabbit": {
      "status": "UP",
      "details": {
        "version": "3.13.0"
      }
    }
  }
}

Metrics

When micrometer-core and actuator are on the classpath, Forage automatically configures RabbitMQ client metrics for all connection factories:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Available metrics include: - rabbitmq.connections - Number of open connections - rabbitmq.channels - Number of open channels - rabbitmq.consumed - Messages consumed - rabbitmq.published - Messages published - rabbitmq.acknowledged - Messages acknowledged - rabbitmq.rejected - Messages rejected

Each metric is tagged with the connection factory name:

curl http://localhost:8080/actuator/metrics/rabbitmq.published
{
  "name": "rabbitmq.published",
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 42.0
    }
  ],
  "availableTags": [
    {
      "tag": "name",
      "values": ["rabbit"]
    }
  ]
}

Cache Modes

The CachingConnectionFactory supports two cache modes:

  • CHANNEL (default) — caches channels within a single connection. Suitable for most use cases.
  • CONNECTION — caches connections and channels. Use when you need multiple connections to the broker.
forage.spring.rabbitmq.cache.mode=CONNECTION
forage.spring.rabbitmq.connection.cache.size=5
forage.spring.rabbitmq.channel.cache.size=25