Java Messaging Service - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Java Messaging Service

Description:

A producer will create a message specifically designated for one consumer. ... Use Session and Destination objects to create Message Consumers and/or Producers. ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 35
Provided by: tcnj
Category:

less

Transcript and Presenter's Notes

Title: Java Messaging Service


1
Chapter 19, 20
  • Java Messaging Service

2
Whats Messaging
  • Process of communicating between client or server
    processes by creating, sending and receiving
    objects called a Message.
  • A Message is similar to a network packet, that
    contains transport information, plus a body that
    can be text, binary or an object.
  • What is needed
  • Message-Oriented-Middleware (MOM) is the
    infrastructure that supports messaging.
  • In JAVA, the Java Messaging Service (JMS) is the
    framework that is used to interface with MOM.

3
Messaging
  • Messaging can be implemented in two ways
  • Client-based messagingEach client implements a
    MOM that directly interfaces with the other
    clients through JMS.
  • Server-based messagingMOM is implemented in a
    centralized application server (called a
    Provider) that supports messaging. Clients, using
    JMS, will send and receive messages from other
    clients through this server.

4
Messaging
  • Servers (Message Providers) are very reliable
    ways of implementing a Messaging architecture.
    They will handle
  • Security of the messages
  • Load balancing and scalability
  • Persistence of messages
  • Other administrative or advanced services
  • Servers usually support a true distributed
    architecture. C/COM objects can send a message
    to an Enterprise Java Bean.

5
Messaging
  • Messaging is not synchronise. Clients sending a
    message do not need to block until the reply,
    like in RPCs. This is a leading advantage over
    conventional object to object communication.ExT
    ake a program that produces news feeds in real
    time (not putting them into a database for other
    clients to read later). This program will produce
    a message (or a bunch of messages - news) and
    send them to be distributed to any client that
    subscribed to that particular feed. The program
    doesnt care about clients that are down, or the
    speed of the delivery - thats the responsibility
    of the server.

6
Messaging Domains
  • Domains are how the delivery of messages take
    place. They can be categorized into
  • Point To Point (QUEUES)
  • Publish-Subscribe (TOPICS)
  • MOMs implement one or more of these categories.
  • Clients can play the role of either sender
    (producer), receiver (consumer) or both (___).

7
Point-to-Point Messaging
  • Very basic concept. A producer will create a
    message specifically designated for one consumer.
    Kind of how the US Post Office works (one piece
    of mail goes to a single address).
  • Implementation usually uses a Queued approach,
    where each client has one or more message queues
    that producers will populate.

8
Publish-Subscribe Messaging
  • Requires a client (or consumer) to Subscribe to a
    particular Topic that is the carrier of a message
    type.
  • A producer of that message type will blindly
    populate that topic with messages, not knowing
    who is reading them (who is subsribed).
  • This allows for many consumers to receive the
    same message (unlike point-to-point).

9
Java Messaging Servicehttp//java.sun.com/product
s/jms/
  • Part of the J2EE Edition
  • Providers should implement messaging as 100 pure
    Java. IE Be skeptical of Microsoft
    implementations!!
  • Defines a Destination as a Queue or Topic.
  • Uses a Connection Factory for creating/storing
    connections to a provider.
  • Supports Distributed Transactions.

10
Java Messaging Service
  • JMS defined two types of interfaces
  • Communication between the client and JMS
  • Communication between JMS and the Provider
    (messaging server).
  • The interfaces cannot simply be called by a java
    class - they must be implemented - usually by a
    providers set of JMS classes.
  • JMS uses other APIs within the J2EE framework,
    including JNDI, JTA and RMI.

11
JMS Destinations
  • Defined as a Provider-Independent representation
    of an address (or delivery point).
  • Destintations come in two classes - Queues and
    Topics, which both may be persistent.
  • Different providers may implement destinations
    differently, but it is all encapsulated and
    transparent to the client that is sending the
    message.

12
Connection Factories
  • These are how the client actually connects to the
    JMS Provider.
  • Its a JNDI implementation, where a client
    retrieves an instance of a JDNI object called a
    ConnectionFactory to a provider, and then
    establishes a connection from there.
  • Connection Factories are divided into the two
    types of classes
  • QueueConnectionFactory
  • TopicConnectionFactory

13
JMS and JNDI
  • Clients connecting to Factories or Destinations,
    do so indepedently of the JMS implementation.
    This provides portability between platforms and
    possibly alternative Providers.
  • This is done through Administered objects, which
    is an object that is retrieved using JNDI and
    provides a layer of abstraction between the
    client calls and the providers implementation of
    the JMS.

14
Connections
  • A client will establish a connection to a JMS
    Provider through the Connection Factory.
  • A client gets a Connection Factory object via
    JNDI, and uses this objects create() method to
    create a Connection object.
  • Like SQL errors throw SQLExceptions, JMS errors
    from the client throw - at the least - a
    JMSException, and must be caught during most
    types of activities that use the JMS objects.

15
Connections
  • Connections may or may not take a
    username/password. Implementation dependent by
    the provider.
  • A Connections data flow can be controlled by a
    start() and stop() method. Clients can then
    control when to receive data from a particular
    Connection.
  • Clients can also receive information about the
    Connection through a MetaData object.

16
Session
  • A Session is a single threaded context for
    producing and consuming messages.
  • Sessions use the Connection to send and receive
    serialized messages in a synchronized fashion. It
    is the Session itself that is sent across the
    wire!
  • Sessions also support distributed transactions.
    In this case, the provider will store all
    sessions received in a Block, until the
    transaction is commited. Then publish the all
    those subscribed - so the receiving clients will
    receive the entire block at once.

