16 Developing MessageDriven Beans Copyright 2004 Oracle All
16 Developing Message-Driven Beans Copyright © 2004, Oracle. All rights reserved.
Objectives After completing this lesson, you should be able to do the following: • Describe the need for a message-oriented middleware (MOM) type • Define Java Message Service (JMS) • Describe and create a message-driven bean (MDB) • Differentiate between a stateless session Enterprise Java. Bean and a message-driven bean • Configure JMS resource provider 16 -2 Copyright © 2004, Oracle. All rights reserved.
Overview of Messaging Systems • • • 16 -3 Messaging is a way for software components and/or applications to communicate. Messaging systems are peer-to-peer in nature. Clients can send or receive messages from other clients. JMS was designed to allow applications to create, send, and receive messages synchronously or asynchronously. Java Message Service allows access to existing MOM services, such as the OC 4 J in-memory JMS server. Copyright © 2004, Oracle. All rights reserved.
Types of Message Consumption Messaging systems are inherently asynchronous, but JMS allows messages to be consumed in two ways: • Synchronously – Receiver thread blocks until a message can be consumed or times out. – Receiver acknowledges message receipt. – There is tightly coupled interaction. • Asynchronously – Receiver listens for messages. When a message is delivered, the listener message handler is invoked. – Receiver thread does not block. – There is loosely coupled interaction. 16 -4 Copyright © 2004, Oracle. All rights reserved.
Java Message Service (JMS) • • Is a J 2 EE standard messaging system Can guarantee message delivery Supports messaging across multiple platforms Supports two models of communication – Point-to-point model – Publish-and-subscribe model Client application Messaging API JMS client 16 -6 MOM Copyright © 2004, Oracle. All rights reserved. JMS client
JMS Application Architecture The main components of a JMS application are: • JMS clients • JMS provider • Administered objects (in the Java Naming and Directory Interface [JNDI] namespace) – Connection factories – Destinations (queues or topics) Producer Consumer Application JMS Application Queue/Topic JMS client 16 -7 JMS provider Copyright © 2004, Oracle. All rights reserved. JMS client
Point-to-Point Model • • Senders send messages to virtual destinations. There is only one receiver for a message from a queue. S 1 S 2 M 1 Queue 1 M 2 16 -8 R 1 R 2 M 3 Queue 2 Messaging server Queue sender M* - Message M 1 M 3 Queues (virtual destinations) Copyright © 2004, Oracle. All rights reserved. R 3 Queue receiver
Publish-and-Subscribe Model • • Publishers send messages to virtual destinations. One or more subscribers receive the messages. M 1 P 1 M 1 Topic 1 S 2 P 2 M 2 Topic publisher M* - Message 16 -9 Topic 2 Messaging server S 3 Topics (virtual destinations) Topic subscriber Copyright © 2004, Oracle. All rights reserved.
Using JMS Interfaces The JMS API service uses the following objects and interfaces that are located in the javax. jms package: • JMS-administered objects: – Connection Factory: Creates connections – Destination: Target/source of messages • • • 16 -10 Connection: Creates sessions Session: Creates a message producer or consumer Message Producer: Sends to a destination Message Consumer: Receives from a destination Message: The body of the information to be communicated Copyright © 2004, Oracle. All rights reserved.
JMS Message Structure JMS messages are composed of the following parts: • A header containing a common set of header fields – Field examples: JMSMessage. ID and JMSDestination – Field values are used by clients and providers for identification and routing information. • • 16 -12 Properties for application-defined property values, which can be used for message filtering purposes A body for the contents of the message, whose contents can be structured as a Stream. Message, Map. Message, Text. Message, Object. Message, or Byte. Message Copyright © 2004, Oracle. All rights reserved.
Sending a Message to a Queue The code steps to send a message to a JMS queue are: 1. Connect to the naming server using JNDI. 2. Obtain a Queue. Connection. Factory. 3. Obtain the name and location of Queue. 4. Open a Queue. Connection and start it. 5. Create a Queue. Session. 6. Create a Queue. Sender from the session. 7. Create the Message and set the header, body, and properties. 8. Send the message and close resources. 16 -13 Copyright © 2004, Oracle. All rights reserved.
Receiving Messages The following are the steps to receive a message from a topic for non-MDB applications: 1. Connect to the JNDI naming service. 2. Look up for the connection factory and the source of the message. 3. Create a Topic. Connection object. 4. Create a Topic. Session object. 5. Create a Topic. Subscriber (or queue receiver) to receive a message. 6. Subscribe (or receive) a message. 7. Write code to process the message contents. 16 -15 Copyright © 2004, Oracle. All rights reserved.
Asynchronous Message Delivery • • 16 -16 In an asynchronous messaging model, the consumer implements a Message. Listener interface. The Message. Listener interface declares an on. Message() method and notifies the consumer of the arrival of messages. MDBs are J 2 EE implementations for message listeners. A sender is notified when the MDB processes the message. Copyright © 2004, Oracle. All rights reserved.
Message-Driven Beans MDBs: • Are programmed for receiving and processing asynchronous messages through the JMS Destination • Are stateless, server-side, transaction-aware components • Are not accessible directly by any client • Do not contain home and component interfaces • Are triggered by a container when a message arrives • Implement the javax. ejb. Message. Driven. Bean and javax. jms. Message. Listener interfaces 16 -17 Copyright © 2004, Oracle. All rights reserved.
MDB Architecture OC 4 J Client MDB JMS resource provider Queue/Topic 16 -18 Copyright © 2004, Oracle. All rights reserved.
Associating JMS Resources with an MDB Use the J 2 EE and OC 4 J deployment descriptors to specify the JMS resources associated with an MDB. • ejb-jar. xml: <message-driven>. . . <ejb-name>Message. Processor</ejb-name>. . . <message-driven-destination> <destination-type>javax. jms. Queue</destination-type> </message-driven-destination> </message-driven> • orion-ejb-jar. xml: <enterprise-beans> <message-driven-deployment max-instances="-1" name="Message. Processor" connection-factorylocation="jms/Queue. Connection. Factory" destinationlocation="jms/demo. Queue" min-instances="0"/> </enterprise-beans> 16 -19 Copyright © 2004, Oracle. All rights reserved.
State Diagram of an MDB Does not exist Container invokes Class. new. Instance, set. Message. Driven. Context(context) and ejb. Create() Container invokes ejb. Remove(…) Method-ready pool Container invokes on. Message() 16 -20 Copyright © 2004, Oracle. All rights reserved.
Developing MDBs 1. Configure Oracle Application Server 10 g Containers for J 2 EE (OC 4 J) with the JMS provider details. 2. Implement the bean class. 3. Configure deployment descriptors: a. Destination type for the bean in ejb-jar. xml b. Connection factory and destination JNDI locations in orion-ejb-jar. xml 4. Create the EJB JAR file that contains the bean and the deployment descriptors. 5. Create the. ear file and deploy the bean. 16 -22 Copyright © 2004, Oracle. All rights reserved.
Interfaces to Be Implemented for MDBs • Message. Driven. Bean interface: package javax. ejb; public interface Message. Driven. Bean extends javax. ejb. Enterprise. Bean { public void set. Message. Driven. Context (Message. Driven. Context context) throws EJBException; public void ejb. Remove() throws EJBException; • Message. Listener interface: package javax. jms; public interface Message. Listener { public void on. Message(Message message); } 16 -23 Copyright © 2004, Oracle. All rights reserved.
Implementing an MDB Class The Message. Logger MDB example: package com. evermind. logger; import javax. ejb. *; import javax. jms. *; … public class Message. Logger implements Message. Driven. Bean, Message. Listener { private Message. Driven. Context message. Context; public void ejb. Create() { } public void ejb. Remove() { } public void set. Message. Driven. Context( Message. Driven. Context context) { this. message. Context = context; } 16 -24 Copyright © 2004, Oracle. All rights reserved.
Receiving Messages in an MDB Class The on. Message() method processes the received message. public void on. Message(Message msg){ Text. Message msg. Text=null; try { msg. Text = (Text. Message) msg; String txt = (msg. Text). get. Text(); System. out. println("Message received=" + txt); } catch (Exception e) { throw new Runtime. Exception("on. Message error"); } } 16 -25 Copyright © 2004, Oracle. All rights reserved.
Creating the Deployment Descriptor MDB-related elements in the ejb-jar. xml file: <message-driven> <ejb-name>. . . </ejb-name> <ejb-class>. . . </ejb-class> <transaction-type>. . . </transaction-type> <message-selector>. . . </message-selector> <acknowledge-mode>. . . </acknowledge-mode> <message-driven-destination> <destination-type>. . . </destination-type> </message-driven-destination> <ejb-ref>. . . </ejb-ref> <security-identity>. . . </ security-identity> <resource-ref>. . . </resource-ref> </message-driven> 16 -26 Copyright © 2004, Oracle. All rights reserved.
ejb-jar. xml: Example. . . <enterprise-beans> <message-driven> <ejb-name>Message. Logger</ejb-name> <ejb-class>btier. impl. Message. Logger</ejb-class> <acknowledge-mode>Auto-acknowledge </acknowledge-mode> <message-driven-destination> <destination-type> javax. jms. Queue </destination-type> <subscription-durability>Durable </subscription-durability> </message-driven-destination> </message-driven>. . . 16 -28 Copyright © 2004, Oracle. All rights reserved.
Mapping in OC 4 J-Specific Deployment Descriptor In the orion-ejb-jar. xml file, associate a JMS Destination with the MDB in the <message-drivendeployment> element by using the following attributes: • Name: MDB name as defined in the <ejb-name> • connection-factory-location: JMS Destination Connection Factory • destination-location: JMS Destination • subscription-name: Subscription name (required only if the JMS Destination is a topic) 16 -29 Copyright © 2004, Oracle. All rights reserved.
orion-ejb-jar. xml: Example <enterprise-beans> <message-driven-deployment name="Message. Logger" connection-factory-location= "jms/Queue. Connection. Factory"> destination-location= "jms/demo. Queue" </message-driven-deployment>. . . </enterprise-beans> 16 -30 Copyright © 2004, Oracle. All rights reserved.
Creating an MDB with JDeveloper 2 1 3 16 -31 Copyright © 2004, Oracle. All rights reserved.
Creating an MDB with JDeveloper ejb-jar. xml 16 -32 Copyright © 2004, Oracle. All rights reserved.
Creating an MDB with JDeveloper Map the destination details in orion-ejb-jar. xml 16 -33 Copyright © 2004, Oracle. All rights reserved.
Testing the MDB 1. Deploy the session and the MDBs. 2. Create a client to invoke the message-sending functionality. 3. Run the client application to send the message to the JMS Destination. 4. Observe the output in the run-time environment. 16 -34 Copyright © 2004, Oracle. All rights reserved.
Summary In this lesson, you should have learned how to: • Describe the different types of MOM: – Point-to-point – Publish-and-subcribe (pub/sub) • • 16 -35 Create an MDB Compare a stateless session EJB and an MDB Describe JMS Configure a JMS resource provider in the OC 4 J jms. xml file Copyright © 2004, Oracle. All rights reserved.
Practice 16 -1: Overview This practice covers the following topics: • Creating a simple MDB to accept a message from an OC 4 J JMS queue, and writing the message to a log table by using an entity bean • Configuring the message-bean deployment descriptor to use an OC 4 J JMS resource • Writing and configuring a Java. Server Pages (JSP) application to send a message from an HTML form to the OC 4 J JMS queue 16 -36 Copyright © 2004, Oracle. All rights reserved.
- Slides: 31