Java Message Service JMS - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Java Message Service JMS

Description:

web-app xmlns='http://java.sun.com/xml/ns/j2ee' ... CMT doesn't require you to handle transaction logic, pushes transaction settings ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 33
Provided by: Tolg1
Category:
Tags: jms | java | message | service

less

Transcript and Presenter's Notes

Title: Java Message Service JMS


1
Java Message Service (JMS)
  • Karteek Suvarna
  • Hardik Vaishnav
  • Nagaraju Medavarapu

2
Introduction
  • A specification that describes a common way for
    Java programs to create, send, receive and read
    distributed enterprise messages.
  • loosely coupled communication
  • Asynchronous messaging
  • Reliable delivery
  • J2EE provides the following mechanisms for
    deferring work to the background
  • JMS (Java Message Service)
  • Message-Driven Beans (MDBs)

3
JMS
  • The JMS API provides a vendor-neutral standard
    programming interface for creating, sending, and
    receiving messages. JMS enables Java applications
    to asynchronously exchange messages containing
    data and events. JMS 1.1 is part of the J2EE 1.4
    standard.

4
MDB
  • Message-Driven Beans (MDBs) are EJBs that receive
    and process JMS messages. MDBs don't maintain
    state between invocations, so they resemble
    Stateless Session Beans. But MDBs differ from
    Stateless Session Beans
  • MDBs differ from Stateless Session Beans in that
  • A client is completely decoupled from an MDBthe
    client sends a JMS message and the MDB picks up
    the message.
  • After sending a JMS asynchronously, the client
    doesn't wait for the MDB to receive the message.
    The client continues executing other business
    logic.

5
JMS Architecture
  • JMS Provider A messaging system that implements
    the JMS specification. A JMS Provider, also known
    as a JMS Server, routes messages between clients.
  • Clients Java applications that send or receive
    JMS messages. A message sender is called the
    Producer, and the recipient is called a Consumer.
  • Messages Messages contain data or events
    exchanged between Producers and Consumers.
  • Destinations A Producer sends a message to a JMS
    Destination (either a Queue or a Topic), and the
    Consumer(s) listening on the JMS Destination
    receives the message.

6
JMS Messaging Model
7
Core JMS API
  • Message Holds business data and routing
    information. Although you'll find several types
    of Messages, most of the time you'll use either a
    TextMessage (that contains textual data in its
    body) or an ObjectMessage (that holds
    serializable objects in its body).
  • Destination Holds messages sent by the Producer
    to be received by the Consumer(s). A Destination
    is either a Queue or a Topic that the JMS Server
    manages on behalf of its clients.
  • Connection Enables a JMS client to send or
    receive Messages. Use a Connection to create one
    or more Sessions.
  • ConnectionFactory A ConnectionFactory is either
    a QueueConnectionFactory or it is a
    TopicConnectionFactory, depending on the
    messaging model, and it exists to create
    Connections.
  • SessionA Session creates Producers, Consumers,
    and Messages. A Publish-Subscribe application
    uses a TopicSession, and a Point-to-Point
    application uses a QueueSession. Sessions are
    single-threaded.

8
Application
  • JAW Motors application needs to run a credit
    check on potential customers to pre-qualify them
    for car loans as part of the auto buying process.
  • We need a way to defer the credit verification
    process to the background so the user can
    continue using the web site.

9
JMS and Its Relationship to J2EE Transactions
  • When you use JMS, you can participate in either
    distributed or local transactions
  • Local transaction A local JMS transaction uses
    programmatic transaction management and only
    includes JMS messages in its transaction context.
  • Distributed transaction A distributed
    transaction (or two-phase commit) uses the JTA
    for container-managed transactions that include
    JMS and other resources, including JDBC, EJBs

