EJB OVERVIEW OF EJB uses application server to
EJB
OVERVIEW OF EJB uses application server to provide services such as resource pooling, networking, security, caching and transaction management. EJB architecture provides consistent framework for creating distributed n-tier middleware applications. It’s a server side component that executes business logic. It is deployed on application server & provides services to local & remote client. Advantages Reusablity, Component based architecture, provides standard system level services.
DISADVANTAGES OF EJB It has a large complicated specification. It has heavy-weighted framework. It is difficult to code and debug. It is only suitable for highly scalable secure transactional object.
ARCHITECTURE OF EJB
EJB ARCHITECTURE 1)EJB server It provides execution environment for the server component. It also provides system services like multi processing, load balancing, device access, naming and transaction processing. It hosts EJB container.
EJB ARCHITECTURE 2) EJB Container It serves as interface between EJB and outside world. The EJB client cannot access the bean directly however it can access the bean through container generated method. Container can be categorized on the basis of types of beans they contain. Session container contains transient, non-persistent EJBs whose states are not saved. Entity container contains persistent EJBs whose states are stored between invocations.
EJB ARCHITECTURE Ø Container hosts following components EJB object – Ø Ø A client invokes a bean instance through EJB object. As it intercepts the message request from the client and delegate it to the bean instance, it is also called request interceptor Remote interface – Ø It duplicates the method of bean class and hence all clients access the remote object through this interface
EJB ARCHITECTURE Ø Home object – Ø Ø Home interface – Ø Ø The clients create and destroy the EJB object through the EJB object factory called Home object. It exposes the method of creating , destroying and finding bean object through home interface. All home objects implement this interface. Local interface – Ø The process of creating bean through the home interface and then calling the bean through the remote interface is very slow. Hence local objects can be used instead of EJB object.
EJB ARCHITECTURE 3)Enterprise beans – These are Java EE components that implement business logic of the application. The EJB container acts as a runtime container for enterprise beans. Although transparent to application developer, it provides system level services such as transaction and security to its enterprise beans Enterprise beans categorized into three types depending upon functionality…
EJB ARCHITECTURE Enterprise beans categorized into three types depending upon functionality… Session beans which represent an action or a process that executes synchronously. Message-driven beans which also represent an action or process that executes asynchronously. Entity beans which represent data or a unique record in database.
EJB ARCHITECTURE 4) EJB client – It makes use of EJB beans to perform their operation. Clients locate EJB container using JNDI (Java Naming & Directory service). Then client invokes the EJB beans method through container. 5) Deployment descriptor – It lists the beans properties and element. Some of the components of DD are as follows: Home and remote interface Bean implementation class JNDI name for the beans Environment variables Transactional attributes Security requirements such as access rights, roles Bean management Lifecycle requirements Persistent requirements
ROLES IN EJB
ROLES IN EJB To have an EJB deployed and running, needs more than an application server and components. Parties associated with EJB development and deployment as shown in fig. 1)Bean provider He supplies the business components that can be assembled into an enterprise application. Each business component is an individual entity that can perform its task. 2)Application asembler He is responsible for integrating the components to develop the enterprise application.
3)EJB Deployer He configures the security, transactions and the performance of the system. This is essential to install the application in application server. He receicves the EJB-JAR files and install it in container. 4)System Administrator He is responsible for maintaining and monitoring the deployed system. He uses monitoring and mangement tools for this purpose.
5)EJB Container/Server provider He supplies the EJB container and the run time environment for the beans. He also provides middleware services to the component and manages them. 6)Tool vendors They supply IDEs to facilate the rapid modeling, building and debuging of the components. Some e. g of such tools are Rational rose modeling, Eclipse for buildng application and JUnit for testing the application. .
VIEW OF EJB CONVERSION EJB is a Server side component to get the services hosted by EJB. The client has to communicate with the EJB Component. To converse with Enterprise beans, the client has to perform following task. 1)Locating the enterprise Bean 2) Accessing Bean 3)Calling bean method 4) Destroying Bean
VIEW OF EJB CONVERSION 1)Locating the enterprise Bean – It is done through naming service. It is a service that associates a symbolic name with object. A DNS maps the symbolic m/c name to IP when client queries DNS server for a particular bean object. The IP address is returned if the DNS server can resolve the same otherwise client queries for another DNS for the same object. Once server is able to resolve the name it returns address to client.
There are two ways to find beans 1) Using JNDI – Java client uses JNDI to lookup EJB component. JNDI call returns a ref to object implementing EJB component. HOME interface is used to gain access to EJB object itself. 2) Using CORBA’s COS – (common object service) naming service of COS also used.
2) Accessing Bean The lookup method in the naming service returns a refrence to the home object that implements a home interface of the EJB component. The client then uses the home interface to lookup for existing EJB instances. If no such instance is available, then new one is created. This lookup method returns a reference to the EJB object remote interface. The client then uses this interface to interact with the EJB object in the server.
3)Calling bean method Once the client obtains the refrence of the remote object, it can then invoke the public methods on the remote EJB object. 4) Destroying Bean When the client has completed its interaction with the remote enterprise bean, it invoke the remove() on the home interface or on the objects remote interface.
TYPES OF ENTERPRISE BEANS
TYPES OF ENTERPRISE BEANS Session Beans 1. Stateless session beans 2. Stateful session beans 2. Entity Beans 3. Message-driven Beans 1.
TYPES OF ENTERPRISE BEANS 1. Session Beans It is analogous to an interactive session. Hence it has only single client. Session beans live only for the current client session. Session beans are non persistent and are destroyed once the session terminates. They implement the business logic and algorithms.
1. SESSION BEANS Based on the conversation that session beans maintain with a client they are categorized as Stateless Session Bean Stateful Session Bean Stateless Session bean Stateful Session bean State instance data
STATELESS SESSION BEAN They do not maintain a conversational state for the client. The state of an object consists of the values of its instance variables. It maintains its values only for the duration of the invocation. These types of beans are used to perform a generic task to all the clients
STATEFUL SESSION BEAN These beans possess internal states of the instance variables which represent the state of a unique client-bean session. Their states can be saved and restored between sessions, they are also called persistent session beans They can be used for managing the workflow of entity beans.
EXAMPLES OF SESSION BEANS Stateless session beans Interest calculator No client specific state needs to be preserved Business logic with no need for database access Stateful session beans Shopping cart Client specific state needs to be preserved
2. ENTITY BEANS They are persistent java objects whose state can be saved into the database and restored for later usage. An entity bean represents a record in the database. Even when the program exits, the entity bean will continue to exist. When the program is restarted, it can reuse the states of the bean object
3. MESSAGE DRIVEN BEANS They can be used by JEE applications to process the messages asynchronously. They act as message consumers for receiving java messaging service (JMS) messages. Client components do not invoke methods directly on message driven beans. Instead they use JMS to send messages to the destination for which the bean class acts as a listener.
MESSAGE DRIVEN BEANS CHARACTERISTICS They execute upon the receipt of a single client message. They are invoked asynchronously. They are relatively short lived. They are transaction aware. They are stateless.
LIFECYCLE OF BEANS
LIFE CYCLE OF STATELESS SESSION BEANS (1) Non-existent Container Initialization 1. Container Initialization new. Instance() 2. ejb. Remove() 1. set. Session. Context() ejb. Create() Ready
LIFE CYCLE OF STATELESS SESSION BEANS (2) A stateless session bean is never passivated. i. e. never changes Its lifecycle has only two stages NON-EXISTENT and READY for the invocation. The client initiates the lifecycle by obtaining a refrence to a stateless session bean. The container’s new. Instance() method initiates the bean class. The set. Session. Context() is used to set session variable. It can be used to access info about the client.
LIFE CYCLE OF STATELESS SESSION BEAN (3) The ejb. Create() method is invoked to make the bean ready for invocation. The beans in the method-ready pool are assigned to the EJBObject and answer incoming request for the duration of the request only. The ejb. Create() method is executed when the bean instance is created and not when the client invokes the Create() method for the EJBObject from the home interface. At the end of lifecycle the EJBContainer calls the ejb. Remove()method to garbage collect the bean instantiation. The ejb. Create() & ejb. Remove() are called only once during the lifecycle of the Client.
LIFE CYCLE OF STATEFUL SESSION BEANS (1) Non-existent Client initialization 1. new. Instance() 1. set. Session. Context() 1. ejb. Create() Client initialization ejb. Remove() Ready Container Initialization 3. ejb. Activate() Container initialization Timeout() 2. Container Initialization ejb. Passivate() Passive
LIFE CYCLE OF STATEFUL SESSION BEANS (2) The Client initiate the lifecycle by obtaining a refrence to a stateful session bean. When the client invokes the Create()method, EJBObject calls the new. Instance(), set. Session. Context() & ejb. Create()method to instantiate the bean and then activate it. While in ready state, the EJB Container can deactivate or passivate the bean by moving it from the memory to the secondary storage. It uses least recently used algorithm to select a bean for passivation. The EJB Container invokes the ejb. Passivate()method to passivate the bean and move it to an inactive state.
LIFE CYCLE OF STATEFUL SESSION BEANS (2) If client invokes business method on bean while it is in passive state the EJB Container activate the bean by calling the ejb. Activate()method. When the bean in active or passive state is timed out, then the container triggers Timeout() method to move bean into nonexistent state. At the end of lifecycle, the client invokes the ejb. Remove() method to garbage collect the bean instance.
LIFECYCLE OF ENTITY BEANS (1)
LIFECYCLE OF ENTITY BEANS (2) Entity beans contain core business data. Data sources can be Object database, relational databases. When the server is started and the container is initiated, the bean instances are created and pooled. Clients create() method triggers the container to invoke the ejbcreate() and ejbpostcreate() methods from beans home interface. These methods move beans to ready state. Clients remove() method triggers the container to invoke the ejbremove() method from the beans home interface. Persistence in entity beans can be managed in two ways.
BEAN MANAGED PERSISTENCE
BEAN MANAGED PERSISTENCE In this method, the developer provides the methods necessary for the bean persistence mechanism, such as insert record , delete record; write and read in the bean class itself Hence this method is more flexible ejb. Load() method is invoked to retrieve the bean’s persistent state from the stable storage. Bean instances in ready state can be passivated to pooled state in two ways Container initiated passivation Client initiated passivation
BEAN MANAGED PERSISTENCE Container initiated passivation The container disassociates a bean instance from the EJBObject after a system specific timeout. It uses ejb. Store() method to save the bean state and then it passivates the bean instance using ejb. Passivate() method. Passivated beans in the pooled state are still connected to clients by means of the EJBObject. It can be activated again by the client using the container initiated ejb. Activate() and ejb. Load() methods.
BEAN MANAGED PERSISTENCE Client Initiated passivation The client uses remove() method which in turn calls ejb. Remove() method to passivate the bean instance and remove it from the persistent storage.
CONTAINER MANAGED PERSISTENCE
CONTAINER MANAGED PERSISTENCE In this method the container provides a generic persistence service. This enables the developer to concentrate on the business logic. The developer has no control over the persistent mechanism. ejb. Load() method invokes the container’s methods to read the beans persistent state from the stable storage. ejb. Store() method also uses the container’s write methods to store the bean’s state to persistent storage.
LIFE CYCLE OF MESSAGE DRIVEN BEANS Non-existent Container Pre. Destroy() Container Post. Construct() Ready Message()
LIFE CYCLE OF MESSAGE DRIVEN BEANS Message Driven Beans (MDB) are stateless session beans that perform their tasks Asynchronously So an MDB is never passivated. It has only two states non-Existent and ready-to-receive messages The Ejb Container usually creates a pool of MDB instances the EJB Container performs tasks on it. At the end of the lifecycle the container calls the Method annotated pre. Destroy, if any the bean’s instance is then ready for garbage Collection.
REFERENCE “Middleware & Enterprise Integration Technologies” Book By-G. Sudha Sadasivam, Radha Shankarmani
Thank You!!!
- Slides: 49