1 / 31

Developing Message-Driven Beans

Developing Message-Driven Beans. 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)

rstanfield
Download Presentation

Developing Message-Driven Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Developing Message-Driven Beans

  2. 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. 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. 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. 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 JMS client JMS client MOM

  6. 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 Application JMS JMS Queue/Topic JMS provider JMS client JMS client

  7. Point-to-Point Model • Senders send messages to virtual destinations. • There is only one receiver for a message from a queue. Queue1 M1 M1 R1 S1 M2 M2 R2 S2 M3 Queue2 R3 M3 Messaging server Queues (virtual destinations) Queue receiver Queue senderM* - Message

  8. Publish-and-Subscribe Model • Publishers send messages to virtual destinations. • One or more subscribers receive the messages. M1 S1 M1 Topic1 P1 S2 Topic2 P2 M2 M2 S3 Messaging server Topics (virtual destinations) Topic subscriber Topic publisherM* - Message

  9. 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

  10. 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

  11. 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.

  12. 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.

  13. 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.

  14. 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

  15. MDB Architecture OC4J Client JMS resourceprovider MDB Queue/Topic

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

  17. State Diagram of an MDB Does not exist Container invokes Class.newInstance, setMessageDrivenContext(context)and ejbCreate() Container invokes ejbRemove(…) Method-ready pool Container invokes onMessage()

  18. 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.

  19. 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); }

  20. 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; }

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

  22. 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>

  23. 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> ...

  24. 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)

  25. 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>

  26. Creating an MDB with JDeveloper 2 1 3

  27. Creating an MDB with JDeveloper ejb-jar.xml

  28. Creating an MDB with JDeveloper • Map the destination details in orion-ejb-jar.xml. orion-ejb-jar.xml

  29. 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.

  30. 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

  31. 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

More Related