Friday, March 02, 2007

JAVA 認證心得 -- SCBCD (10)

Message-Driven Bean Component Contract

  • setMessageDrivenContext(),onMessage() can take an argument, only two methods in javax.ejb.MessageDrivenBean interface.
  • All calls to a MDB instance must be serialized
  • A message-driven bean must not be coded as reentrant
  • Only onMessage method maybe called with a specified transaction context.
  • The message-driven bean’s lifecycle is newInstance() -> setMessageDrivenContext() -> ejbCreate() -> onMessage()
  • onMessage() is able to access other enterprise beans, with BMT able to access resource managers
  • [MDB] For BMT beans, the Container uses the acknowledge-mode DD elements
  • A Message Driven Bean can only have Required and NotSupported as its transaction attribue.
  • MDB classes should not define a finialize() method.
  • MDB classes must define one ejbCreate() method, which must not take any argument. It is invoked only once in the life of the bean instance
  • MDB classes are allowed to have instance variables, which can contain state across the handling of client messages.
  • MDB can’t be accessed using the Java RMI API. And they are stateless
  • A message driven class must be defined public and must not be defined final or abstract.
  • The getRollbackOnly(),setRollbackOnly() method called from a message-driven bean with container-managed transaction demarcation will not throw any system exception
  • The ejbRemove() method invoked on a message-driven bean with container-managed transaction demarcation will not throw any system exception.
  • javax.ejb.EJBContext's methods which throw java.lang.IllegalStateException from a message-driven bean with container-managed transaction demarcation are getCallerIdentity(), getCallerPrincipal(), getEJBLocalHome() and getEJBHome().
  • A message-driven bean is required to provide no argument public constructor and a no argument ejbCreate method
  • The message-driven bean instance continues to exist from the client's perspective. The container actually discards(transition to 'does not exist' state) the bean instance which has thrown the java.lang.RuntimeException.
  • The code written in the onMessage(javax.jms.Message) method must take care of out of sequence messages. The message-driven bean instance may receive messages that are out of sequence.
  • In which of the following cases a message will be redelivered to the message-driven bean instance if the transaction rolls back?

    The message-driven bean uses container-managed transaction demarcation and Required transaction attribute is specified for the onMessage(javax.jms.Message) method

  • A MessageDrivenBean must implement ejbCreate and ejbRemove methods. Implementing multiple ejbCreate() is not an error but the container will only call the no-args ejbCreate() method, which the bean must have.
  • onMessage() is declared in javax.jms.MessageListener interface, which must be implemented by every MDB.
  • MessageDrivenBean interface has only two methods: ejbRemove() and setMessageDrivenContext.
  • An important point to remember here is that only the sending of the message is a part of the transaction. Whether the MDB receives the message or consumes it does not affect Bean's transaction. Further, the client's transaction context is never passed to a MDB. The container creates a new transaction context for a MDB if it requires a transaction. So, if the MDB rolls back its transaction, it does not affect the message sender's transaction.
    The second point is that if an MDB does not throw any exception while processing the message (Note: It can only throw Runtime or EJBExceptions from onMessage()), the message is considered to be consumed. On the other hand, if the onMessage() method throws an exception, the exception is logged, the MDB instance is discarded, and the transaction is rolled back. The message is redelivered (to a different MDB instance). How many times a message can be redelivered depends on the container.
  • The message-driven bean class must define one ejbCreate() method whose signature must follow these rules:

    1. The method name must be ejbCreate.

    2. The method must be declared as public.

    3. The method must not be declared as final or static.

    4. The return type must be void.

    5. The method must have no arguments.

    6. The throws clause must not define any application exceptions.

  • The onMessage() method may contain method calls to other private methods of the bean.
  • As any method of a message-driven bean class must not throw any application exception.
  • A message-driven bean can receive messages asynchronously but it sends messages synchronously.
  • From the client's perspective a message-driven is shared between multiple clients
  • MDB doesn’t complete transaction before the end of the onMessage method

    1. Log application error

    2. roll back

3. discard instance of the bean

Read more!

No comments: