EJB is a enterprise component, part of J2EE framework, which allows to create independant components to implement business logic or represent Data.
2. What are the Advantages of EJB
- Component Driver architecture, so plug and play to any project
- With local , remote interfaces present, the EJB itself does not have to know who the client is whether an RMI or local Servlet or something else. Developer have to focus on writing business logic, rest everthing like Remote access, Session management , Security is taken care by the framework.
- Session Beans - It has two types : Statefull and Statless session beans
Stateless Session Beans : They behave like POJO classes, just write methods, they do not maintain session or state across the requests from the user.
Following is a Simple Example of EJB3.0 Stateless Session bean
Step1 : Write the Local and Remote Interfaces
package com.abc; import javax.ejb.Remote; @Remote public interface CalcBeanRemote { public int getRate(); }
Step2 : Write the Stateless Session Bean
package com.abc; import javax.ejb.Stateless;
@Stateless public class CalcBean implements CalcBeanRemote, CalcBeanLocal { /** * Default constructor. */ public CalcBean() { // TODO Auto-generated constructor stub } public int getRate(){ return 75; } }
- Entity Beans : Provides the domain as well as DAO layer to the enterprise application. In EJB2.0 with CMP and BMP they were too complicated, Hence Hibernate was used for the database Interaction.
- With EJB3, JPA has been introduced which is even better than Hibernate as it uses annotations. There is a good article on Java world about JPA. http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html
- Message Driven Beans (MDB) : They execute the business logic on the OnMessage(Message obj) method , they listen to JMS Queues to get invoked. In the Deployment Descriptor(ejb-jar.xml , we mention the Queue to listen to)
Following are the steps to invoke a Ejb method
- 1.Get hold of EJB HOME through JNDI Lookup.
Note: EJBHome has all the create methods to create the EJBObject
- 2. Create instance of EjbObject from Home
Note: ejbObject is an interface and just provides the signatures of the all the methods in EJB itself.
- 3. Invoke the method on the EJB.
5. What are Local and Remote interfaces in EJB and their importance..
Since EJBObjects are the interfaces which expose themselves to the client, for a remote client, we need a Remote Interface. And for local clients like Servlets we need Local Interfaces.
We two different because, we do not want have a overload of a Remote interface if the invocation is loca.
6. What version of EJB you have used?
I have used EJB2.x, lates is EJB 3.0 which makes heavy use of Annotations, thus eliminating the need of Home and EJBObject interfaces.
7. What is the Directory Structure of EJB Jar.
EjbProject --> EjbHome.java
EjbObject.java
Ejb.java
ejb-jar.xml (Deployment Descriptor which metions various settings like EJB Type, connection params, Listening queue etc)