Stateless EJBs: Pooling and Lifecycle
Join the DZone community and get the full member experience.Join For Free
a summarized view (notes) of the stateless ejb pooling and life-cycle. useful for newbies . . . . .
ejb pooling: quick overview
- ejb instances are stored in a location known as the ejb pool – this is nothing but an in-memory cache .
- stateless ejbs are generally instantiated on-demand i.e. as and when the client invokes a method on the bean.
- however, most application servers also provide a facility wherein they can be configured so that the ejb pool can be populated at start up with certain number of instances e.g. in weblogic, this can be configured within the weblogic-ejb-jar.xml by stating the exact number in the initial-beans-in-free-pool element.
- improved performance
- client doesn’t need to wait until the bean is instantiated in order to service the request
stateless bean lifecycle
slsb states : non_existent, inactive, busy
non_existent – this is a scenario where the bean does not yet exist in the ejb pool (has not been instantiated)
inactive – it is a state where the ejb instance already exists in the ejb pool but is not servicing a client request
busy – an ejb instance is summoned in order to service a client request, and as a result it goes into a busy state.
an ejb instance is summoned in order to service a client request, and as a result it goes into a busy state. an ejb can go into a busy state from either inactive or non_existent states
slsb state transitions
inactive to busy – as the name suggests, an inactive bean in the pool was chosen to cater to the client invocation
non_existent to busy – a fresh ejb instance was created in order to serve a client request
busy to inactive
an ejb can go from a busy state back to an inactive state i.e. after servicing the client request, the instance goes back to the pool
inactive to non_existent
an ejb instance can also be destroyed under certain scenarios by configuring a time out which forces the application server to remove all ejb instances in the pool which have been inactive for a certain time interval . however, app servers do ensure that the ejb instance count does not go below the minimum number of permissible instances (configurable property – read above)
note : this primarily done in order to conserve the amount of memory which is consumed by the pooled ejbs since they are cached there. if there are a lot of unused instances, they can be removed in order to optimize memory consumption. the container can create new instances on-demand
what happens when a client invokes a method on a stateless bean?
scenario 1 : a fresh instance of the bean is instantiated by the container.
why does this happen?
possible reasons are
- there are no beans in the ejb pool to begin with – the app server was not instructed to pool a default number of beans on server startup
- all the beans in the ejb pool are busy servicing requests from clients, but the total bean count in the pool is less than the maximum number of instances which are permissible (a configuration parameter)
scenario 2 : an existing instance of the bean is returned by the container
- there are certain number of ejb instances available in the pool and they are not in a busy state i.e. they are not participating in a servicing a client request. one such instance is just returned by the container
- there were certain number of ejb instances available in the pool but all of them are in a busy state and the maximum permissible number of beans in the pool has already been reached. the client application has to wait until one such bean becomes free and is available to cater to the client request
scenario 3 : the client request is not services (receives an exception)
if the maximum permissible number of beans in the pool has already been reached and the none of the busy beans become available within a certain time frame
Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Apache Kafka vs. Message Queue: Trade-Offs, Integration, Migration
Working on an Unfamiliar Codebase
Front-End: Cache Strategies You Should Know