Join Patrick Royal for an in-depth discussion in this video Understanding the life cycle of beans, part of Java EE Essentials: Enterprise JavaBeans.
- View Offline
- [Voiceover] Because JavaBeans are intended to be modular components most Beans have a fairly straightforward life cycle. They're instantiated, their methods are invoked, and then they are destroyed. Sometimes a Bean might be passivated or moved from memory into secondary storage in order to save space if it will not be used in a while. Within this overall framework of a Bean life cycle, each different type of Bean has slightly different means by which it transitions between the stages. In this video we will investigate those transitions for each category of Bean.
Stateful, stateless, entity, and message-driven. The simplest life cycle is that of the Stateless session Bean. As the name suggests this type of Bean doesn't have a state so it cannot transition from an active to a passive state. Instead the only actions that apply to this Bean are creating it and destroying it. To create a Stateless session Bean the container must first set the session context via the setSessionContext method. Then it calls ejbCreate to instantiate the Bean. When the Bean is finished the container calls ejbRemove to destroy it.
The Stateful session Bean is a bit more complicated. Unlike the Stateless session Bean this Bean can freely transition from active to passive and vice versa. So there are four different actions that can apply to this Bean. To create a Stateful session Bean the container initiates the life cycle with the create method and then calls setSessionContext and ejbCreate to instantiate the Bean. By default, this Bean will now be in the ready state. It can then be passivated by calling the ejbPassivate method.
And once passivated it can be reactivated by calling the ejbActivate method. While in passive mode, none of the Beans method can execute so whenever the client invokes the method on the Bean the Bean must first be activated. Finally when the Bean is finished. The container will call the remove method to end the life cycle, followed by the ejbRemove method to destroy the Bean itself. The Entity Bean has essentially the same possible states as the Stateful session Bean but its transitions from state to state are quite different.
The first method to be called on an Entity Bean is not create but set entity context which passes the entity context to the Bean. This then puts the Bean into a pool of available instances. The important thing to note here is that at this point, the Bean still hasn't been created. It's just an instance that's not yet associated with an object identity. The identity will not be assigned until the instance is moved to the ready stage. There are actually two paths from the pool to the ready state.
The first time the transition occurs the container calls the create, ejbCreate, and ejbPostCreate methods. Once in the ready state, the Bean can be passivated just like a Stateful session Bean which sends it back into the pool. It can then be reactivated and returned to the ready state without needing to recreate the Bean. The other path back to the pool state is for the container to call remove and ejbRemove. In this case it essentially reversed the Entity Bean to no longer be associated with an object.
Thus it must be created again in order to become ready. Finally, when a context itself is no longer needed the container can call the unsetEntityContext method to completely remove the Entity Bean. The last type of Bean is the Message-driven Bean. Like the Stateless session Bean this Bean is never passivated so the main transitions are creation which requires the set message driven context and ejbCreate methods and destruction which requires the ejbRemove method. In the ready state, the Message Beans can also be subject to an additional method called OnMessage.
This causes it to receive and act upon a single message after which it automatically returns to the ready state. Thus we have the basics of the life cycles for Enterprise Beans. In the next video, we will use this knowledge to start creating a home interface.
- Building your first simple JavaBeans component
- Creating simple entity beans and message-driven beans
- Defining a primary key
- Understanding the life cycle of beans
- Creating home and component interfaces
- Controlling concurrent access to JavaBeans objects
- Debugging and optimizing JavaBeans code
- Enabling security and encryption
- Creating asynchronous methods