mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
Add AMQP 1.0 RabbitMQ auto-configuration
See gh-49857 Signed-off-by: Eddú Meléndez <eddu.melendez@gmail.com>
This commit is contained in:
committed by
Stéphane Nicoll
parent
ebac87ae65
commit
0ed57a6109
@@ -95,6 +95,7 @@ dependencies {
|
||||
implementation(project(path: ":module:spring-boot-actuator"))
|
||||
implementation(project(path: ":module:spring-boot-actuator-autoconfigure"))
|
||||
implementation(project(path: ":module:spring-boot-amqp"))
|
||||
implementation(project(path: ":module:spring-boot-amqp-rabbitmq"))
|
||||
implementation(project(path: ":module:spring-boot-cache"))
|
||||
implementation(project(path: ":module:spring-boot-cache-test"))
|
||||
implementation(project(path: ":module:spring-boot-data-cassandra"))
|
||||
|
||||
+82
@@ -213,3 +213,85 @@ You can also customize the javadoc:org.springframework.core.retry.RetryPolicy[]
|
||||
IMPORTANT: By default, if retries are disabled and the listener throws an exception, the delivery is retried indefinitely.
|
||||
You can modify this behavior in two ways: Set the `defaultRequeueRejected` property to `false` so that zero re-deliveries are attempted or throw an javadoc:org.springframework.amqp.AmqpRejectAndDontRequeueException[] to signal the message should be rejected.
|
||||
The latter is the mechanism used when retries are enabled and the maximum number of delivery attempts is reached.
|
||||
|
||||
|
||||
|
||||
[[messaging.amqp.rabbitmq-amqp]]
|
||||
== RabbitMQ AMQP 1.0 Support
|
||||
|
||||
https://www.rabbitmq.com/[RabbitMQ] supports the AMQP 1.0 protocol via the https://github.com/rabbitmq/rabbitmq-amqp-java-client[RabbitMQ AMQP 1.0 Java client].
|
||||
Spring AMQP provides support for this client through `spring-rabbitmq-client`.
|
||||
Spring Boot offers auto-configuration for this support via the `spring-boot-starter-amqp-rabbitmq` starter.
|
||||
|
||||
RabbitMQ AMQP 1.0 configuration is controlled by external configuration properties in `+spring.amqp.rabbitmq.*+`.
|
||||
For example, you might declare the following section in `application.properties`:
|
||||
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
amqp:
|
||||
rabbitmq:
|
||||
host: "localhost"
|
||||
port: 5672
|
||||
username: "admin"
|
||||
password: "secret"
|
||||
----
|
||||
|
||||
Alternatively, you could configure the same connection using the `address` attribute:
|
||||
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
amqp:
|
||||
rabbitmq:
|
||||
address: "amqp://admin:secret@localhost"
|
||||
----
|
||||
|
||||
NOTE: When specifying an address that way, the `host` and `port` properties are ignored.
|
||||
|
||||
See javadoc:org.springframework.boot.amqp.autoconfigure.RabbitAmqpProperties[] for more of the supported property-based configuration options.
|
||||
To configure lower-level details of the auto-configured javadoc:com.rabbitmq.client.amqp.Environment[], define a javadoc:org.springframework.boot.amqp.autoconfigure.RabbitAmqpEnvironmentBuilderCustomizer[] bean.
|
||||
|
||||
If a javadoc:com.rabbitmq.client.amqp.CredentialsProvider[] bean exists in the context, it will be automatically used by the auto-configured javadoc:com.rabbitmq.client.amqp.Environment[].
|
||||
|
||||
|
||||
|
||||
[[messaging.amqp.rabbitmq-amqp.sending]]
|
||||
=== Sending a Message
|
||||
|
||||
Spring's javadoc:org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate[] and javadoc:org.springframework.amqp.rabbitmq.client.RabbitAmqpAdmin[] are auto-configured, and you can autowire them directly into your own beans, as shown in the following example:
|
||||
|
||||
include-code::MyBean[]
|
||||
|
||||
If a javadoc:org.springframework.amqp.support.converter.MessageConverter[] bean is defined, it is associated automatically to the auto-configured javadoc:org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate[].
|
||||
|
||||
You can set properties for the template as follows:
|
||||
|
||||
[configprops,yaml]
|
||||
----
|
||||
spring:
|
||||
amqp:
|
||||
rabbitmq:
|
||||
template:
|
||||
exchange: "my-exchange"
|
||||
routing-key: "my-key"
|
||||
default-receive-queue: "my-queue"
|
||||
----
|
||||
|
||||
You can also customize the javadoc:org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate[] programmatically by declaring a javadoc:org.springframework.boot.amqp.autoconfigure.RabbitAmqpTemplateCustomizer[] bean.
|
||||
|
||||
|
||||
|
||||
[[messaging.amqp.rabbitmq-amqp.receiving]]
|
||||
=== Receiving a Message
|
||||
|
||||
When the RabbitMQ AMQP 1.0 infrastructure is present, any bean can be annotated with javadoc:org.springframework.amqp.rabbit.annotation.RabbitListener[format=annotation] to create a listener endpoint.
|
||||
If no javadoc:org.springframework.amqp.rabbitmq.client.config.RabbitAmqpListenerContainerFactory[] has been defined, a default one is automatically configured.
|
||||
|
||||
The following sample component creates a listener endpoint on the `someQueue` queue:
|
||||
|
||||
include-code::MyBean[]
|
||||
|
||||
TIP: See javadoc:org.springframework.amqp.rabbit.annotation.EnableRabbit[format=annotation] for more details.
|
||||
|
||||
To customize the listener container, define a javadoc:org.springframework.amqp.rabbit.config.ContainerCustomizer[] bean parameterized with javadoc:org.springframework.amqp.rabbitmq.client.listener.RabbitAmqpListenerContainer[].
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.docs.messaging.amqp.rabbitmqamqp.receiving;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RabbitListener(queues = "someQueue")
|
||||
public class MyBean {
|
||||
|
||||
@RabbitHandler
|
||||
public void processMessage(String content) {
|
||||
// ...
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2012-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.docs.messaging.amqp.rabbitmqamqp.sending;
|
||||
|
||||
import org.springframework.amqp.rabbitmq.client.RabbitAmqpAdmin;
|
||||
import org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyBean {
|
||||
|
||||
private final RabbitAmqpAdmin amqpAdmin;
|
||||
|
||||
private final RabbitAmqpTemplate amqpTemplate;
|
||||
|
||||
public MyBean(RabbitAmqpAdmin amqpAdmin, RabbitAmqpTemplate amqpTemplate) {
|
||||
this.amqpAdmin = amqpAdmin;
|
||||
this.amqpTemplate = amqpTemplate;
|
||||
}
|
||||
|
||||
// @fold:on // ...
|
||||
public void someOtherMethod() {
|
||||
this.amqpTemplate.convertAndSend("hello");
|
||||
}
|
||||
// @fold:off
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user