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 EJBEJB 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:
1- Session BeansA 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
Stateless Session BeanIn 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 BeanA 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)
When to Use Enterprise Java Beans EJB
- 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.