17
Sessions
  • Sessions use Acknowledgment as a way to guarantee
    delivery. Each message may be sent to a
    particular client many times until they
    acknowledge its receipt (or timeout).
  • Sessions provide an API for creating a Message
    object
  • createTextMessage()
  • createObjectMessage()
  • createBytesMessage() ...

18
Message Producers
  • The MessageProducer is responsible for sending
    the message.
  • Producers come in two flavors, depending on what
    type of Connection youre using
  • TopicProducer
  • QueueSender

19
Message Consumers
  • MessageConsumers are used by clients to receive
    messages.
  • Clients must establish a Destination from which
    it will listen for messages from a particular
    address.
  • Consumers also come in two styles, depending on
    what type of Destination object the client has
  • QueueReceiver
  • TopicSubscriber

20
Message Consumers
  • Messages can be received synchronisely or
    asynchronisely, depending on client
    implementation.
  • Synchronized message delivery usually involves
    running a separate thread to received the
    messages one at a time.
  • Asynchronized delivery requires the existence of
    a Message Listener on the client.

21
Message Objects
  • Each message object has the following high-level
    components
  • Header includes expiration date, priority,
    etc...
  • Properties specific message attrs. Not
    required.
  • Body content of the message. Binary, text, etc
  • When a Message is created, the actual subclass of
    the message is specified (which is inherited from
    the Message class.

22
Message Objects
  • The following subclasses are used to define what
    type of body the message will contain
  • StreamMessage sequence of objects primitives
  • MapMessage key-value pairs
  • TextMessage string values
  • ObjectMessage any serialized java object
  • BytesMessage byte values

23
Message Selectors
  • Used as Filters by client consumers to restrict
    delivery of particular types of messages, even if
    subscribed to that channel.
  • This concept allows for reducing network traffic,
    thus improving performance of the client -
    because its filtered on the server before ever
    reaching the client.

24
Message Selectors
  • The syntax message selectors are compliant with
    SQL92
  • price BETWEEN 10 and 100
  • JMSType carrier
  • phone LIKE 12
  • name IN (stephen, scott)

25
ReCapSteps To JMS Programming
  • Retrieve a ConnectionFactory (JNDI)
  • Retrieve any Destinations (JNDI)
  • Create a Connection object from Factory
  • Create a Session from the Connection object.
  • Use Session and Destination objects to create
    Message Consumers and/or Producers.
  • Send and Receive messages through those.

26
Point-to-Point Messaging
  • This type of messaging uses a Queue as a
    destination source, to deliver a message to a
    particular (and known client).
  • Each Consumer has its own unique Queue to
    receive messages from.
  • Each Provider can use the same Queue, thereby
    allowing multiple clients to send messages to a
    single source.
  • Queues may be persistence, depending on the JMS.

27
QueueConnectionFactory
  • Queues are registered as a JNDI service, and are
    used through a Connection Factory.
  • QueueFactories return a Queue Connection, used by
    both Consumers and Providers.
  • Example
  • Qfactory (QueueConnectionFactory)
    context.lookup(QueyeConnectionFactory)
  • Qconnection Qfactory.createQueueConnection()

28
QueueConnection
  • These connections are resposible for
    point-to-point communication between
    clients/servers.
  • They are defined by one commonly used method
    called CreateQueueSession ().
  • This returns a Queues SESSION, that will be used
    to Send and Receive messages between two points.

29
Queue Sessions
  • Used to create Queues, and their Producers and
    Consumers using the following methods
  • createQueue() Creates a Queue.
  • createSend() Creates a message producer called a
    QueueSender.
  • createReceiver() Creates a message consumer
    called a QueueReceiver. Can also limit messates
    by a messageSelector object.
  • createBrowser() Creates a QueueBrowser object,
    that can be used to display information
    messages in the Queue.

30
Senders and Receivers
  • Queue Senders and Receivers will obtain a Queue
    by either
  • Creating one - then using it
  • Looking up one via the JNDI context
  • Queue (Queue) JMSystem.context.Lookup(MyQueue
    )
  • sender QSession.createSender(Queue)
  • receiver QSession.createReceiver(Queue)

31
Senders and Receivers
  • // Sending a message
  • Message msg qsession.createTextMessage(Hello
    JMS)
  • sender.send(msg)
  • QSession.commit() // if using a transaction
  • // Receiving a message in a separate thread
  • while (true)
  • msg receiver.receive(interval) //1000 1
    second
  • if ( msg ! null) System.out.println(Message
    msg)

32
Publish-Subscribe Messaging
  • This model is very similar to the Point-To-Point
    model, using Topics instead of Queues.
  • Connection Factories for Topics are used to
    create TopicConnections, which are then used to
    create a Topic Session.
  • From a TopicSession, a Topic, Publisher or
    Subscriber can be created, as well as the ability
    to UnSubscribe to a particular Topic.

33
Publishers and Subscribers
  • topic (Topic) JMSystem.context.lookup(JavaTopic
    )
  • sender TSession.createPublisher(topic)
  • receive TSession.createSubscriber(topic)
  • // Publish a message to a Topic
  • Message msg TSession.createTextMessage(Java
    Pointers?)
  • sender.publish(msg)
  • // Subscribe to a Topic and Receive Messages
  • while (true)
  • msg receiver.receive(interval) //1000 1
    second
  • if ( msg ! null) System.out.println(Message
    msg)

34
Durable Subscribers
  • Normally, clients only receive messages when they
    are actively subscribed to a channel, and
    running. If they shut down and restart later, any
    messages published in their absence will be lost.
  • For clients wishing to receive all messages, even
    when theyre down, they must create a durable
    subscription. The Server will serialize the
    messages when the client is unavailable, and send
    them to the client once back online.
Write a Comment
User Comments (0)
About PowerShow.com