Download presentation
Presentation is loading. Please wait.
Published byLuis Hensley Modified over 11 years ago
1
16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans
2
16-2 Copyright © 2005, 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 JavaBean and a message-driven bean Configure JMS resource provider
3
16-3 Copyright © 2005, Oracle. All rights reserved. Overview of Messaging Systems 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 OC4J in-memory JMS server.
4
16-4 Copyright © 2005, 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.
5
16-5 Copyright © 2005, Oracle. All rights reserved.
6
16-6 Copyright © 2005, Oracle. All rights reserved. Java Message Service (JMS) Is a J2EE 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 MOM JMS client
7
16-7 Copyright © 2005, Oracle. All rights reserved. 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) JMS provider Queue/Topic JMS client JMS Application Consumer JMS client JMS Application Producer
8
16-8 Copyright © 2005, Oracle. All rights reserved. Point-to-Point Model Senders send messages to virtual destinations. There is only one receiver for a message from a queue. Queue sender M* - Message Queues (virtual destinations) Queue receiver R3 R2 M1 M2 M3 Messaging server M1 M3 Queue1 Queue2 M2 S1 S2 R1
9
16-9 Copyright © 2005, Oracle. All rights reserved. Publish-and-Subscribe Model Publishers send messages to virtual destinations. One or more subscribers receive the messages. Topic subscriber S3 S2 S1 M1 M2 M1 M2 P2 Topic1 P1 Topics (virtual destinations) Topic publisher M* - Message Messaging server Topic2
10
16-10 Copyright © 2005, 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 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
11
16-11 Copyright © 2005, Oracle. All rights reserved.
12
16-12 Copyright © 2005, 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: JMSMessageID and JMSDestination –Field values are used by clients and providers for identification and routing information. 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 StreamMessage, MapMessage, TextMessage, ObjectMessage, or ByteMessage
13
16-13 Copyright © 2005, 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 QueueConnectionFactory. 3.Obtain the name and location of Queue. 4.Open a QueueConnection and start it. 5.Create a QueueSession. 6.Create a QueueSender from the session. 7.Create the Message and set the header, body, and properties. 8.Send the message and close resources.
14
16-14 Copyright © 2005, Oracle. All rights reserved.
15
16-15 Copyright © 2005, 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 TopicConnection object. 4.Create a TopicSession object. 5.Create a TopicSubscriber (or queue receiver) to receive a message. 6.Subscribe (or receive) a message. 7.Write code to process the message contents.
16
16-16 Copyright © 2005, Oracle. All rights reserved. Asynchronous Message Delivery In an asynchronous messaging model, the consumer implements a MessageListener interface. The MessageListener interface declares an onMessage() method and notifies the consumer of the arrival of messages. MDBs are J2EE implementations for message listeners. A sender is not notified when the MDB processes the message.
17
16-17 Copyright © 2005, 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.MessageDrivenBean and javax.jms.MessageListener interfaces
18
16-18 Copyright © 2005, Oracle. All rights reserved. MDB Architecture OC4J MDB JMS resource provider Queue/Topic Client
19
16-19 Copyright © 2005, Oracle. All rights reserved. Associating JMS Resources with an MDB Use the J2EE and OC4J deployment descriptors to specify the JMS resources associated with an MDB. ejb-jar.xml: orion-ejb-jar.xml:... MessageProcessor... javax.jms.Queue
20
16-20 Copyright © 2005, Oracle. All rights reserved. State Diagram of an MDB Does not exist Method-ready pool Container invokes Class.newInstance, setMessageDrivenContext(context) and ejbCreate() Container invokes ejbRemove(…) Container invokes onMessage()
21
16-21 Copyright © 2005, Oracle. All rights reserved.
22
16-22 Copyright © 2005, Oracle. All rights reserved. Developing MDBs 1.Configure Oracle Application Server 10g Containers for J2EE (OC4J) 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.
23
16-23 Copyright © 2005, Oracle. All rights reserved. Interfaces to Be Implemented for MDBs MessageDrivenBean interface: MessageListener interface: package javax.ejb; public interface MessageDrivenBean extends javax.ejb.EnterpriseBean { public void setMessageDrivenContext (MessageDrivenContext context) throws EJBException; public void ejbRemove() throws EJBException; package javax.jms; public interface MessageListener { public void onMessage(Message message); }
24
16-24 Copyright © 2005, Oracle. All rights reserved. Implementing an MDB Class The MessageLogger MDB example: package com.evermind.logger; import javax.ejb.*; import javax.jms.*; … public class MessageLogger implements MessageDrivenBean, MessageListener { private MessageDrivenContext messageContext; public void ejbCreate() { } public void ejbRemove() { } public void setMessageDrivenContext( MessageDrivenContext context) { this.messageContext = context; }
25
16-25 Copyright © 2005, Oracle. All rights reserved. Receiving Messages in an MDB Class The onMessage() method processes the received message. public void onMessage(Message msg){ TextMessage msgText=null; try { msgText = (TextMessage) msg; String txt = (msgText).getText(); System.out.println("Message received=" + txt); } catch (Exception e) { throw new RuntimeException("onMessage error"); }
26
16-26 Copyright © 2005, Oracle. All rights reserved. Creating the Deployment Descriptor MDB-related elements in the ejb-jar.xml file:.........
27
16-27 Copyright © 2005, Oracle. All rights reserved.
28
16-28 Copyright © 2005, Oracle. All rights reserved. ejb-jar.xml : Example... MessageLogger btier.impl.MessageLogger Auto-acknowledge javax.jms.Queue Durable...
29
16-29 Copyright © 2005, Oracle. All rights reserved. Mapping in OC4J-Specific Deployment Descriptor In the orion-ejb-jar.xml file, associate a JMS Destination with the MDB in the element by using the following attributes: Name : MDB name as defined in the connection-factory-location : JMS Destination Connection Factory destination-location : JMS Destination subscription-name : Subscription name (required only if the JMS Destination is a topic)
30
16-30 Copyright © 2005, Oracle. All rights reserved. orion-ejb-jar.xml : Example <message-driven-deployment name="MessageLogger" connection-factory-location= "jms/QueueConnectionFactory"> destination-location= "jms/demoQueue"...
31
16-31 Copyright © 2005, Oracle. All rights reserved. Creating an MDB with JDeveloper 1 2 3
32
16-32 Copyright © 2005, Oracle. All rights reserved. Creating an MDB with JDeveloper ejb-jar.xml
33
16-33 Copyright © 2005, Oracle. All rights reserved. Creating an MDB with JDeveloper Map the destination details in orion-ejb-jar.xml. orion-ejb-jar.xml
34
16-34 Copyright © 2005, 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.
35
16-35 Copyright © 2005, 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) Create an MDB Compare a stateless session EJB and an MDB Describe JMS Configure a JMS resource provider in the OC4J jms.xml file
36
16-36 Copyright © 2005, Oracle. All rights reserved. Practice 16-1: Overview This practice covers the following topics: Creating a simple MDB to accept a message from an OC4J JMS queue, and writing the message to a log table by using an entity bean Configuring the message-bean deployment descriptor to use an OC4J JMS resource Writing and configuring a JavaServer Pages (JSP) application to send a message from an HTML form to the OC4J JMS queue
37
16-37 Copyright © 2005, Oracle. All rights reserved.
38
16-38 Copyright © 2005, Oracle. All rights reserved.
39
16-39 Copyright © 2005, Oracle. All rights reserved.
40
16-40 Copyright © 2005, Oracle. All rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.