Upload
maxim-konovalov
View
219
Download
1
Embed Size (px)
Citation preview
Java Messaging with AMQP and RabbitMQ
или “JMS, давай до-свидания”
The big picture
Async Java World
Servlets 3.0 - async requests Servlets 3.1 - nio
Netty ReactiveX(RxJava)
Vert.xJAX-RSSpring MVC
Play Framework Akka
Messaging Protocols
AMQP MQTTSTOMP
0-9-1 1-0
JMS
SMTP, POP3, IMAP
XMPPDDS
Protocols comparison
AMQP factsWire Protocol
Родился в 2006 in J.P.Morgan (миллиарды сообщений в день)
Миссия: интеграция сообщений между платформами
Поддерживается в Apache Camel, Spring Integration, Mule,..
AMQP is the Internet Protocol for Business Messaging
versions:
1.0 - Now International standard: ISO/IEC 19464:2014, OASIS (2014)
RabbitMQ supports version 1.0 via plugin0-10, 0-9-1, 0-9, 0-8
1.0:Not declaring broker architecture, broker
management commands. Symmetric.
FramesFrame structure:
Sending content:
Content Header:
One Million Messages Per Second on Google31 nodes, 8 virtual CPUs on each, 248 cores total
(2014, https://blog.pivotal.io/pivotal/products/rabbitmq-hits-one-million-messages-per-second-on-google-compute-engine
JMS
AMQP
Exchange
Direct Fanout Topic Headers
Types:
Features:• Durability (survive broker restart)• Auto-delete (deleted when all queues have finished using it)
Default Exchange
Topic Exchange
* (star) can substitute for exactly one word. # (hash) can substitute for zero or more words.
More Exchange Types• Headers - по заголовкам сообщения “x-
match”=“any”/“all”
• Dead Letter Exchange (для недоставленных сообщений)
• Constant-Hash (plugin)
• Delayed Exchange ( задержки перед отправкой)
• more …
Queues
• FIFO (soft)
• Durable / in-memory
• Exclusive
• Auto-delete
• Arguments (TTL,…)
Consumers
• Rule: One message goes to one consumer, unless redelivery happens
• Serial per Channel (thread)
• Pull / Subscribe API
• Send acknowledgements (ack)
• can do “auto-ack” ( hits reliability )
• round robin with optional priorities
Что у кролика внутриErlang
• создан в Ericsson для телеком-железа• functional• lightweight processes• good scaling over CPU• deadlocks-aware design• communication between network-distributed processes• Live code upgrade• Built in Process Monitoring and Control
Concurrency
Spring@Beanpublic ConnectionFactory connectionFactory() {
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");cf.setUsername("guest");cf.setPassword("guest");return cf;
}
@Beanpublic AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());}
@Beanpublic RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());// Отправлять сюдаtemplate.setRoutingKey(this.helloWorldQueueName);// Чиать отсюдаtemplate.setQueue(this.helloWorldQueueName);return template;
}
Spring async consume@Beanpublic SimpleMessageListenerContainer listenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory());container.setQueueNames(this.helloWorldQueueName);container.setMessageListener(new MessageListenerAdapter(new
HelloWorldHandler()));return container;
}
Sync:AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);System.out.println("Received: " + amqpTemplate.receiveAndConvert());
Spring: ListenerContainerAbstractMessageListenerContainerprivate volatile Object messageListener;private volatile AcknowledgeMode acknowledgeMode = AcknowledgeMode.AUTO;private MessageConverter messageConverter;private ErrorHandler errorHandler = new ConditionalRejectingErrorHandler();
…
SimpleMessageListenerContainer
private volatile Executor taskExecutor = new SimpleAsyncTaskExecutor();private volatile int concurrentConsumers = 1;private volatile Integer maxConcurrentConsumers;private volatile boolean exclusive;private volatile int prefetchCount = DEFAULT_PREFETCH_COUNT; // 1
…
RabbitTemplatecurrently, the only implementation of AMQPTemplate
Methods:send(Message m, …), convertAndSend(Object o , …) …
recieve(): Message, recieveAndConvert(…), recieveAndReply(…) …
sendAndRecieve(…), convertSendAndRecieve(), …
Features:• Retry and recovery (spring-retry)• Publisher confirms and returns (ReturnCallback, ConfirmCallback)• Message PostProcessors - after receive and before publish (zip/unzip,
…)
Finally, AMQP vs JMS
JMSStandard API
Native to Java worldEnterprizzy
AMQPTruly Cross - platform
Comprehensive TopologyMore Tuning (?)
Some Readings:
“Understanding the differences between AMQP and JMS”
http://www.wmrichards.com/amqp.pdf
Why Erlang is good for AMQP implementation
https://pubs.vmware.com/vfabric5/index.jsp#com.vmware.vfabric.rabbitmq.2.4/erlang.html
Alvaro Videla’s presentation “RabbitMQ Internal Architecture”
http://www.slideshare.net/old_sound/dissecting-the-rabbit
All About Messaging Protocols. What Are the Differences? by Bryon Moyer
http://www.eejournal.com/archives/articles/20150420-protocols/
Whats wrong with AMQP and how to fix it
http://www.imatix.com/articles:whats-wrong-with-amqp/
Messaging Products performance comparison
https://softwaremill.com/mqperf/
Thanks
Sincerely Yours,
Maxim Konovalov
BPC Banking Technologies, Runet Business Systems
https://www.facebook.com/maxim.konovalov.1