Monday, April 16, 2012

What is an Enterprise Java Bean EJB and benefits of EJB

What is Enterprise Java Beans ( EJB ):

EJB is written in Java programming language and it is server side component that encapsulates the business logic of an java application from customers. Business logic code is managed in EJB that fulfills the purpose of the java applications. For example, in an eCommerce web application, the enterprise java beans might implement the business logic in methods called checkAvailableItem and orderProduct.

Benefits of Enterprise Java Beans EJB

EJB technology enables rapid and simplified the process of distributed, transactional, secure and portable java desktop applications development and Java ee web applications development because
  • EJB container provides System level services to enterprise java beans.
  • EJB developer just focus on business logic and on solving business problems.
  • Because business logic lies in EJB, so Front end developer can focus on the presentation of client interface.
  • The client developer does not have to code the routines that implement business rules or access databases. As a result, clients side has less codes which is  particularly important for clients that run on small devices.
  • Java Beans are portable components which enable the java application assembler to build new applications from existing java beans.
  • EJB is a standard API due to which applications build on EJB can run on any complaint Java EE web application server.

Types of Enterprise Java Beans:

There are two main types of  EJBs
1- Session Beans
2- Message Driven Beans

1- Session Beans

A session bean encapsulates business logic that can be invoked programmatically by a client over local, remote, or web service client views. A session bean is not persistent, means its data is not saved to a database. EJB Session Beans has three types which are

Stateful  Session Bean

In stateful session bean, 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.

Stateless Session Bean

In EJB stateless session bean conversational state is not maintained with 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.

Single tone Session Bean

A singleton session bean is instantiated once per application and exists for the whole lifecycle of the java application. A single enterprise bean instance is shared across all the applications clients and it is concurrently accessed by clients

2- Message Driven Bean (MDB)

Message Driven Beans ( MDBs ) also known as Message Beans. Message Driven Beans are business objects whose execution is triggered by messages instead of by method calls.

When to Use Enterprise Java Beans EJB

EJB can be used in any java application which are simple or complicated. Java applications can be developed with out EJB. Now answer to question " when to use EJB" is that  EJB should be used if your application has any of the following requirements.
  • For Scalable Java Applications: The application must be scalable. To accommodate a growing number of users, you may need to distribute an application’s components across multiple machines. Not only can the enterprise beans of an application run on different machines, but also their location will remain transparent to the clients.
  • For Transactional java applications:  In application transactions must ensure data integrity. Enterprise java beans support transactions, the mechanisms that manage the concurrent access of shared objects.
  • Java Application with different Client like Desktop, Web, Mobile etc: The java application will have a variety of clients. With only a few lines of code, remote EJB clients can easily locate enterprise java beans. These clients can be thin, various, and numerous.
Find below slides to learn more about Enterprise Java Beans.


1 comment: