Monday, April 18, 2011

EJB 3 Overview

An EJB platform is for building portable, reusable and scalable business applications using the Java programming language. EJB is a component or framework that lets you build enterprise Java applications without having to revent services such as transactions, security, automated persistence which allows you to to spend more time on the business logic than the infrastructure code.
EJB 3 is simpler and lighter than its previous versions, EJB 3 components are now more like Plain Old Java Objects (POJO) with special powers. There are three types of EJB components
  • Session Beans (Stateless and Stateful)
  • Message-Driven Beans
  • Entities
Both session and message-driven beans implement business logic, where as entities are used for persistence.
EJB components execute in a specialized runtime environment called the EJB Container, an example of this would JBoss but there are many others. The EJB Container can provide business logic, managing application state, storing and retrieving information from a relational database, managing transactions, implementing security, performing asynchronous processing, etc.

Since Java 5 metadata annotations can be used to preconfigure the EJBs by specifying the type of service to add when the container deploys the EJBs. Metadata annotations dramatically simplify development and testing of applications without having to depend on external XML configuration file, you can declaratively add services to EJB components as and when they are need, you can also be used to specify other EJB components.

EJB allows you to build applications using two different layered architectures
  • Traditional four-tier architecture
  • Domain-Driven Design (DDD)
The traditional four-tier architecture is pretty popular, you have the presentation layer which the client uses, the business logic layer which is the heart of the application, the persistence layer which provides a high-level object-oriented (OO) abstraction over the database layer and finally the database layer which consists of a RDBMS like Oracle, DB2, MySQL, etc. EJB fits into the business logic layer (session and message-drive beans) and the persistence layers (entities).

The Domain-Driven Design is a new concept, it emphasizes that domain objects should contain business logic and should not be just dumb replica of database records. Domain objects are known as entities in EJB 3 and these may contain business logic, which means entities defined by EJB 3 Java Persistence API (JPA) supports OO features, such as inheritance and polymorphism.
There are a number of reasons that you would choose EJB 3 over other products like Spring, that's not to say you cannot implement both
  • Ease of Use - POJO, use of annotations, sensible defaults, JPA
  • Integrated solution stack - persistence, messaging, scheduling, remoting, web services, dependency injection(DI), interceptors
  • Open Java EE standard - Allows for broader vendor support
  • Broad vendor support - Oracle, IBM, JBoss, Geronimo
  • Stable, high-quality code - There have been many advances with EJB due to the companies involved like Oracle, IBM and JBoss
  • Clustering, Load Balancing and Failover - Highly scalable and Highly available solutions are available and provided by the vendors