Spring RabbitMQ For High Load
Spring RabbitMQ For High Load
Martin Toshev
Who am I
Software consultant (CoffeeCupConsulting)
Twitter: @martin_fmi
2
3
Agenda
• Messaging Basics
• RabbitMQ Overview
• Spring RabbitMQ
4
Messaging Basics
5
Messaging
6
Use cases
• Many others …
7
Messaging protocols
8
Messaging protocols comparison
AMQP MQTT XMPP STOMP Kafka
goal replacement of messaging for instant messaging, message-oriented processing of large
proprietary protocols resource-constrained adopted for wider middleware real-time data feeds
devices use
format binary binary XML-based text-based binary
API divided into classes simple (5 basic different XML items ~ 10 basic commands 42 request types in
(> 40 methods in operations with 2-3 with multiple types latest version (Kafka
RabbitMQ) packet types for each) 2.0.0)
10
Common broker characteristics
• Secure message transfer, authentication and
authorization of messaging endpoints
• Broker subscriptions
11
RabbitMQ Overview
12
RabbitMQ
• An open source message broker written in Erlang
13
AMQP
• AMQP is a binary protocol that aims to standardize
middleware communication
14
AMQP characteristics
• The AMQP protocol defines:
– exchanges – the message broker endpoints that receive messages
– queues – the message broker endpoints that store messages from exchanges
and are used by subscribers for retrieval of messages
– bindings – rules that bind exchanges and queues
16
Message routing
• Different types of messaging patterns are implemented
by means of different types of exchanges
17
Default exchange
default exchange: suitable for point-to-point
communication between endpoints
exchange=“”
key=“general” Subscriber
payload=“XYZ”
Publisher (AMQP
general
default)
chat error
Subscriber
log warning
Publisher
Subscriber
18
Direct exchange
direct exchange: suitable for point-to-point
communication between endpoints
exchange=“chat”
key=“b_general” Subscriber
payload=“XYZ”
Subscriber
19
Fanout exchange
fanout exchange: suitable for broadcast type
of communication between endpoints
exchange=“log”
key=“” Subscriber
payload=“XYZ”
Subscriber
20
Topic exchange
topic exchange: suitable for multicast type of
communication between endpoints
exchange=“log”
key=“warning.#” Subscriber
payload=“XYZ”
Publisher (AMQP
default) general
chat error
warning.client warn.server Subscriber
log warn.client
Publisher warning.server
Subscriber
21
RabbitMQ clustering
22
RabbitMQ Overview
(demo)
23
Spring RabbitMQ
24
Spring RabbitMQ
25
Spring AMQP
26
Spring AMQP usage
27
RabbitAdmin
(plain Java)
CachingConnectionFactory factory = new
CachingConnectionFactory("localhost");
Queue queue = new Queue("sample-queue");
TopicExchange exchange =
new TopicExchange("sample-topic-exchange");
RabbitAdmin admin = new RabbitAdmin(factory);
admin.declareQueue(queue);
admin.declareExchange(exchange);
admin.declareBinding(BindingBuilder.bind(queue).to(exchange)
.with("sample-key"));
factory.destroy();
28
Container listener
(plain Java)
CachingConnectionFactory factory =
new CachingConnectionFactory(
"localhost");
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(factory);
Object listener = new Object() {
public void handleMessage(String message) { … }};
MessageListenerAdapter adapter = new
MessageListenerAdapter(listener);
container.setMessageListener(adapter);
container.setQueueNames("sample-queue");
container.start();
29
RabbitTemplate
(plain Java)
CachingConnectionFactory factory =
new CachingConnectionFactory("localhost");
RabbitTemplate template =
new RabbitTemplate(factory);
template.convertAndSend("", "sample-queue",
"sample-queue test message!");
30
Spring-based configuration
31
RabbitTemplate
(Spring configuration)
<rabbit:connection-factory
id="connectionFactory"
host="localhost" />
<rabbit:template id="amqpTemplate"
connection-factory="connectionFactory"
exchange=""
routing-key="sample-queue-spring"/>
32
Container listener
(Spring configuration)
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener ref="springListener"
method="receiveMessage"
queue-names="sample-queue-spring" />
</rabbit:listener-container>
<bean id="springListener"
class=“ua.org.javaday.rabbitmq.spring.ListenerSpringExample"/>
33
Container listener
(Spring configuration)
public class ListenerSpringExample {
public void receiveMessage(String message) {
System.out.println("Message received: " +
message);
}
}
34
Container listener
(Spring annotations)
public class ListenerSpringExample {
@RabbitListener(queues = "sample-queue-spring")
public void receiveMessage(String message) {
System.out.println("Message received: " +
message);
}
}
35
RabbitAdmin
(Spring configuration)
<rabbit:admin id="amqpAdmin"
connection-factory="connectionFactory" />
36
Spring Boot
37
Spring Boot
@SpringBootApplication
public class AppConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory("localhost");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
}
38
Spring Integration AMQP
39
Spring Integration AMQP
40
Spring Integration AMQP scenario
41
Spring Integration AMQP scenario
<rabbit:connection-factory
id="connectionFactory"
host="localhost" />
<channel id="test-channel" />
<rabbit:template id="amqpTemplate"
connection-factory="connectionFactory"
exchange=""
routing-key="test-queue" />
42
Spring Integration AMQP scenario
<amqp:inbound-channel-adapter
channel="test-channel"
queue-names="test-queue"
connection-factory="connectionFactory" />
<amqp:outbound-channel-adapter
channel="test-channel"
exchange-name=""
routing-key="test-destination-queue"
amqp-template="amqpTemplate" />
43
Yet another scenario
order processing system
45
Spring RabbitMQ
(demo)
46
Summary
47
Thank you !
Q&A
demos: https://github.com/martinfmi/spring_rabbitmq_samples
48
References
AMQP 0.9.1 specification
https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf
RabbitMQ documentation
http://www.rabbitmq.com/documentation.html
49
References
Choosing Your Messaging Protocol: AMQP, MQTT, or STOMP
http://blogs.vmware.com/vfabric/2013/02/choosing-your-
messaging-protocol-amqp-mqtt-or-stomp.html
50