MDB Lifecycle and its callback methods

Lifecycle callback methods

The following lifecycle event callbacks are supported for message-driven beans.

PostConstruct

  • The PostConstruct callback occurs after the container creates a new instance of MDB and before the first message listener method invocation on the bean. This is at a point after which any dependency injection (DI) has been performed by the container.

PreDestroy

  • The PreDestroy callback occurs before the bean is removed from the pool or destroyed.

The PostConstruct and PreDestroy lifecycle callback interceptor methods execute in an unspecified transaction and security context.

MDB Lifecycle

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 message listener method.

The following steps describe the life cycle 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 injects the bean’s MessageDrivenContext, if applicable, and performs any other dependency injection as specified by metadata annotations on the bean class or by the deployment descriptor.
  • The container then calls the bean’s PostConstruct lifecycle callback methods, if any.
  • The message-driven bean instance is now ready to be delivered a message sent to its associated destination or endpoint by any client or a call from the container to a timeout callback method.
  • 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 the PreDestroy lifecycle callback methods for it, if any. This ends the life of the message-driven bean instance.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.