Lifecycle Callbacks for Stateful Session Beans



Stateful session beans support callback events for the following events:

- construction
- destruction
- activation
- passivation

A method in the bean may be annotated to be executed when any of these events occur.

The PostContruct callback happens immediately after a bean is instantiated in the EJB container.
The bean may use dependency injection to get reference to other beans or resources, in that case the event happens after these injections are completed.
The method itself doesn’t have to have a certain name. It can be called anything and is a good place to for example create a Logger instance for logging.
A method could look like this:

The PreDestroy event happens after a method has been completed which has the annotation @Remove (see below).
This would be a good place to for example place code to save the shopping cart to database.

When a method is marked with the @Remove annotation,
it means that the stateful bean instance will be removed from the object pool of the container when the method has finished executing.

A method annotated with the @PrePassivate annotation will execute when for example the user has been idle for some time.
It could be a good method to store the state of the bean to a cache memory.

A method annotated with the @PostActivate annotation is executed when the user starts to use a passivated stateful session bean again.







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">