Commit 4602558a authored by Stephane Nicoll's avatar Stephane Nicoll

Polish AMQP doc

Closes gh-3431
parent 0ce7be44
...@@ -34,6 +34,7 @@ Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson; ...@@ -34,6 +34,7 @@ Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson;
:spring-reference: http://docs.spring.io/spring/docs/{spring-docs-version}/spring-framework-reference/htmlsingle :spring-reference: http://docs.spring.io/spring/docs/{spring-docs-version}/spring-framework-reference/htmlsingle
:spring-security-reference: http://docs.spring.io/spring-security/site/docs/{spring-security-docs-version}/reference/htmlsingle :spring-security-reference: http://docs.spring.io/spring-security/site/docs/{spring-security-docs-version}/reference/htmlsingle
:spring-javadoc: http://docs.spring.io/spring/docs/{spring-docs-version}/javadoc-api/org/springframework :spring-javadoc: http://docs.spring.io/spring/docs/{spring-docs-version}/javadoc-api/org/springframework
:spring-amqp-javadoc: http://docs.spring.io/spring-amqp/docs/current/api/org/springframework/amqp
:spring-data-javadoc: http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa :spring-data-javadoc: http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa
:spring-data-commons-javadoc: http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data :spring-data-commons-javadoc: http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data
:spring-data-mongo-javadoc: http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb :spring-data-mongo-javadoc: http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb
......
...@@ -2867,7 +2867,6 @@ locate a JMS `ConnectionFactory` using JNDI. By default the locations `java:/Jms ...@@ -2867,7 +2867,6 @@ locate a JMS `ConnectionFactory` using JNDI. By default the locations `java:/Jms
[[boot-features-using-jms-template]]
[[boot-features-using-jms-sending]] [[boot-features-using-jms-sending]]
==== Sending a message ==== Sending a message
Spring's `JmsTemplate` is auto-configured and you can autowire it directly into your own Spring's `JmsTemplate` is auto-configured and you can autowire it directly into your own
...@@ -2936,14 +2935,14 @@ more details. ...@@ -2936,14 +2935,14 @@ more details.
[[boot-features-amqp]] [[boot-features-amqp]]
=== AMQP === AMQP
The Advanced Message Queuing Protocol (AMQP) is a platform-neutral, wire-level protocol for The Advanced Message Queuing Protocol (AMQP) is a platform-neutral, wire-level protocol
message-oriented middleware. The Spring AMQP project applies core Spring concepts to the development of for message-oriented middleware. The Spring AMQP project applies core Spring concepts to
AMQP-based messaging solutions. the development of AMQP-based messaging solutions.
[[boot-features-rabbitmq]] [[boot-features-rabbitmq]]
==== RabbitMQ support ==== RabbitMQ support
RabbitMQ is a lightweight, reliable, scalable and portable message broker based on the AMQP protocol. RabbitMQ is a lightweight, reliable, scalable and portable message broker based on the
Spring uses RabbitMQ to communicate using the AMQP protocol. AMQP protocol. Spring uses `RabbitMQ` to communicate using the AMQP protocol.
RabbitMQ configuration is controlled by external configuration properties in RabbitMQ configuration is controlled by external configuration properties in
`+spring.rabbitmq.*+`. For example, you might declare the following section in `+spring.rabbitmq.*+`. For example, you might declare the following section in
...@@ -2953,8 +2952,8 @@ RabbitMQ configuration is controlled by external configuration properties in ...@@ -2953,8 +2952,8 @@ RabbitMQ configuration is controlled by external configuration properties in
---- ----
spring.rabbitmq.host=localhost spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672 spring.rabbitmq.port=5672
spring.rabbitmq.username=guest spring.rabbitmq.username=admin
spring.rabbitmq.password=guest spring.rabbitmq.password=secret
---- ----
See See
...@@ -2964,24 +2963,22 @@ for more of the supported options. ...@@ -2964,24 +2963,22 @@ for more of the supported options.
TIP: Check http://spring.io/blog/2010/06/14/understanding-amqp-the-protocol-used-by-rabbitmq/[Understanding AMQP, the protocol used by RabbitMQ] TIP: Check http://spring.io/blog/2010/06/14/understanding-amqp-the-protocol-used-by-rabbitmq/[Understanding AMQP, the protocol used by RabbitMQ]
for more details. for more details.
[[boot-features-using-amqp-template]]
[[boot-features-using-amqp-sending]] [[boot-features-using-amqp-sending]]
==== Sending a message ==== Sending a message
Spring's `AmqpTemplate` and `AmqpAdmin` are auto-configured and you can autowire them directly into your own Spring's `AmqpTemplate` and `AmqpAdmin` are auto-configured and you can autowire them
beans: directly into your own beans:
[source,java,indent=0] [source,java,indent=0]
---- ----
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class MyBean { public class MyBean {
private final AmqpAdmin amqpAdmin; private final AmqpAdmin amqpAdmin;
private final AmqpTemplate amqpTemplate; private final AmqpTemplate amqpTemplate;
@Autowired @Autowired
...@@ -2995,76 +2992,38 @@ beans: ...@@ -2995,76 +2992,38 @@ beans:
} }
---- ----
To send a message you should declare a queue using `AmqpAdmin` and then use `AmqpTemplate` to send NOTE: {spring-amqp-javadoc}/rabbit/core/RabbitMessagingTemplate.{dc-ext}[`RabbitMessagingTemplate`]
the message to the declared queue. can be injected in a similar manner.
[source,java,indent=0]
----
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
// ...
@PostConstruct
public void setUpQueue() {
this.amqpAdmin.declareQueue(new Queue("foo"));
}
public void send() {
this.rabbitTemplate.convertAndSend("foo", "hello");
}
} Any `org.springframework.amqp.core.Queue` that is defined as a bean will be automatically
---- used to declare a corresponding queue on the RabbitMQ instance if necessary.
[[boot-features-using-amqp-receiving]] [[boot-features-using-amqp-receiving]]
==== Receiving a message ==== Receiving a message
Spring's `ConnectionFactory` is auto-configured and you can autowire it directly into your own
beans.
To receive a message from a queue you should create a `SimpleMessageListenerContainer` When the Rabbit infrastructure is present, any bean can be annotated with `@RabbitListener`
using the configured `ConnectionFactory`. The `SimpleMessageListenerContainer` is to create a listener endpoint. If no `RabbitListenerContainerFactory` has been defined, a
responsible for handling incoming messages through it's `MessageListenerAdapter`. default one is configured automatically.
[source,java,indent=0]
----
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
@SpringBootApplication The following component creates a listener endpoint on the `someQueue` queue:
public class AmqpDemoApplication {
@Autowired
private ConnectionFactory connectionFactory;
@Bean [source,java,indent=0]
public SimpleMessageListenerContainer container() { ----
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer( @Component
this.connectionFactory); public class MyBean {
MessageListenerAdapter adapter = new MessageListenerAdapter(new Object() {
public void handleMessage(String msg) {
System.out.println(msg);
}
});
container.setMessageListener(adapter);
container.setQueueNames("foo");
return container;
}
@RabbitListener(queues = "someQueue")
public void processMessage(String content) {
// ... // ...
}
} }
---- ----
In the code above the `SimpleMessageListenerContainer` was configured to receive messages from the declared "foo" queue. TIP: Check {spring-amqp-javadoc}/rabbit/annotation/EnableRabbit.{dc-ext}[the Javadoc of `@EnableRabbit`]
When a message is sent to "foo" queue it will be delivered to the handleMessage(String msg) method. for more details.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment