Vous êtes sur la page 1sur 39

JA 331 - SAP Java Open Integration Technologies

Java Message Service & Message Driven Beans

Java Message Service and Message Driven Beans: Unit Objectives

After completing this unit, you will be able to: name the participants and explain the concepts of the Java Message Service (JMS) give examples where JMS can be used describe the special features of Message Driven Beans (MDBs) implement general JMS applications and MDBs using the SAP NetWeaver Developer Studio explain the special JMS features in SAP NetWeaver 04

SAP AG 2004, JA 331 / Unit: JMS and MDB / 2

Java Message Service and Message Driven Beans: Course Overview Diagram

<To be taken from the Course Overview unit. Change the highlight to show which unit is now covered.>

SAP AG 2004, JA 331 / Unit: JMS and MDB / 3

Java Message Service and Message Driven Beans: Business Scenario

JMS is adequate for coupling systems which have to exchange data, but to not depend on the other systems immediate reaction. Scenario: A company has among others two systems. One is located in a call center, one in the accounting department. The call center agents enter new complaints and have the possibility to grant a financial compensation to the customers. If they do so, the system they use will send a message to the accounting system and they can proceed with their work it does not matter to them when the accounting guys will execute the request.
SAP AG 2004, JA 331 / Unit: JMS and MDB / 4

Java Message Service: Topic Objectives

After completing this topic, you will be able to: name the participants involved in the Java Message Service (JMS) explain the basic concepts of JMS implement an application for sending a message

explain the transaction concept used for JMS

SAP AG 2004, JA 331 / Unit: JMS and MDB / 5

Asynchronous Messaging
Asynchronous messaging can be compared to e-mail communication between people

System 1

Message-Oriented Middleware

System 2

Benefits: Better performance: Avoid waiting for replies Loosely coupling of systems Connection of systems written in different languages

SAP AG 2004, JA 331 / Unit: JMS and MDB / 6

Java Message Service


The Java Message Service is a Message-Oriented Middleware (MOM) and thus allows to decouple producers and consumers of messages

The JMS API defines a vendor independent access to various MOM (JMS based) systems is part of the J2EE platform is defined within the javax.jms package

SAP AG 2004, JA 331 / Unit: JMS and MDB / 7

JMS Providers & Clients


JMS Providers provide an implementation of the JMS API Web Application Server

JMS Clients use the implementation and can be

Producers (send messages)


Consumers (receive messages)

SAP AG 2004, JA 331 / Unit: JMS and MDB / 8

JMS Destinations

Queue
Point-To-Point

Topic
Publish-Subscribe

One registered receiver per message

Many receivers per message possible


2 kinds of subscription Non-durable Durable

Durability by store-and-forward messaging FIFO

SAP AG 2004, JA 331 / Unit: JMS and MDB / 9

Messaging Models

Point-to-Point / Publish-Subcribe

Queue Sender 5 4

3 2 Queue

Queue Receiver 1

Topic Publisher 3

1a 2 Topic JMS Provider

Topic Subscriber 1

1b

Topic Subscriber 2

1. 2. 3.
SAP AG 2004, JA 331 / Unit: JMS and MDB / 10

Messages to be sent Messages waiting in the Queue Messages being consumed

Point-to-point QueueBrowser

Queue

JMS Provider

QueueBrowsers can see all messages on a Queue regardless of the order QueueBrowsers do NOT consume messages, i.e. all messages will be available for the according receivers A QueueBrowser starts at the end of the Queue and can then iterate over the messages; it cannot go back

Queue Browser

SAP AG 2004, JA 331 / Unit: JMS and MDB / 11

Message Format

Header

Administrative Information; key-value-pairs

Properties

Additional Information; key-value-pairs

Some may be disabled to improve performance

Body

A Message of one of the following types: StreamMessage MapMessage TextMessage ObjectMessage BytesMessage [Message ( has no body)]

SAP AG 2004, JA 331 / Unit: JMS and MDB / 12

Sending a message
Source code / 1
InitialContext iCtx = new InitialContext(); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) iCtx.lookup("java:comp/env/jms/QConnFactory");
Queue Connection Factory

QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();

Queue Connection

QueueSession queueSession = queueConnection.createQueueSession(true, 0); //true -> session is transacted; 0 -> acknowledgement not specified

Queue Session

Queue queue = (Queue)iCtx.lookup("java:comp/env/jms/myQueue");

