Spring Boot starter for AMQP messaging with RabbitMQ including auto-configuration, connection management, and message processing capabilities
—
Comprehensive configuration options for RabbitMQ connections, caching, SSL, retry behavior, and message handling through Spring Boot properties under the spring.rabbitmq prefix.
Basic connection configuration for RabbitMQ server connectivity.
/**
* Main RabbitMQ connection properties
*/
@ConfigurationProperties("spring.rabbitmq")
public class RabbitProperties {
/** RabbitMQ host (default: localhost) */
private String host = "localhost";
/** RabbitMQ port (default: 5672, or 5671 if SSL enabled) */
private Integer port;
/** Login username (default: guest) */
private String username = "guest";
/** Login password (default: guest) */
private String password = "guest";
/** Virtual host to use when connecting */
private String virtualHost;
/** List of addresses for cluster connections (overrides host/port) */
private List<String> addresses;
/** Address shuffle mode for cluster connections */
private AddressShuffleMode addressShuffleMode = AddressShuffleMode.NONE;
/** Requested heartbeat timeout (default: server default) */
private Duration requestedHeartbeat;
/** Number of channels per connection (default: 2047, 0 for unlimited) */
private int requestedChannelMax = 2047;
/** Connection timeout (default: 60s) */
private Duration connectionTimeout;
/** RPC timeout for channel operations (default: 10 minutes) */
private Duration channelRpcTimeout = Duration.ofMinutes(10);
/** Maximum inbound message body size (default: 64MB) */
private DataSize maxInboundMessageBodySize = DataSize.ofMegabytes(64);
}Configuration Example:
spring:
rabbitmq:
host: rabbitmq.example.com
port: 5672
username: myapp
password: ${RABBITMQ_PASSWORD}
virtual-host: /production
connection-timeout: 30s
requested-heartbeat: 30sSSL/TLS configuration for secure connections to RabbitMQ.
/**
* SSL configuration properties
*/
public static class Ssl {
/** Enable SSL support */
private Boolean enabled;
/** Path to key store */
private String keyStore;
/** Key store type (JKS, PKCS12, etc.) */
private String keyStoreType;
/** Key store password */
private String keyStorePassword;
/** Path to trust store */
private String trustStore;
/** Trust store type */
private String trustStoreType;
/** Trust store password */
private String trustStorePassword;
/** SSL algorithm to use */
private String algorithm;
/** Validate server certificate */
private boolean validateServerCertificate = true;
/** Verify hostname */
private boolean verifyHostname = true;
/** SSL bundle name for SSL configuration (Spring Boot 3.1+) */
private String bundle;
}SSL Configuration Example:
spring:
rabbitmq:
host: secure-rabbitmq.example.com
port: 5671
ssl:
enabled: true
bundle: rabbitmq-ssl
validate-server-certificate: true
verify-hostname: trueSSL Bundle Configuration (Spring Boot 3.1+):
SSL bundles provide a centralized way to configure SSL certificates and can be referenced by name:
spring:
ssl:
bundle:
jks:
rabbitmq-ssl:
keystore:
location: classpath:rabbitmq-keystore.jks
password: ${KEYSTORE_PASSWORD}
truststore:
location: classpath:rabbitmq-truststore.jks
password: ${TRUSTSTORE_PASSWORD}
rabbitmq:
ssl:
enabled: true
bundle: rabbitmq-ssl # Reference to the SSL bundle aboveAlternative PEM Bundle Configuration:
spring:
ssl:
bundle:
pem:
rabbitmq-ssl:
keystore:
certificate: classpath:client-cert.pem
private-key: classpath:client-key.pem
truststore:
certificate: classpath:ca-cert.pem
rabbitmq:
ssl:
enabled: true
bundle: rabbitmq-sslConnection and channel caching configuration for connection pooling and resource management.
/**
* Cache configuration properties
*/
public static class Cache {
/** Connection cache configuration */
private final Connection connection = new Connection();
/** Channel cache configuration */
private final Channel channel = new Channel();
/**
* Connection cache properties
*/
public static class Connection {
/** Connection cache mode (CHANNEL or CONNECTION) */
private CacheMode mode = CacheMode.CHANNEL;
/** Connection cache size (when mode is CONNECTION) */
private Integer size;
}
/**
* Channel cache properties
*/
public static class Channel {
/** Channel cache size per connection */
private Integer size;
/** Checkout timeout for channels */
private Duration checkoutTimeout;
}
}Caching Configuration Example:
spring:
rabbitmq:
cache:
connection:
mode: connection
size: 10
channel:
size: 50
checkout-timeout: 5sConfiguration for publisher confirms and returns for reliable message delivery.
/**
* Publisher-related properties in RabbitProperties
*/
// Within RabbitProperties class:
/** Enable publisher returns (for undeliverable messages) */
private boolean publisherReturns;
/** Type of publisher confirms to use */
private ConfirmType publisherConfirmType;
/**
* Publisher confirm types
*/
public enum ConfirmType {
/** No publisher confirms */
NONE,
/** Simple publisher confirms */
SIMPLE,
/** Correlated publisher confirms */
CORRELATED
}Publisher Configuration Example:
spring:
rabbitmq:
publisher-returns: true
publisher-confirm-type: correlatedRabbitTemplate-specific configuration for message operations.
/**
* Template configuration properties
*/
public static class Template {
/** Enable mandatory flag for messages */
private Boolean mandatory;
/** Receive timeout for synchronous receives */
private Duration receiveTimeout;
/** Reply timeout for sendAndReceive operations */
private Duration replyTimeout;
/** Default exchange for send operations */
private String exchange = "";
/** Default routing key for send operations */
private String routingKey = "";
/** Default receive queue */
private String defaultReceiveQueue;
/** Retry configuration */
private final Retry retry = new Retry();
}Template Configuration Example:
spring:
rabbitmq:
template:
mandatory: true
receive-timeout: 10s
reply-timeout: 30s
exchange: default.exchange
routing-key: default.keyRetry configuration for failed message operations and listener recovery.
/**
* Retry configuration properties
*/
public static class Retry {
/** Enable retry */
private boolean enabled;
/** Maximum number of attempts */
private int maxAttempts = 3;
/** Initial retry interval */
private Duration initialInterval = Duration.ofSeconds(1);
/** Retry interval multiplier */
private double multiplier = 1.0;
/** Maximum retry interval */
private Duration maxInterval = Duration.ofSeconds(10);
}
/**
* Listener-specific retry configuration
*/
public static class ListenerRetry extends Retry {
/** Whether retries are stateless */
private Boolean stateless = true;
}Retry Configuration Example:
spring:
rabbitmq:
template:
retry:
enabled: true
max-attempts: 5
initial-interval: 2s
multiplier: 2.0
max-interval: 30s
listener:
simple:
retry:
enabled: true
max-attempts: 3
initial-interval: 1s
stateless: falseProgrammatic configuration through customizer interfaces.
/**
* Customizer for RabbitMQ connection factory
*/
@FunctionalInterface
public interface ConnectionFactoryCustomizer {
void customize(com.rabbitmq.client.ConnectionFactory connectionFactory);
}
/**
* Customizer for RabbitTemplate
*/
@FunctionalInterface
public interface RabbitTemplateCustomizer {
void customize(RabbitTemplate rabbitTemplate);
}
/**
* Customizer for retry templates
*/
@FunctionalInterface
public interface RabbitRetryTemplateCustomizer {
void customize(Target target, RetryTemplate retryTemplate);
enum Target {
SENDER, LISTENER
}
}
/**
* Customizer for RabbitMQ environment
*/
@FunctionalInterface
public interface EnvironmentBuilderCustomizer {
void customize(Environment.EnvironmentBuilder builder);
}Customizer Usage Example:
import org.springframework.boot.autoconfigure.amqp.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitCustomizerConfig {
@Bean
public ConnectionFactoryCustomizer connectionFactoryCustomizer() {
return factory -> {
factory.setRequestedChannelMax(1000);
factory.setNetworkRecoveryInterval(10000);
};
}
@Bean
public RabbitTemplateCustomizer rabbitTemplateCustomizer() {
return template -> {
template.setMandatory(true);
template.setUserIdExpression(new LiteralExpression("system"));
};
}
@Bean
public RabbitRetryTemplateCustomizer retryTemplateCustomizer() {
return (target, retryTemplate) -> {
if (target == RabbitRetryTemplateCustomizer.Target.LISTENER) {
retryTemplate.registerListener(new CustomRetryListener());
}
};
}
}Configuration for RabbitMQ Streams support.
/**
* Stream configuration properties
*/
public static class Stream {
/** Stream host (defaults to main host) */
private String host;
/** Stream port (default: 5552) */
private int port = 5552;
/** Stream username */
private String username;
/** Stream password */
private String password;
/** Stream name */
private String name;
}Stream Configuration Example:
spring:
rabbitmq:
stream:
host: stream.rabbitmq.example.com
port: 5552
username: stream-user
password: ${STREAM_PASSWORD}
name: main-streamInstall with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter-amqp