30
Java Messaging with AMQP and RabbitMQ или “JMS, давай до-свидания”

Java Messaging with AMQP and RabbitMQ

Embed Size (px)

Citation preview

Page 1: Java Messaging with AMQP and RabbitMQ

Java Messaging with AMQP and RabbitMQ

или “JMS, давай до-свидания”

Page 2: Java Messaging with AMQP and RabbitMQ

The big picture

Page 3: Java Messaging with AMQP and RabbitMQ
Page 4: Java Messaging with AMQP and RabbitMQ

Async Java World

Servlets 3.0 - async requests Servlets 3.1 - nio

Netty ReactiveX(RxJava)

Vert.xJAX-RSSpring MVC

Play Framework Akka

Page 5: Java Messaging with AMQP and RabbitMQ

Messaging Protocols

AMQP MQTTSTOMP

0-9-1 1-0

JMS

SMTP, POP3, IMAP

XMPPDDS

Page 6: Java Messaging with AMQP and RabbitMQ

Protocols comparison

Page 7: Java Messaging with AMQP and RabbitMQ

AMQP factsWire Protocol

Родился в 2006 in J.P.Morgan (миллиарды сообщений в день)

Миссия: интеграция сообщений между платформами

Поддерживается в Apache Camel, Spring Integration, Mule,..

Page 8: Java Messaging with AMQP and RabbitMQ

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.

Page 9: Java Messaging with AMQP and RabbitMQ
Page 10: Java Messaging with AMQP and RabbitMQ

FramesFrame structure:

Sending content:

Content Header:

Page 11: Java Messaging with AMQP and RabbitMQ

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

Page 12: Java Messaging with AMQP and RabbitMQ

JMS

AMQP

Page 13: Java Messaging with AMQP and RabbitMQ

Exchange

Direct Fanout Topic Headers

Types:

Features:• Durability (survive broker restart)• Auto-delete (deleted when all queues have finished using it)

Default Exchange

Page 14: Java Messaging with AMQP and RabbitMQ
Page 15: Java Messaging with AMQP and RabbitMQ
Page 16: Java Messaging with AMQP and RabbitMQ

Topic Exchange

* (star) can substitute for exactly one word. # (hash) can substitute for zero or more words.

Page 17: Java Messaging with AMQP and RabbitMQ

More Exchange Types• Headers - по заголовкам сообщения “x-

match”=“any”/“all”

• Dead Letter Exchange (для недоставленных сообщений)

• Constant-Hash (plugin)

• Delayed Exchange ( задержки перед отправкой)

• more …

Page 18: Java Messaging with AMQP and RabbitMQ

Queues

• FIFO (soft)

• Durable / in-memory

• Exclusive

• Auto-delete

• Arguments (TTL,…)

Page 19: Java Messaging with AMQP and RabbitMQ

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

Page 20: Java Messaging with AMQP and RabbitMQ
Page 21: Java Messaging with AMQP and RabbitMQ
Page 22: Java Messaging with AMQP and RabbitMQ

Что у кролика внутри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

Page 23: Java Messaging with AMQP and RabbitMQ

Concurrency

Page 24: Java Messaging with AMQP and RabbitMQ

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;

}

Page 25: Java Messaging with AMQP and RabbitMQ

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());

Page 26: Java Messaging with AMQP and RabbitMQ

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

Page 27: Java Messaging with AMQP and RabbitMQ

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,

…)

Page 28: Java Messaging with AMQP and RabbitMQ

Finally, AMQP vs JMS

JMSStandard API

Native to Java worldEnterprizzy

AMQPTruly Cross - platform

Comprehensive TopologyMore Tuning (?)

Page 29: Java Messaging with AMQP and RabbitMQ

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/

Page 30: Java Messaging with AMQP and RabbitMQ

Thanks

Sincerely Yours,

Maxim Konovalov

BPC Banking Technologies, Runet Business Systems

https://www.facebook.com/maxim.konovalov.1