Queue
Queue Sender

QueueSender queueSender = queueSession.createSender(queue);


SAP AG 2004, JA 331 / Unit: JMS and MDB / 13

Sending a message
Source code / 2
1 Message message = queueSession.createMessage(); message.setStringProperty("name", "value"); //...
Queue Connection Factory

Queue Connection Queue Session

queueSender.send(message);

1 Queue Sender

Queue

SAP AG 2004, JA 331 / Unit: JMS and MDB / 14

Sending a message

Logical names and Mapping


Source Code
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) initialContext.lookup("java:comp/env/jms/QConnFactory");

1 Standard deployment descriptor


<resource-ref> <res-ref-name>jms/QConnFactory</res-ref-name> ... </resource-ref>

2
<resource-ref> <res-ref-name>jms/QConnFactory</res-ref-name> <res-link>QueueConnectionFactory</res-link> </resource-ref>

Vendor deployment descriptor

SAP AG 2004, JA 331 / Unit: JMS and MDB / 15

Transactions
Sessions may be specified as transacted. Finishing a transaction automatically starts the next. call commit() / rollback() on your session

Sending App 4 3

Receiving App

6 5 Transaction A1
JMS Provider

2 1 Transaction B1 Session B

Session A

SAP AG 2004, JA 331 / Unit: JMS and MDB / 16

Acknowledgement
If a session is not transacted, there are three ways to realize the acknowledgement: AUTO_ACKNOWLEDGE the session acknowledges the message receipt clients have to be prepared to receive the last message more than once DUPS_OK_ACKNOWLEDGE lazy acknowledgement; may result in duplicate messages clients have to be prepared to receive multiple messages more than once CLIENT_ACKNOWLEDGE the client acknowledges; the acknowledgement of one message automatically acknowledges all other messages

Flag: JMSRedelivered (message header) for resent messages


SAP AG 2004, JA 331 / Unit: JMS and MDB / 17

Java Message Service: Topic Summary

You should now be able to: name the participants involved in the Java Message Service (JMS) explain the basic concepts of JMS implement an application for sending a message explain the transaction concept used for JMS

SAP AG 2004, JA 331 / Unit: JMS and MDB / 18

Message Driven Beans: Topic Objectives

After completing this topic, you will be able to: state the differences between Message Driven Beans (MDBs) and other Enterprise Java Beans explain how messages are passed to MDBs implement an MDB

SAP AG 2004, JA 331 / Unit: JMS and MDB / 19

Enterprise Java Beans Overview

EJB

Session Beans

Entity Beans

Message Driven Beans

Stateless

Statefull

Container Managed Persistence

Bean Managed Persistence Asynchronous Communication

Synchronous Communication

SAP AG 2004, JA 331 / Unit: JMS and MDB / 20

Message Driven Beans


came new with EJB 2.0 are handled by the EJB Container, which acts as a JMS Client MDBs can consume messages (and optionally produce them) will be assigned to one Topic or Queue are server-side components (like other EJBs) and consist of the bean class two deployment descriptors no interfaces (home, remote) MDBs are accessed by sending messages to the according Queue or Topic are stateless

SAP AG 2004, JA 331 / Unit: JMS and MDB / 21

The Dual Nature of MDBs


<<Interface>> EnterpriseBean

<<Interface>> MessageListener

<<Interface>> MessageDrivenBean

ExampleMDB

javax.jms.MessageListener provide an asynchronous receipt of JMS messages

javax.ejb.MessageDrivenBean by this MDBs inherit the methods the container needs to handle them like other EJBs

The business logic is put into the onMessage() method.

SAP AG 2004, JA 331 / Unit: JMS and MDB / 22

Transactions
MDBs do not use the usual JMS transaction model; they can make use of distributed transactions

container-managed transaction demarcation The onMessage() method gets one the following attributes

Required: The message receipt and the onMessage() method run within the same, new transaction. NotSupported: The onMessage() method runs without a transaction.

bean-managed transaction demarcation The onMessage() method starts and commits transactions explicit.

SAP AG 2004, JA 331 / Unit: JMS and MDB / 23

Parallel Processing
The EJB Container handles several instances of one MDB at one time server-side concurrent processing All instances are equivalent Each MDB instance runs within its own thread. There is no guarantee that messages are delivered in exact the same order as they are sent.
MDB Instance 1 MDB MDB Instance 2 JMS Provider EJB Container MDB Instance 3

