Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt

Use this file to discover all available pages before exploring further.

Spring Boot auto-configures messaging infrastructure for RabbitMQ (AMQP), Apache Kafka, Apache Pulsar, and JMS with ActiveMQ Artemis. Each integration follows the same pattern: set connection properties, autowire a template to produce messages, and annotate listeners to consume them. This guide covers setup, sending, receiving, and transaction configuration for each technology.

RabbitMQ and Apache Kafka

Connect to RabbitMQ

Add the spring-boot-starter-amqp starter to your project. Configure the broker connection using spring.rabbitmq.* properties:
application.yaml
spring:
  rabbitmq:
    host: "localhost"
    port: 5672
    username: "admin"
    password: "secret"
Alternatively, configure the connection using the addresses attribute:
application.yaml
spring:
  rabbitmq:
    addresses: "amqp://admin:secret@localhost"
When addresses is set, the host and port properties are ignored. If the address uses the amqps protocol, SSL support is enabled automatically.

Send messages with RabbitTemplate

AmqpTemplate and AmqpAdmin are auto-configured. Inject AmqpTemplate (backed by RabbitTemplate) directly into your beans:
MyBean.java
@Component
public class MyBean {

    private final AmqpTemplate amqpTemplate;

    public MyBean(AmqpTemplate amqpTemplate) {
        this.amqpTemplate = amqpTemplate;
    }

    public void send(String message) {
        this.amqpTemplate.convertAndSend("myExchange", "myRoutingKey", message);
    }
}
If a MessageConverter bean is defined, it is automatically associated with the auto-configured AmqpTemplate.To enable retries when the broker connection is lost:
application.yaml
spring:
  rabbitmq:
    template:
      retry:
        enabled: true
        initial-interval: "2s"

Receive messages with @RabbitListener

Annotate any bean method with @RabbitListener to create a message listener endpoint. A default SimpleRabbitListenerContainerFactory is auto-configured:
MyBean.java
@Component
public class MyBean {

    @RabbitListener(queues = "someQueue")
    public void processMessage(String content) {
        // handle message
    }
}
Switch to a direct container type using spring.rabbitmq.listener.type=direct.Any Queue bean you define is automatically declared on the RabbitMQ instance:
MyRabbitConfiguration.java
@Bean
public Queue myQueue() {
    return new Queue("someQueue", true);
}
By default, if retries are disabled and the listener throws an exception, the delivery is retried indefinitely. Set defaultRequeueRejected=false to prevent infinite redelivery, or throw AmqpRejectAndDontRequeueException to signal that the message should be rejected.

Apache Pulsar

Add the spring-boot-starter-pulsar starter to your project. Configure the Pulsar client service URL:
application.yaml
spring:
  pulsar:
    client:
      service-url: "pulsar://localhost:6650"
Spring Boot auto-configures a PulsarTemplate for producing messages and supports @PulsarListener for consuming messages, following the same pattern as RabbitMQ and Kafka.

JMS with ActiveMQ Artemis

Add the spring-boot-starter-artemis starter to configure JMS with ActiveMQ Artemis. Configure the broker URL and credentials:
application.yaml
spring:
  artemis:
    mode: native
    broker-url: "tcp://localhost:61616"
    user: "admin"
    password: "admin"
JmsTemplate is auto-configured and can be injected directly. Use @JmsListener to create listener endpoints. If your JMS broker does not support transacted sessions, disable transaction support by customizing the listener container factory:
MyJmsConfiguration.java
@Configuration
public class MyJmsConfiguration {

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
            ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory =
            new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setSessionTransacted(false);
        return factory;
    }
}
This configuration overrides the default factory. Apply the same pattern to any other listener container factories your application defines.

Enable transaction support

Enable Kafka transactions by setting spring.kafka.producer.transaction-id-prefix. When this property is set, a KafkaTransactionManager is automatically configured and associated with the listener container factory:
application.yaml
spring:
  kafka:
    producer:
      transaction-id-prefix: "tx-"
A custom ChainedKafkaTransactionManager must be marked @Primary since it usually references the auto-configured KafkaTransactionManager bean.
Enable Kafka Streams by adding the @EnableKafkaStreams annotation and ensuring kafka-streams is on the classpath. Spring Boot auto-configures the required KafkaStreamsConfiguration bean:
application.yaml
spring:
  kafka:
    streams:
      application-id: "my-streams-app"
      bootstrap-servers: "localhost:9092"
      auto-startup: true
Wire StreamsBuilder into your @Bean to define stream processing topology:
MyKafkaStreamsConfiguration.java
@Configuration
@EnableKafkaStreams
public class MyKafkaStreamsConfiguration {

    @Bean
    public KStream<Integer, String> kStream(StreamsBuilder streamsBuilder) {
        KStream<Integer, String> stream = streamsBuilder.stream("someTopic");
        stream.to("anotherTopic");
        return stream;
    }
}
Enable retries on the AmqpTemplate for producer-side resilience, and configure dead-letter exchange routing on the broker for failed consumer messages:
application.yaml
spring:
  rabbitmq:
    template:
      retry:
        enabled: true
        initial-interval: "2s"
        max-attempts: 3
        multiplier: 1.5
When retries are exhausted on the consumer side, the default RejectAndDontRequeueRecoverer rejects the message. If the broker has a dead-letter exchange configured, rejected messages are routed there automatically. Define a custom MessageRecoverer bean to override this behavior.

Testing with embedded brokers

Annotate a test class with @EmbeddedKafka from spring-kafka-test to start an embedded Kafka broker. Remap the broker addresses into Spring Boot’s configuration property:
application-test.yaml
spring:
  kafka:
    bootstrap-servers: "${spring.embedded.kafka.brokers}"
Or configure the property name directly on the annotation:
MyTest.java
@SpringBootTest
@EmbeddedKafka(bootstrapServersProperty = "spring.kafka.bootstrap-servers")
class MyTest {
    // ...
}

Build docs developers (and LLMs) love