Friday, February 6, 2009

MDB Illustrated

In the previous article, we briefed the skeleton of a MDB. In this article, we describes a conceptional model to illustrate how MDB works inside a container.

A container is loosely referred to an application server in this article. The functions performed by a container mentioned below includes those done by a resource adapter and messaging provider.

When a MDB is deployed in a container, the container located two JMS resources that each MDB must be associated with.
  • The ConnectionFactory that the container uses to create a connection to the messaging server.
  • The Destination that the MDB is interested in receiving messages from the messaging server.
The container creates a connection (if none is available) to the messaging server based on the allocated ConnectionFactory. If the connection was acquired/created successfully, the container then creates a message consumer (on behalf of the MDB) on the destination associated with the MDB. The container is now ready to deliver messages to the MDB instances.

The container constructs a MDB instance with the following sequence.
  • Invokes the MDB class'es newInstance() method to obtain a new instance.
  • Injects resources to the instance,if any. For example, injects MessageDrivenContext if declared.
  • Invokes the PostConstruct method, if any.
When a message arrives (received by the container on behalf of the MDB), the container delivers/dispatches the message as follows.
  • Get an available MDB instance (or create a new one if none is available) from the instance pool.
  • Allocate a thread from the thread pool (to execute the MDB instance).
  • Start the allocated thread to deliver the message with the following sequence.
    1. Start a new transaction if the MDB uses a container-managed transaction (default).
    2. Invoke the onMessage(Message m) method.
    3. Acknowledge/commit the transaction when onMessage() returns.
    4. Return the current thread and MDB instance back to their own pools.
At any point in time, there could be multiple instances been executed concurrently per MDB class. The maximum concurrent instances are usually configured during deployment. The container executes each MDB instance with one thread at a time.

The container calls an MDB instance's PreDestroy method, if any, before it destroys the instance.

No comments:

Post a Comment