Tuesday, November 5, 2013

EJB Most Wanted Questions

1. What is EJB?
-------------------
Enterprise JavaBeans (EJB) is the server-side component which runs on application server developed for the purpose of distributed and enterprise level application. Container/Server will provide support for system level services like Transaction Management, security which make developer task easy and he can focus on business logic.

2. What are the different types of EJB?
----------------------------------------------
There are 3 types of enterprise beans, namely: Session bean, Entity beans and Message driven beans.

Session bean :
A session bean represents a single client inside the Application Server. To access an application that is deployed on the server, the client invokes the session bean’s methods. The session bean performs work for its client, shielding the client from complexity by executing business tasks inside the server.

Two type of session bean:

 Stateless : A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean’s  instance variables may contain a state specific to that client, but only for the duration of the invocation. 

 Stateful: The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a   unique client-bean session. Because the client interacts (“talks”) with its bean, this state is often called the conversational state.The state is retained for  the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears. This transient nature of the state is not a problem, however, because when the conversation between the client and the bean ends there is no need to retain the state

 Message Driven Beans: these beans are work as a listener for messaging services like JMS .


 3. How EJB Invocation happens?
 ----------------------------------------
 Step 1: Retrieve Home Object reference from Naming Service via JNDI.
 step 2: Return Home Object reference to the client.
 step 3: Create me a new EJB Object through Home Object interface.
 step 4: Create EJB Object from the Ejb Object
 step 5: Return EJB Object reference to the client.
 step 6: Invoke business method using EJB Object reference.
 step 7: Delegate request to Bean (Enterprise Bean

 
 4. session bean life cycle callbacks  methods
 ------------------------------------------
 The EJB session bean life cycle methods are called by the EJB container to notify a session bean instance when specific life cycle events occur.

 ejbCreate():  Called just after the session bean instance is created. in response to a client calling create() on the bean's home interface.
 
 ejbRemove() : Called just before a session bean instance is permanently destroyed, in response to a client calling remove() on the bean's remote interface or on the bean's home interface
              
ejbPassivate() : Called just before the container passivated the bean by storing the bean data (typically serializing the bean) and removing the bean instance from memory. The container passivated a bean in order to conserve memory and other resources. The container is prepared, however, to reactivate the bean automatically as soon as it is needed again. 

ejbActivate() : Called just after the container has reactivated a bean that was previously passivated.
 
 5. EJB Entity Bean Life Cycle callbacks Methods
 -----------------------------------------------------------
  The EJB entity bean life cycle methods are called by the EJB container to notify an entity bean instance when specific life cycle events occur
 
ejbCreate() : Called just after the entity bean instance is created, in response to a client calling create on the bean's home interface. The return  PrimaryKeyType depends on the kind of persistence that is used:             
                Container-Managed Persistence -- returns null
                Bean-Managed Persistence -- returns the primary key for this bean instance.
 
ejbPostCreate() : Called after the entity bean is fully initialized. At this stage both the bean data and the primary key are initialized irrespective of whether container-managed or bean-managed persistence is used.
  
ejbRemove() :  Called just before an entity bean instance is permanently destroyed, in response to a client calling remove() on the bean's remote interface or on the bean's home interface.   
  
ejbPassivate()  : Called just before the container passivates the bean by storing the bean data (typically serializing the bean) and removing the bean instance from memory.   
 
ejbActivate() :  Called just after the container has reactivated a bean that was previously passivated.
 
ejbLoad()  :  Load the entity bean state from the database. Typically, the container calls this method at the start of a transaction to ensure that the state of the bean in memory is synchronized with the state in the database.

ejbStore() :   Store the entity bean state in the database. Typically, the container calls this method at the end of a transaction to update the  bean state in the database.    

ejbFind() :  The ejbFind() methods need only be defined on the entity bean class.

setEntityContext : particularly for BMP
 
unsetEntityContext : particularly for BMP

   
6. Life Cycle of a Stateless Session Bean
--------------------------------------------------- 
Does not exist: In this state, the bean instance simply does not exist.

Ready state: When Server is first started, several bean instances are created and placed in the Ready pool. More instances might be created by the container as needed by the EJB container.

Brief :
Stales session bean has short life cycle it can have two stage does not exist and ready stage. ejb container create the instance of stateless session bean and call setSessionContext () and ejbCreate() method.Now the bean is ready to invoke business method on this.it will not maintain the state so remove () method is been called after completion of business method which in turns  call ejbRemove  and now its ready for  garbage collection.

              
7. Life Cycle of a Stateful Session Bean
----------------------------------------------
 Does not exist: In this state, the bean instance simply does not exist.

 Ready state: A bean instance in the ready state is tied to particular client and engaged in a conversation.

 Passive state: A bean instance in the passive state is passivated to conserve resource.

Brief :
Stateful session beans life cycle starts when  client call create() method.The container create the instance of session bean and call setSessionContext() and ejbCreate() method. Now the stateful session bean is ready to serve the client request after serving the request if it is not used after a long time container can move this bean to passive stage by calling the ejbPassivate() method. similarly when bean is in passive stage and client invoke the business method the container call ejbActivate() method to move bean from passive stage to active or ready stage. At the end of life cycle client call remove() method and container will call ejbRemove() method and bean is ready for


8. Life Cycle of an Entity Bean
-----------------------------------
Does not exist: In this state, the bean instance simply does not exist.

Pooled state : When WebLogic server is first started, several bean instances are created and placed in the pool. A bean instance in the pooled state is not tied to particular data, that is, it does not correspond to a record in a database table. Additional bean instances can be added to the pool as needed, and a maximum number of instances can be set.

Ready state: A bean instance in the ready state is tied to particular data, that is, it represents an instance of an actual business object.              
 
Brief :
First stage is Does Not Exist Stage then Container creates the instance of EJB and  call SetEntityContext() method  which will set all entity context to bean and now it will become available on pool,to get a particular identity of an EJB object it has to move from Pooled stage to ready stage which is done by calling the create () method which in turns call ejbCreate() and ejbPostCreate() method .There is another way by which directly entity bean can move to pooled stage to
ready stage that’s is call ejbActivate() method. now we are ready to invoke method of entity bean .After completion of method if we want to move again in pooled stage from ready stage we can call remove() method which in turns call ejbRemove() or directly call ejbPassivate () method. At the end container remove the instance from pool and call unSetEntityContext().



9.  Message-Driven Bean Life Cycle callbacks Methods
---------------------------------------------------------------------
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

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

Brief :
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 meta-data 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.
 
 
10. What happens if remove( ) is never invoked on a session bean?
--------------------------------------------------------------------------------
In case of a stateless session bean it may not matter if we call or not as in both cases nothing is done. The number of beans in cache is managed by the container. In case of Stateful session bean, the bean may be kept in cache till either the session times out, in which case the bean is removed or when there is a requirement for memory in which case the data is cached and the bean is sent to free pool.

11.  can we have static initializer Block in EJB.
--------------------------------------------------------
From Technical point view of java its correct but violation of specification. In EJB if we use static initializer block to initialize static field then EJB components are used in distributed environment mean run on different JVM then it will be a problem coz if we change or update the value in on environment then only the instance running on same JVM have new value. that’s why static blocks are avoided and also all static field should be final. and same thing we can
achieve in ejbCreate(), setSessionContext() or setEntityContext() methods.


12.Threading is possible in EJB?
----------------------------------------
 Not possible because EJBs are created and managed by container and if in ejbs we allow threading containers life cycle methods will be interrupted by us.

13. What about connection pooling feature of EJB container?
-------------------------------------------------------------------------
Connection pooling is one of the Advance feature of container which enhanced our application performance .Using connection pooling mechanism client are not required to create every time connection object to interact with database.Whenever a client request for a database connection then an instance is picked from the connection pool to get an access to database and when user complete with his work instance is returned to connection pool.

14. What are the transaction Attribute ?
------------------------------------------------
A transaction attribute controls the scope of a transaction. Transaction is group of operation should be performed either completely or none. transaction must  have ACID property(atomicity , consistency, integrity,durability) so transaction can be said completed if we commit on successful execution and rollback on  unsuccessful execution.

A transaction attribute can have one of the following values

Required : the container starts a new transaction before running the method.

RequiresNew : If the client is not associated with a transaction, the container starts a new transaction before running the method or if associated Suspends transaction and Starts a new transaction. After Delegates the call to the method Resumes the client’s transaction.

Mandatory :  If the client is running within a transaction then method executes within the client’s transaction. If the client is not associated with a transaction, the container throws the TransactionRequiredException.

NotSupported : If the client is running within a transaction then container suspends the client’s transaction

Supports : If the client is running within a transaction then method executes within the client’s transaction. If the client is not associated with a  transaction, then container does not start a new transaction before running the method.

Never : If the client is running within a transaction and invokes the enterprise bean’s method, the container throws a RemoteException.



15.Difference Between ejbStore() and ejbLoad()?
------------------------------------------------------------
 ejbLoad method load or refresh the instance variable from the database. This method is called when it is necessary to synchronize the bean with data from the database

 ejbStore method writes the variables to the database.This method is called when it is necessary to synchronize the bean data with the database.


16 .Difference between JNDI , Initial , Session , Entity and Ejb Context.
----------------------------------------------------------------------------------------
JNDI context: provides mechanism to lookup resources on network.

Initial Context: it provides initial context to resources.

Session Context: is an EJBContext object provided by the EJB container. it has all the information available which is required to session bean

Entity Context :  is an EJBContext object provided by the EJB container. it has all the information available which is required to entiry bean


17. Transaction Management for EJBs
------------------------------------------------------
Container Managed Transactions - In this type, container manages the transaction states.

Bean Managed Transactions - In this type, developer manages the life cycle of transaction states.


18. Home ,Remote , Local interface
-------------------------------------------
Home Interface : home interface specifies how the server will create the bean, using the EJBCreate() method of the bean implementation.
Local Interface :  Allow clients to access bean if reside in same JVM,  pass parameter by reference
Remote Interface : Allow clients to access bean if reside in different JVM,  pass parameter by value

19. What Stubs and Skeletons
------------------------------------
 A stub is a proxy for a remote object that runs on the client computer. Stubs forward a client's remote method invocations and their associated arguments  to skeletons. he purpose of stub is marshalling the data. converting the java code in network oriented stream. stub classes needed by EJB applications are  generated dynamically at runtime when an EJB client needs them.

 A skeleton is a proxy for a remote object that runs on the server. converting the network oriented stream into java code.

 20 For session beans, we can use the SessionSynchronization interface. For entity beans, how do we have control over a transaction?
 ---------------------------------------------------------------------------------------------------------------------------------------
 SessionSynchronization interface is used by the Session beans to Synchronize the Instance variables after a rollback or after a commit operation,  because container does not have any other way to inform the bean of these operations.

 For Entity beans Container automatically calls the ejbLoad method that refreshed the values from the database.
 
21. Why is ejbFindByPrimaryKey mandatory?
------------------------------------------------------------
ejbFindByPrimaryKey is a method used to locate and load an Entity Bean into the container.Purpose is to reduce duplication of bean.

22. Why do we have a remove method in both EJBHome and EJBObject?
----------------------------------------------------------------------------------------------
EJBHome version of the remove allow us to delete an entity bean without instantiating it
Remote interface version works on an entity bean that you have already instantiated.

23. What restrictions are imposed on an EJB?
---------------------------------------------------------------
1. An enterprise Bean must not use read/write static fields. Using read-only static fields is allowed.
2. An enterprise Bean must not use thread synchronization.
3. An enterprise Bean must not use the AWT
4. An enterprise bean must not use the java.io package
5. An enterprise bean must not attempt to listen on a socket
6. The enterprise bean must not attempt to load a native library.
7. The enterprise bean must not attempt to manage threads.

24. Is it possible to share an HttpSession between a JSP and EJB?
-----------------------------------------------------------------------------------
 we can pass the HttpSession as parameter to an EJB method

No comments:

Post a Comment

AWS Services

      1.         Identity Access Management (IAM): Used to control Identity (who) Access (what AWS resources).                   1....