JMS Tutorial

39
Overview Describe the concepts and some practical applications of messaging. Describe the concepts and basic structure of JMS. Write simple JMS messaging code using the publish and subscribe and point-to-point domains. Discuss advanced JMS issues such as guaranteed messaging, transactions, reliability, and recovery.

description

Good jms presentation

Transcript of JMS Tutorial

Page 1: JMS Tutorial

Overview

Describe the concepts and some practical applications of messaging.Describe the concepts and basic structure of JMS.Write simple JMS messaging code using the publish and subscribe and point-to-point domains.Discuss advanced JMS issues such as guaranteed messaging, transactions, reliability, and recovery.

Page 2: JMS Tutorial

Part 1: Enterprise Messaging and JMS

Page 3: JMS Tutorial

Enterprise messaging

Message-Oriented

Middleware

Application A

Messaging API

Messaging Clients

Application B

Messaging API

Messaging Clients

Page 4: JMS Tutorial

Tightly Coupled RPC

BusinessApplication A

RPCClient/Server

BusinessApplication D

RPCClient/Server

BusinessApplication B

RPCClient/Server

BusinessApplication C

RPCClient/Server

Requires n * (n-1) / 2 connections

Page 5: JMS Tutorial

JMS messaging

JMS Client

Message Server

JMS Client

JMS Client

JMS Client

JMS Client

JMS Client

JMS Client

Local "server"

JMS Client

Local "server"

JMS Client

Local "server"

JMS Client

Application A

Local "server"

Application B Application C Application D

Router

Decentralized message server (IP multicast)

Centralized message server(hub and spoke)

Page 6: JMS Tutorial

The J2EE platform

Client Application

JMS Server

EJB Container

Web Container

Application Client

Container

JDBC Database

J2EE Platform

Page 7: JMS Tutorial

Part 2: JMS Concepts and Coding

Page 8: JMS Tutorial

Wholesaler and retailer

Message ServerWholesaler

Retailer 1

Retailer 2

Update price(publish)

Order product(send)

Page 9: JMS Tutorial

JMS features

Standard Java APIMessage delivery modesTwo messaging modelsTransactionsReliability levelsPersistent messaging

Page 10: JMS Tutorial

JMS messaging domains

Publish and subscribeMany consumers per message

Point-to-pointOne consumer per message

Page 11: JMS Tutorial

Publish and subscribe: Supply chain management

PublisherPublisher SubscriberSubscriberTopicTopic

Page 12: JMS Tutorial

Point-to-point: Order and fulfillment

Goods ShippedGoods Shipped

Queue

Need SuppliesNeed Supplies

Sender Receiver

Page 13: JMS Tutorial

JMS components

Message Server

Message MessageDestination

JMS Client

Connection

Session

Producer

JMS Client

Connection

Session

Consumer

Page 14: JMS Tutorial

Connections and sessions

A connection connects to a message server.

You can create one or more sessions within a connection.

JMS Client

Connection

Session

Session

Session

Page 15: JMS Tutorial

Creating connections and sessions

Message Server

JNDI Store

JMS Client

Connection

Session

Session

Session

ConnectionFactory

create

ConnectionFactoriesDestinations

Page 16: JMS Tutorial

JMS message types

A stream of uninterpreted bytesBytesMessage

A stream of Java primitivesStreamMessage

A set of name/value pairs where values are Java primitivesMapMessage

A serializable Java objectObjectMessage

A standard Java stringTextMessage

No body Message

Message bodyMessage type

Page 17: JMS Tutorial

Creating a message

createStreamMessage( );

StreamMessage

TopicSession

createTextMessage( );

TextMessage

QueueSession

Page 18: JMS Tutorial

JMS message headers

Automatically assigned headers

JMSDestinationJMSDeliveryModeJMSMessageIDJMSTimestampJMSExpirationJMSRedeliveredJMSPriority

Developer-assigned headers

JMSReplyToJMSCorrelationIDJMSType

Page 19: JMS Tutorial

Producers, consumers, and destinations

