Download - Messaging in Java
![Page 1: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/1.jpg)
Messaging Approaches in Java
(JMS, AMQP)
Kirill Afanasjev
Jug.lv
Riga,Latvia
![Page 2: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/2.jpg)
Why Messaging?
1. Start sending binary data with TCP 2. Add queueing 3. Add networking abstraction 4. Add authentification and ACL 5. Add virtual connections 6. Add high avalaibility 7. Add publish/subscribe 8. Result would be very similar :)
![Page 3: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/3.jpg)
RPC
CORBA, SOAP Web Services, RMI, XML-RPC Synchronous Tight coupling
![Page 4: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/4.jpg)
Message oriented middleware
Sender and receiver know nothing about each other, only destination and message format
Email is for people what messaging is for applications
![Page 5: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/5.jpg)
Advantages
Asynchronous
A client does not have to request messages in order to receive them
Sender can fire and forget the message to the broker
Reliable
It is possible to guarantee message is delivered safely once and only once
![Page 6: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/6.jpg)
Disadvantages
Extra component in architecture (message transfer agent or message broker)
Inter-application communication tend to be synchronous
Lack of standarts
![Page 7: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/7.jpg)
Point-to-point
![Page 8: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/8.jpg)
Point-to-point
Message queues, senders and receivers Message is sent to a queue Each message has only one consumer Queue may be configured to persist messages May be used for load balancing
![Page 9: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/9.jpg)
Publish-subscribe
![Page 10: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/10.jpg)
Publish-subscribe
Publishers, subscribers, topics Message may have multiple consumers, or no
consumer at all Each message is delivered to every client
subscribed to a topic
![Page 11: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/11.jpg)
JMS
Java Message Oriented Middleware API Part of the Java EE Defined in specification developed under JSR
914 RFC 6167 defines a jms: URI scheme JMS 1.0.2b (June 25, 2001) JMS 1.1 (March 18, 2002) JMS 2 - ?
![Page 12: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/12.jpg)
JMS architecture
JMS provider (example : ActiveMQ) JMS clients Messages Administered objects (Destinations and
connection factories) Native clients
![Page 13: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/13.jpg)
JMS API
ConnectionFactory Connection Session Message producer Message producer Destination
- Queue- Topic
Message
![Page 14: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/14.jpg)
JMS API
![Page 15: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/15.jpg)
JMS message
Header Properties (optional) Body (optional)
![Page 16: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/16.jpg)
JMS message headers
JMSCorrelationId - (String) This header is set by the application for use by other applications.
JMSDestination JMSDeliveryMode - (Integer) This header is set
by the JMS provider and denotes the delivery mode.
JMSExpiration
![Page 17: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/17.jpg)
JMS message headers
JMSPriority - (Integer) The priority of the message.
JMSMessageId JMSTimestamp - (Long) The time the message
was sent. JMSReplyTo JMSType JMSRedelivered
![Page 18: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/18.jpg)
JMS message delivery modes
DeliveryMode.NON_PERSISTENT DeliveryMode.PERSISTENT
![Page 19: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/19.jpg)
JMS message selector
Message consumer receives only messages whose headers and properties match the selector
A message selector cannot select messages on the basis of content of the message body
![Page 20: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/20.jpg)
JMS provider implementations
Apache ActiveMQ
Apache Qpid, using AMQP
EMS from TIBCO
OpenJMS, from The OpenJMS Group
JBoss Messaging and HornetQ from JBoss
Open Message Queue, from Sun Microsystems
BEA Weblogic and Oracle AQ from Oracle
RabbitMQ, using AMQP
Solace JMS from Solace Systems
SonicMQ from Progress Software
StormMQ, using AMQP
WebSphere MQ (formerly MQSeries) from IBM
![Page 21: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/21.jpg)
Spring JMS support
Message-driven POJOs MessageConverter, to convert between Java
objects and JMS messages JMSTemplate
![Page 22: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/22.jpg)
Sending message with Spring
public class JmsQueueSender {
private JmsTemplate jmsTemplate;
private Queue queue;
public void simpleSend() {
this.jmsTemplate.send(this.queue, new MessageCreator(){
public Message createMessage(Session session) {
return session.createTextMessage("hello queue world");
}
});
}
}
![Page 23: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/23.jpg)
Receiving message with Spring
public class ExampleListener implements MessageListener {
public void onMessage(Message message) {
if (message instanceof TextMessage) {
System.out.println(((TextMessage) message).getText());
}
}
}
![Page 24: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/24.jpg)
Apache ActiveMQ
Open source JMS 1.1 message broker Clustering Multiple message stores TCP, UDP, NIO, SSL, VM connectivity OpenWire API for high performance Stomp API for easier implementation REST API Can be used as in-memory JMS provider
![Page 25: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/25.jpg)
Why AMQP, not JMS?
Bound to Java Other protocols (STOMP, e.t.c) do not offer all
the functionality of the broker Single standart for interoperability of brokers
(AMQP is Protocol, not API)
![Page 26: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/26.jpg)
Why JMS, not AMQP
More implementations Better support in Java world Being an API allows for custom protocol
implementations (VM connector)
![Page 27: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/27.jpg)
AMQP
Open standart protocol Support in all major languages Binary wire protocol (JMS defines API only) 1.0 version of protocol published 07 Oct 2011
![Page 28: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/28.jpg)
AMQP protocol
Defines how clients and brokers talk Data serialization, heartbeat Hidden inside client libraries
![Page 29: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/29.jpg)
AMQP model
Message broker - server User Connection – physical connection Channel – logical connection Exchanges – named entities, to which
messages are sent (may be durable or not) Queues – names entities, that store received
messages (may be exclusive)
![Page 30: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/30.jpg)
AMQP model
P - producer X - exchange C - consumer
![Page 31: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/31.jpg)
AMQP model
P/C – producer/consumer Ch – channel Conn – connection X - exchange
![Page 32: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/32.jpg)
AMQP message
Header + content body Immediate – message will be handled as
unroutable if there is no client waiting for it Expiration Priority Delivery mode
![Page 33: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/33.jpg)
AMQP bindings
Relationship between one queue and one exchange
Unconditional Conditional on fixed string Conditional on pattern match Conditional on content inspection Conditional on algorithmic comparison
![Page 34: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/34.jpg)
Fanout exchange
1:N message delivery pattern Bind a queue to the exchange and messages
sent to that exchange get delivered to all the bound queues
![Page 35: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/35.jpg)
Direct exchange
Queue binds to exchange with string key
Publisher sends message with key
Message is passed to the queue only if keys are equal
![Page 36: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/36.jpg)
AMQP working group Bank of America, N.A.
Barclays Bank PLC
Cisco Systems, Inc.
Credit Suisse
Goldman Sachs
JPMorgan Chase Bank & Co.
Microsoft Corporation
Novell
Progress Software
Red Hat, Inc.
Software AG
VMware, Inc.
![Page 37: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/37.jpg)
AMQP in Java world
Grails plug in Java client Scala / Lift support Spring AMQP project 1.0.0.RELEASE
(http://www.springsource.org/spring-amqp)
![Page 38: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/38.jpg)
Spring AMQP project
Similar to Spring JMS support AMQPTemplate MessageListener Transactions e.t.c
![Page 39: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/39.jpg)
Apache QPID
JMS interface for AMQP Message broker implemented in Java Version 0.12 :(
![Page 40: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/40.jpg)
AMQP future
ActiveMQ, HornetQ, e.t.c has plans to support AMQP
1.0 version?
![Page 41: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/41.jpg)
RabbitMQ
Leading implementation of AMQP Developed by SpringSource division of Vmware Full range of commercial support services Implemented in Erlang Clustering built-in
![Page 42: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/42.jpg)
RabbitMQ performance
We use it for login data processing Each user login in game = 1 message to the
queue Performance depends on
persistence/transactions enabled At 20k 1-kilobyte persistent messages per
second with sub-millisecond latency RabbitMQ was far from being a bottleneck
![Page 43: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/43.jpg)
Book to read
Hohpe, Gregor; Bobby Woolf (2003).
Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions.
ISBN 0-321-20068-3.
![Page 44: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/44.jpg)
Book to read
ActiveMQ in Action
Bruce Snyder, Dejan Bosanac and Rob Davies
ISBN 1933988940
![Page 45: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/45.jpg)
Book to read
RabbitMQ in Action
Alvaro Videla and Jason J.W. Williams
ISBN: 9781935182979
![Page 46: Messaging in Java](https://reader037.vdocument.in/reader037/viewer/2022100304/55510975b4c9057b478b5442/html5/thumbnails/46.jpg)
Questions?
?