Thursday, August 7, 2014

Introduction to EJB

What is EJB ?



  • Stands for Enterprise Java Beans.
  • Very important part in J2EE platform.
  • Provides components based development architecture.


What is a Bean ?


  • Simply, a bean is a component. 
  • Component is something like building block.


Why EJB ?


EBJ is a component based architecture design. It means it consist of components. When in a enterprise application, it is very easy to interact with this kind of design. EJB gives you a lot of services. So you need not to write them in your programs. Basically EJB let you to think about the business logic easily.  

Other thing is, you can customize and reuse your components at deploy time without touching the source code. This is a one of main advantages of EJB.

EJB services


EBJ gives you many services that you need not to code. 
  • Security
  • Concurrency
  • Networking
  • Transaction management
  • Resource management
  • Persistence
  • Messaging
  • Customization

Architecture of EJB




As you can see Enterprise beans are in EJB container. There are EJB objects also within the container. Later on we discuss about them. This is the very simple demonstration of the EJB container and how it works with client objects. Client can be a servlet or JSP or other Java application and we think he is a remote one. 

Think about a banking system. You have a bank account and a ATM card (Master card, VISA...etc) for your account. So you can get your money back using that ATM card from anywhere in the world. You can use your mobile phone to do transactions with your account. Then you are a remote client who are not directly connect to the main server and you are running on another JVM. EJB is very useful to do this kind of huge processing and transactions. I think now you have a question, how to connect remote clients as above image. It looks very simple, isn't it ? 




It is very easy to understand. Think if you are a client and you use your laptop and WiFi network to connect to the internet to connect to some kind of enterprise application to get some details from its database.   

EJB has highly thought about security. No one can directly connect with a Enterprise bean even you. You have to use EJB object to connect and access Enterprise bean. EJB object is like a bodyguard of a Enterprise bean. So clients have to  connect to Enterprise bean via EJB object. 


Image resource : http://blog.xebialabs.com/

But I have said that client is a remote one on the most of enterprise applications which is running on another JVM. Then it is not easy to connect with client and EJB object of the enterprise application, because client has to go through proxies, firewalls and other protocols. So how to handle these things ? 

These things are handled by Java RIM (Remote Method Invocation). It gives you a proxy object called "stub" to handle the communication with remote objects. This object act as a helper to the client object. It helps to programmer with promoting network transparency. So you need not to think about them.

There is another object in server side as well. It is called "skeleton object". This object is received the remote connection from remote stub and figure out what it says.



Types of Beans in EJB



  • Session beans
  • Entity beans
  • Message driven beans

Session beans

Session bean stores a data of a particular user in a session. There are two types of session beans.

  1. Stateless
  2. Stateful

Entity beans

This is use to represent a thing in a persistence store. You can use it to save data on database and retrieve data from it.

Message driven beans

This can be used only if you want JMS (Java Message Service) consumer. It listen for messages from JMS.