10
Sending Messages with JMS
  • To send JMS messages from the JAW Motors
    application, do the following
  • Upgrade the web site
  • Add a "Run Credit Check" link and form.
  • Add a "Run Credit Check" action to the Controller
    Servlet.
  • Add JMS
  • Create an object that holds the user's credit
    information to send as a JMS message.
  • Write a utility class to encapsulate sending a
    JMS message.
  • Add JMS-based JNDI reference settings to the
    Web-based deployment descriptors (web.xml and
    jboss-web.xml).
  • Automate JMS-based JNDI reference settings with
    XDoclet.
  • Deploy a JMS Queue on JBoss with an MBean.

11
Upgrade the Site Running a Credit Check
  • We need to add a "Run Credit Check" link to the
    JAW Motors homepage

12
JAW Motors Run Credit Check page
  • When the user clicks on the "Run Credit Check"
    link, the Controller routes them to the Run
    Credit Checkpage

13
ControllerServlet.java
14
create a message
15
Sending message
  • pressing the "Submit Credit Info" from the "Run
    Credit Check" form invokes the Controller
    Servlet's runCreditCheck action. The Controller
    Servlet stores the user's credit information in
    the CreditCheckReqDTO and calls
    JmsProducer.sendMessage( ) to send the credit
    data as an asynchronous JMS message. After
    sending the message, the Controller Servlet
    returns the user to the main page.
  • We've encapsulated the user's credit information
    in a CreditCheckRequestDTO

16
Sending message
17
ServiceLocator.java
18
Web-Based Deployment DescriptorsWeb.xml
  • ltres-ref-namegt is the JNDI name for each
    resourcejavacomp/env/jms/CreditCheckQueue and
    javacomp/env/jms/MyXAQueueConnectionFactory.
  • The ltres-typegt for the CreditCheckQueue is a JMS
    Queue, and javax.jms.Queue is its fully qualified
    class name.
  • The ltres-typegt for the Connection Factory is a
    JMS Queue Connection Factoryjavax.jms.QueueConnec
    tionFactory

19
Web.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltweb-app xmlns"http//java.sun.com/xml/ns/j2ee"
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
    ce"
  • xsischemaLocation"http//java.sun.com/xml/ns/j2e
    e
  • http//java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version"2.4"gt
  • ...
  • ltresource-ref gt
  • ltres-ref-namegtjms/CreditCheckQueuelt/res-ref-
    namegt
  • ltres-typegtjavax.jms.Queuelt/res-typegt
  • ltres-authgtContainerlt/res-authgt
  • lt/resource-refgt
  • ltresource-ref gt
  • ltres-ref-namegtjms/MyXAQueueConnectionFactory
    lt/res-ref-namegt
  • ltres-typegtjavax.jms.QueueConnectionFactorylt/
    res-typegt
  • ltres-authgtContainerlt/res-authgt
  • lt/resource-refgt
  • ...

20
jboss-web.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web
    Application 2.4//EN"
  • "http//www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd"gt
  • ltjboss-webgt
  • ...
  • ltresource-refgt
  • ltres-ref-namegtjms/CreditCheckQueuelt/res-ref-
    namegt
  • ltjndi-namegtqueue/CreditCheckQueuelt/jndi-name
    gt
  • lt/resource-refgt
  • ltresource-refgt
  • ltres-ref-namegtjms/MyXAQueueConnectionFactory
    lt/res-ref-namegt
  • ltjndi-namegtjava/JmsXAlt/jndi-namegt
  • lt/resource-refgt
  • ...
  • lt/jboss-webgt

21
jboss-web.xml
  • ltres-ref-namegt is the JNDI name for each
    resource.
  • The textual value of each ltres-ref-namegt element
    in jboss-web.xml MUST match the value of the
    ltres-ref-namegt in web.xml. The Queue's
    ltjndi-namegt is the local JBoss-specific JNDI name
    that JBoss uses to deploy a JMS Queue.
  • The Queue Connection Factory's ltjndi-namegt is the
    local JBoss-specific JNDI name that JBoss uses to
    deploy a JMS QueueConnectionFactory

22
Message-Driven Beans (MDBs)
  • A Message-Driven Bean (MDB) is an EJB whose sole
    purpose is to consume JMS messages. When a JMS
    producer sends a message to a JMS destination,
    the MDB listening on that destination receives
    the message and processes it. MDBs are pooled,
    stateless
  • An MDB implements the JMS MessageListener
    interface its onMessage( ) method processes the
    message received from the JMS destination.
  • Pooling enables concurrent behavior, so several
    instances of an MDB would run in parallel if
    multiple messages are in the queue or topic.

23
Writing an MDB for JAW
  • In the JAW Motors application, the
    CreditCheckProcessor is the MDB
  • Consumes a JMS ObjectMessage that contains a
    CreditCheckRequestDTO
  • The onMessage( ) method consumes a JMS message
    that contains the user's credit information, but
    we have to pull the CreditCheckRequestDTO out of
    the message before using it.
  • we pass the CreditCheckRequestDTO to our external
    credit verification service. CreditVerificationSer
    vice.verifyCredit( )
  • it returns a String value that tells whether the
    credit check passed or failed.

24
CreditCheckProcessorBean.java
25
MDB Transaction Settings
  • Message-Driven Bean has two ways to manage
    transactions
  • 1) Container-Managed Transactions (CMT)
  • 2) Bean-Managed Transactions (BMT).
  • BMT requires extra Java coding to handle
    transaction logic and forces the developer to
    manage the transaction
  • CMT doesn't require you to handle transaction
    logic, pushes transaction settings into
    deployment descriptors.
  • -uses JBoss transaction services
  • There's no need to specify a transaction setting
    in the CreditCheckProcessor MDB because it
    doesn't participate in a transaction. The Bean's
    onMessage( ) method only sends an email message
    and doesn't do anything transactional (like
    updating a database).

26
Deploying an MDB
  • deploy it by adding information about the EJB
    (meta-data) to the J2EE standard (ejb-jar.xml)
    and JBoss (jboss.xml) EJB deployment descriptors.
  • The new ltmessage-drivengt element describes.
  • - About the bean class.
  • - That it uses container-managed transactions
    (CMT).
  • - That its Acknowledgment mode is
    Auto-acknowledge.
  • - That it listens on a non-durable Queue

27
ejb-jar.xml
28
Jboss.xml
29
Deploying an MDB
  • In the ejb-jar.xml file you can only indicate
    that your MDB listens on a Queue, but there are
    no elements that associate the CreditCheckProcesso
    r MDB with the CreditCheckQueue. Since specifying
    the Queue name isn't part of the EJB
    specification
  • we use the jboss.xml descriptor to associate an
    MDB with a JMS Destination. The ltmessage-drivengt
    element's ltdestination-jndi-namegt sub-element
    tells JBoss that the CreditCheckProcessor MDB
    listens on and receives messages from the
    CreditCheckQueue

30
Automating MDB Deployment with XDoclet
  • The _at_ejb.bean XDoclet tag generates the
    CreditCheckProcessor MDB's ltmessage-drivengt
    element and all its subelements in ejb-jar.xml.
  • The _at_jboss.destination-jndi-name XDoclet tag
    generates the corresponding ltmessage-drivengt
    element in jboss.xml that indicates that the
    CreditCheckProcessor MDB listens for messages on
    the CreditCheckQueue

31
CreditCheckProcessorBean.java
  • /
  • _at_ejb.bean name"CreditCheckProcessor"
  • display-name"CreditCheckProcessorMDB"
  • acknowledge-mode"Auto-acknowledge"
  • destination-type"javax.jms.Queue"
  • subscription-durability"NonDurable"
  • transaction-type"Container"
  • _at_jboss.destination-jndi-name
  • name"queue/CreditCheckQueue"
  • /
  • public class CreditCheckProcessorBean implements
    MessageDrivenBean, MessageListener
  • ...

32
Questions?
Write a Comment
User Comments (0)
About PowerShow.com