Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc.

Message-driven Bean Component Contract

Enterprise JavaBeans 2.0, Public Draft

Message-driven bean state diagram

From the client perspective, the message consumer continues to exist. If the client continues sending messages to the Destination associated with the bean, the Container can delegate the client’s messages to another instance.

14.4.11 Missed ejbRemove() calls

The Bean Provider cannot assume that the Container will always invoke the ejbRemove() method on a message-driven bean instance. The following scenarios result in ejbRemove() not being called on an instance:

A crash of the EJB Container.

A system exception thrown from the instance’s method to the Container.

If the message-driven bean instance allocates resources in the ejbCreate() method and/or in the onMessage method, and releases normally the resources in the ejbRemove() method, these resources will not be automatically released in the above scenarios. The application using the mes- sage-driven bean should provide some clean up mechanism to periodically clean up the unreleased resources.

14.5 Message-driven bean state diagram

When a client sends a message to a Destination for which a message-driven bean is the consumer, the container selects one of its method-ready instances and invokes the instance’s onMessage method.

The following figure illustrates the life cycle of a MESSAGE-DRIVEN bean instance.

5/31/00

286

Sun Microsystems Inc

Message-driven bean state diagram

Enterprise JavaBeans 2.0, Public Draft

Message-driven Bean Component Contract

Figure 60 Lifecycle of a MESSAGE-DRIVEN bean.

does not exist

1. newInstance()

 

ejbRemove()

 

2. setMessageDrivenContext(mdc)

 

 

 

3. ejbCreate()

 

 

 

 

 

 

 

 

onMessage()

method-ready

pool

onMessage() action resulting from client message arrival ejbCreate() action initiated by container

The following steps describe the lifecyle of a message-driven bean instance:

A message-driven bean instance’s life starts when the container invokes newInstance() on the message-driven bean class to create a new instance. Next, the container calls setMessageDrivenContext() followed by ejbCreate() on the instance.

The message-driven bean instance is now ready to be delivered a message sent to its Destination by any client.

When the container no longer needs the instance (which usually happens when the container wants to reduce the number of instances in the method-ready pool), the container invokes ejbRemove() on it. This ends the life of the message-driven bean instance.

14.5.1 Operations allowed in the methods of a message-driven bean class

Table 9 defines the methods of a message-driven bean class in which the message-driven bean instances can access the methods of the javax.ejb.MessageDrivenContext interface, the java:comp/env environment naming context, resource managers, and other enterprise beans.

287

5/31/00

Sun Microsystems Inc.

Message-driven Bean Component Contract

Enterprise JavaBeans 2.0, Public Draft

Message-driven bean state diagram

If a message-driven bean instance attempts to invoke a method of the MessageDrivenContext interface, and the access is not allowed in Table 9, the Container must throw and log the java.lang.IllegalStateException.

If a bean instance attempts to access a resource manager or an enterprise bean and the access is not allowed in Table 9, the behavior is undefined by the EJB architecture.

Table 9

Operations allowed in the methods of a message-driven bean

 

 

 

 

 

 

 

Bean method can perform the following operations

 

Bean method

 

 

 

Container-managed transaction

Bean-managed transaction

 

 

 

 

demarcation

demarcation

 

 

 

 

 

 

 

 

 

constructor

-

-

 

 

 

 

 

setMessageDriven-

JNDI access to java:comp/env

JNDI access to java:comp/env

 

Context

 

 

 

 

 

 

 

ejbCreate

JNDI access to java:comp/env

MessageDrivenContext methods:

 

 

getUserTransaction

 

ejbRemove

 

JNDI access to java:comp/env

 

 

 

 

 

 

 

 

 

MessageDrivenContext methods:

MessageDrivenContext methods:

 

 

getRollbackOnly, setRollbackOnly

getUserTransaction

 

onMessage

JNDI access to java:comp/env

UserTransaction methods

 

Resource manager access

JNDI access to java:comp/env

 

 

 

 

Enterprise bean access

Resource manager access

 

 

 

Enterprise bean access

 

 

 

 

Additional restrictions:

The getRollbackOnly and setRollbackOnly methods of the MessageDrivenContext interface should be used only in the message-driven bean methods that execute in the context of a transaction. The Container must throw the java.lang.IllegalStateException if the methods are invoked while the instance is not associated with a transaction.

The reasons for disallowing operations in Table 9:

Invoking the getCallerPrincipal and isCallerInRole methods is disallowed in the message-driven bean methods because the Container does not have a client security context. The Container must throw and log the java.lang.IllegalStateException if either of these methods is invoked.

Invoking the getRollbackOnly and setRollbackOnly methods is disallowed in the message-driven bean methods for which the Container does not have a meaningful transaction context, and for all message-driven beans with bean-managed transaction demarcation.

5/31/00

288

Sun Microsystems Inc

Object interaction diagrams for a MESSAGE-DRIVEN beanEnterprise JavaBeans 2.0, Public Draft Message-driven Bean Component

Accessing resource managers and enterprise beans is disallowed in the message-driven bean methods for which the Container does not have a meaningful transaction context or client security context.

The UserTransaction interface is unavailable to message-driven beans with con- tainer-managed transaction demarcation.

Invoking getEJBHome is disallowed in message-driven bean methods because there are no EJBHome objects for message-driven beans. The Container must throw and log the java.lang.IllegalStateException if this method is invoked.

14.6Object interaction diagrams for a MESSAGE-DRIVEN bean

This section contains object interaction diagrams that illustrate the interaction of the classes.

14.6.1 Message receipt: onMessage method invocation

The following diagram illustrates the invocation of the onMessage method.

Figure 61 OID for invocation of onMessage method on MESSAGE-DRIVEN bean instance

container-provided classes

client

destination

container messagesynchro-

instance

 

 

driven

nization

 

 

 

context

 

 

 

 

 

 

 

message

onMessage method

289

5/31/00