Upload
007veer
View
221
Download
0
Embed Size (px)
Citation preview
8/12/2019 03 Message Driven Beans
1/12
1
Message Driven Beans
Daniel Stollenwerk
8/12/2019 03 Message Driven Beans
2/12
2
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Af ter completing th is topic, you will be able to:
state the dif ferences between Message Driven Beans
(MDBs) and other Enterprise Java Beans
explain how messages are passed to MDBs
implement an MDB
Message Driven Beans: Topic Objectives
8/12/2019 03 Message Driven Beans
3/12
3
Message Driven Beans add a new functionality to the Enterprise Java Beans:
asynchronous message receiving
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Enterprise Java Beans Overview
Session
Beans
Entity
Beans
EJB
Stateless Statefull Container
Managed
Persistence
Bean
Managed
Persistence
Asynchronous
Communication
Message Driven
Beans
Synchronous Communication
8/12/2019 03 Message Driven Beans
4/12
4
Sometimes Message Driven Beans are referred to as JMS Clients. This might be
because in a way they receive messages. But in fact the EJB Container acts as a
client and receives messages which it then distributes along the different Beans
respectively their instances
Message Driven Beans do not have any interfaces for direct access to their
business methods they can only be called via messages
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
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 producethem)
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 accordingQueue or Topic
are stateless
8/12/2019 03 Message Driven Beans
5/12
5
When developing an MDB you do not have to care about other methods thanonMessage( ) . You can of course put coding into the other methods if you need
to be executed at the according hooks but to keep it simple you may also just putthe needed business logic into onMessage( ) .
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
The Dual Nature of MDBs
EnterpriseBean
ExampleMDB
MessageDrivenBean
MessageListener
The business logic is put
into the onMessage() method.
javax.ejb.MessageDrivenBean
by this MDBs inherit themethods the container needs to
handle them like other EJBs
javax.jms.MessageListener
provide an asynchronousreceipt of JMS messages
8/12/2019 03 Message Driven Beans
6/12
6
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Transactions
MDBs do not use the usual JMS transaction model; they can make
use of distributed t ransactions
container-managed transaction demarcation
The onMessage() method gets one the following attributes
Required: The message receipt and theonMessage() method
run within the same, new transaction.
NotSupported: The onMessage() method runs without a
transaction.
bean-managed t ransaction demarcation
The onMessage() method starts and commits transactions
explicit.
The usual way of transaction handling in JMS applications is to specify which
messages should be sent / received as a transaction. MDBs do not care about
messages in transactions but rather make sure that their business logic is
executed in a transactional mode.
container-managed
If you choose this option you can choose one of the following:
Required: Every onMessage() execution, including all commands executed in the method, will be
handled as a transaction.
NotSupported: The onMessage( ) execution runs without a transaction
bean-managed
Arbitrary parts of the code in the onMessage() method may be put into a transaction
8/12/2019 03 Message Driven Beans
7/12
7
The EJB Container automatically instantiates new objects of a MDB if there are
more messages to be processed than instances available.
Because the EJB Container delivers an incoming message to an arbitrary MDB
instance which is capable of processing the message there is no guarantee about
the order of the messages.
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Parallel Processing
The EJB Container handles several instances of one MDB at one
time server-side concurrent processing
Al l 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.
JMS ProviderEJB
Container
MDB
Instance 1 Thread A
MDB
Instance 2 Thread B
MDB
Instance 3 Thread C
MDB
8/12/2019 03 Message Driven Beans
8/12
8
voi d set MessageDr i venCont ext ( MessageDr i venCont ext ct x)
is called by the EJB Container after the creation of a MDB instance, providingthe MDB instance with a reference to its MessageDr i venCont ext object.
voi d ej bCr eat e( )
is called by the EJB Container after the creation of a MDB instance (after theset MessageDr i venCont ext ( ) method call).
In this method resources needed for the beans life can be obtained.
voi d ej bRemove( )
is called by the EJB Container before deleting the MDB instance.
Resources should be released here.
if the onMessage( ) method throws a Runt i meExcept i on the bean will return tothe state does not exist; the ej bRemove method will then not be called.
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Lifecycle
does not
exist
method-ready
pool
ejbRemove()//create a new instance
setMessageDrivenContext()
ejbCreate()
onMessage()
called before
deletion
invoked on
message
receipt
called after
creation
8/12/2019 03 Message Driven Beans
9/12
9
The ej b- j ar . xml is the J2EE standard deployment descriptor.
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Deployment Descriptorsejb-jar.xml / 1
MDBs are declared in the ejb-jar.xml with in the tag
EJB related data / Messaging related data
EJB name
bean class name
transaction demarcation type
environment entries used by the MDB
The JMS destination type the MDB l istens to
EJB related
Messaging
related
8/12/2019 03 Message Driven Beans
10/12
10
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Deployment Descriptorsejb-jar.xml / 2
Name / class
Destination type
Environment
8/12/2019 03 Message Driven Beans
11/12
11
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
Deployment Descriptorsejb-j2ee-engine.xml
Vendor specific descriptor
contains additional information JMS information
the concrete destination of the MDB
the concrete ConnectionFactory used
8/12/2019 03 Message Driven Beans
12/12
12
SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /Message Driven Beans
You should now be able to:
state the dif ferences between Message Driven Beans
(MDBs) and other Enterprise Java Beans
explain how messages are passed to MDBs
implement an MDB
Message Driven Beans: Topic Summary