Thread A

Thread B

Thread C

SAP AG 2004, JA 331 / Unit: JMS and MDB / 24

Lifecycle
does not exist
called before deletion //create a new instance setMessageDrivenContext() ejbCreate() ejbRemove()

called after creation

invoked on message receipt

method-ready pool
onMessage()

SAP AG 2004, JA 331 / Unit: JMS and MDB / 25

Deployment Descriptors
ejb-jar.xml / 1

MDBs are declared in the ejb-jar.xml within the tag <message-driven> EJB related data / Messaging related data EJB name bean class name

transaction demarcation type


environment entries used by the MDB <message-driven-destination> The JMS destination type the MDB listens to

EJB related

Messaging related

SAP AG 2004, JA 331 / Unit: JMS and MDB / 26

Deployment Descriptors
ejb-jar.xml / 2

Name / class Destination type

Environment

SAP AG 2004, JA 331 / Unit: JMS and MDB / 27

Deployment Descriptors
ejb-j2ee-engine.xml

Vendor specific descriptor contains additional information JMS information

the concrete destination of the MDB

the concrete ConnectionFactory used

SAP AG 2004, JA 331 / Unit: JMS and MDB / 28

Message Driven Beans: Topic Summary

You should now be able to: state the differences between Message Driven Beans (MDBs) and other Enterprise Java Beans explain how messages are passed to MDBs implement an MDB

SAP AG 2004, JA 331 / Unit: JMS and MDB / 29

JMS @ SAP NetWeaver 04: Topic Objectives

After completing this topic, you will be able to: state the special features regarding the Java Message Service in SAP NetWeaver create a Message Driven Bean with the SAP NetWeaver Developer Studio

SAP AG 2004, JA 331 / Unit: JMS and MDB / 30

Overview
Full implementation of JMS 1.0.2b Integrated in SAP NetWeavers Security concepts Accessing the central user store you can determine who is allowed to administrate JMS objects. For all destinations (Queues & Topics) it can be defined who is allowed to send to or receive from them.

Failover Capability
JMS Connector IDE Support

SAP AG 2004, JA 331 / Unit: JMS and MDB / 31

Failover capability
If the node with the JMS Service running on it crashes, another node will host it.

JMS

JMS

Cluster

Cluster

JMS

Cluster

SAP AG 2004, JA 331 / Unit: JMS and MDB / 32

JMS Connector / 1
Through the JMS Connector it gets transparent for the application which JMS Provider is used.

JMS App

JMS App

JMS App

XML

JMS Connector

JMS Provider

SAP AG 2004, JA 331 / Unit: JMS and MDB / 33

JMS Connector / 2
[Source code / standard deployment descriptor]
<resource-ref> <res-ref-name>jms/QConnFactory</res-ref-name> <res-link>QueueConnectionFactory</res-link> </resource-ref>

Vendor deployment descriptor

JMS Connector deployment descriptor

<factory-name> QueueConnectionFactory </factory-name> ... <link-factory-name> jmsfactory/default/QueueConnectionFactory </link-factory-name>

2 JMS Provider / JNDI

SAP AG 2004, JA 331 / Unit: JMS and MDB / 34

IDE Support

Creating a new MDB / 1

SAP AG 2004, JA 331 / Unit: JMS and MDB / 35

IDE Support

Creating a new MDB / 2

Deployment descriptor

MDB class

Business Logic

SAP AG 2004, JA 331 / Unit: JMS and MDB / 36

IDE Support

Bidirectional Editing

SAP AG 2004, JA 331 / Unit: JMS and MDB / 37

JMS @ SAP NetWeaver 04: Topic Summary

You should now be able to: state the special features regarding the Java Message Service in SAP NetWeaver create a Message Driven Bean with the SAP NetWeaver Developer Studio

SAP AG 2004, JA 331 / Unit: JMS and MDB / 38

Java Message Service and Message Driven Beans: Unit Summary

You should now be able to: name the participants and explain the concepts of the Java Message Service (JMS) give examples where JMS can be used describe the special features of Message Driven Beans (MDBs) implement general JMS applications and MDBs using the SAP NetWeaver Developer Studio explain the special JMS features in SAP NetWeaver 04

SAP AG 2004, JA 331 / Unit: JMS and MDB / 39

Vous aimerez peut-être aussi