Message ServerSend

messageConsumemessageDestination

Producer

Bind todestination

Consumer

Read message

Bind todestination

Page 20: JMS Tutorial

Creating destinations

Message Server

JNDI StoreJMS Client

Topic

Queue

ConnectionFactoriesDestinations lookup

Page 21: JMS Tutorial

Producing a message

create message

produce message

Session

MessageProducer

Message Destinationcreate

Page 22: JMS Tutorial

Consuming messages

Incoming messages

MessageListener

onMessage(message);

Message Server

acknowledge

MessageConsumer

Message receive();

Incoming messages

acknowledge

Page 23: JMS Tutorial

Asynchronous message delivery

Incoming messages

MessageListener

onMessage(message);Message Server

acknowledge

Page 24: JMS Tutorial

Using receive( )

Incoming messages

MessageConsumer

Message receive();Message Server

acknowledge

Page 25: JMS Tutorial

Filtering with message selector

Message Server

PublisherDepartments.Sales

SubscriberDepartments.SalesPipeline > 15000

JMSPriority = 2Pipeline = 20000

delivered

SubscriberDepartments.SalesJMSPriority > 5

not delivered

SubscriberDepartments.SalesJMSPriority >= 2 AND Pipeline > 10000

delivered

SubscriberDepartments.SalesPipeline > 20000

notdelivered

Page 26: JMS Tutorial

Browsing a queue

nextElement();

QueueBrowser

getEnumeration();

Message1

Message2

Message3

Message4

Enumeration

Page 27: JMS Tutorial

Accessing message content

Message

TextMessageMapMessage ObjectMessage StreamMessage BytesMessage

getText(); getObject();

get<type>(Name);

read<type>();

Page 28: JMS Tutorial

Part 3: Guaranteed messaging

Page 29: JMS Tutorial

Guaranteeing message delivery

DurableSubscriber

OrQueueReceiverProducer Message

Server

PERSISTENT message

Persistent store

Message removed after acknowledgment

Acknowledgemessage

Page 30: JMS Tutorial

Persistent and non-persistent messages

Message Server

Persistent store

Non-persistent message not guaranteed to survive provider failure

Persistent message guaranteed to survive provider failure

Page 31: JMS Tutorial

Acknowledgement modes

AUTO

CLIENT

DUPS_OK

I got another message!

It might be easy for you to tell me that for every message, but you are using a lot of bandwidth.

I received all messages up to this point.

Where have you been? I have been waiting to delete 50 messages.

Consumer Server

When you are lazy like this, I might have to deliver some messages more than once.

Okay, I might seem “lazy” to you, but it is not important to me if there are some duplicates.

Page 32: JMS Tutorial

Durable subscribers

DurableSubscriber1(connected)

Publisher Message Server

PERSISTENT message

Persistent storeUndelivered messages delivered to Subscriber2 when reconnected

DurableSubscriber2

(not connected)

Page 33: JMS Tutorial

Redelivered messages

Before message server receives acknowledgement:Message server has failed and restarted

ORConsumer has failed and restarted

ConsumerMessage Server

JMSRedelivered=true

Page 34: JMS Tutorial

Part 4: Transactions and recovery

Page 35: JMS Tutorial

Transacted session: Producer

ProducerMessage Server

Consumer

Before commit( )

Upon commit( )Message Server

Transacted session

Page 36: JMS Tutorial

Transacted session: Consumer

Message Server Consumer

Before commit( )

Upon commit( )Message Server Consumer

12Deliver messagesConsume messages

12

Acknowledge messagesDelete

messages if all recipients have acknowledged

Page 37: JMS Tutorial

Distributed transaction support

Message Server

JNDI Store

JMS Client

XAConnection

XASession

XASession

XASession

XAConnectionFactory

create

ConnectionFactoriesDestinations

Page 38: JMS Tutorial

Recovering from a lost connection

Client runtime

Message Server

ExceptionListener

onException()Reconnect

Page 39: JMS Tutorial

Understanding the Java Message Service: Conclusion