Top Banner
16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans
31

16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

Mar 27, 2015

Download

Documents

Luis Hensley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16Copyright © 2005, Oracle. All rights reserved.

Developing Message-Driven Beans

Page 2: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

Page 3: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 4: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 5: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

MOMJMS client JMS client

Page 6: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

JMSApplication

Producer

Page 7: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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 senderM* - Message

Queues (virtual destinations)

Queue receiver

R3

R2

M1

M2

M3

Messaging server

M1

M3

Queue1

Queue2

M2

S1

S2

R1

Page 8: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

S1M1

M2

M1

M2P2

Topic1P1

Topics (virtual destinations)

Topic publisherM* - Message

Messaging server

Topic2

Page 9: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

Page 10: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

Page 11: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 12: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 13: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 14: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

Page 15: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16-18 Copyright © 2005, Oracle. All rights reserved.

MDB Architecture

OC4J

MDB

JMS resourceprovider

Queue/Topic

Client

Page 16: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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:<enterprise-beans> <message-driven-deployment max-instances="-1" name="MessageProcessor" connection-factory-location="jms/QueueConnectionFactory" destination-location="jms/demoQueue" min-instances="0"/> </enterprise-beans>

<message-driven> ...<ejb-name>MessageProcessor</ejb-name> ... <message-driven-destination>

<destination-type>javax.jms.Queue</destination-type> </message-driven-destination> </message-driven>

Page 17: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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 invokesejbRemove(…)

Container invokes onMessage()

Page 18: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 19: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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);

}

Page 20: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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;

}

Page 21: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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");}

}

Page 22: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16-26 Copyright © 2005, 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>

Page 23: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16-28 Copyright © 2005, Oracle. All rights reserved.

ejb-jar.xml: Example

...<enterprise-beans> <message-driven> <ejb-name>MessageLogger</ejb-name> <ejb-class>btier.impl.MessageLogger</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> ...

Page 24: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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 <message-driven-deployment> 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)

Page 25: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16-30 Copyright © 2005, Oracle. All rights reserved.

orion-ejb-jar.xml: Example

<enterprise-beans> <message-driven-deployment name="MessageLogger" connection-factory-location= "jms/QueueConnectionFactory"> destination-location= "jms/demoQueue"</message-driven-deployment> ...</enterprise-beans>

Page 26: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16-31 Copyright © 2005, Oracle. All rights reserved.

Creating an MDB with JDeveloper

1

2

3

Page 27: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

16-32 Copyright © 2005, Oracle. All rights reserved.

Creating an MDB with JDeveloper

ejb-jar.xml

Page 28: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

Page 29: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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.

Page 30: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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

Page 31: 16 Copyright © 2005, Oracle. All rights reserved. Developing Message-Driven Beans.

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