Monday 12 September 2011

EJB 3.0

Enterprise Applications:

                 The Enterprise Applications generally are very large scale S/W apps that are developed  intended to  use at large scale involving concurrent access by many users. Hence the s/w needs to take care of concurrency control, integrity of data, security, persistence and other issues.  

                        The Java Enterprise Edition is a very powerful but complex platform to develop server side applications. So the  Enterprise JavaBeans are introduced to make the development of enterprise applications simple. The motive behind introducing EJB is to make the EAS development easy by providing the features like Transaction management,resource and life cycle management, Concurrency Control, Security, Remote Accessibility(using RMI), Clustering and load-balancing so that the Application developer can concentrate on developing the business functions of the Application. 

EJB Definition: 
                       Enterprise beans are the Java EE server side components that run inside the EJB container and encapsulates the business logic of an Enterprise Applications.
                    The EJB specification was originally developed in 1997 by IBM and later was adopted by the Sun Microsystems which developed EJB 1.0 and 1.1. Then the Java Community Process developed the EJB 2.0, 2.1, 3.0 ans 3.1 versions of the EJB. 

                    In the rest of the post i would like to share details of EJB 3.0 with some details of the Enterprise Beans, the difficulties in implementing them and then the EJB improvements.                  

 Benefits Of  Enterprise Beans: 
  • Bean Developers can Concentrate on solving the business problems as the Bean containers provide system level services like transaction management, security and others. 
  • The Client developer can focus on the presentation of the application as the business logic is taken care by the bean developer.
  • The Beans developed once can be used many times on JEE compliant servers.
Types of Enterprise Beans:

              -> Session Beans
                        - Stateful
                        - Stateless
              -> Entity Beans 
              -> Message-Driven Beans

Session Beans:

              There is only one client(just like in interactive session) invoking method of a Session bean. The session bean then performs the task for the client shielding it from the complexity of implementation of task.
A session Bean is:
       - Not Shared
       - Not Persistent
      - When the client terminates, its session bean terminates and is no longer associated with the client 
Sateful Session Beans:
     Instance variables represent the state of a unique client-bean session (called conversational state). The state is retained during the session between the Client and the bean and is ended once the session is over or the Bean terminates.

Sateless Session Beans:
            A stateless session bean does not maintain a conversational state for a particular client. When a client invokes the method of a stateless bean, the bean's instance variables may contain a state, but only for the duration of the invocation. When the method is finished, the state is no longer retained. 
        Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. This helps this bean to support multiple clients. This results in better scalability for applications that require large numbers   of clients.
Entity Beans:
      An entity bean represents a business object in a persistent storage mechanism.

        Examples of business objects - customers, orders, and products.
Entity beans are: 
                Persistent
                Allow shared access
                Have primary keys
                May participate in relationships with other entity beans.


Message-Driven Beans:
 A message-driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. The messages may be sent by any J2EE component--an application client, another enterprise bean, or a Web component--or by a JMS application or system that does not use J2EE technology.
clients do not access message-driven beans through interfaces.

 In several respects, a message-driven bean resembles a stateless session bean:
  • A message-driven bean's instances retain no data or conversational state for a specific client.
  • All instances of a message-driven bean are equivalent, allowing the EJB container to assign a message to any message-driven bean instance. The container can pool these instances to allow streams of messages to be processed concurrently.
  • A single message-driven bean can process messages from multiple clients.
 Generally, a client may access a session or an entity bean only through the methods defined in the bean's interfaces. These interfaces define the client's view of a bean. All other aspects of the bean like method implementations, deployment descriptor settings, abstract schemas, and database access calls are hidden from the client.

Difficulties in implementing the EJB before EJB 3.0: 

Developing EJB before release of EJB 3.0 was not so easy because of some unnecessary steps involved that are usually unused. 
  • A set of three source files must be create
  • Creating multiple xml deployment descriptors
  • Implementing several callback methods
  • Throwing several types of exceptions
  • EJB-QL is limited in functionality and difficult to use

The EJB 3.0 Specification:       
The EJB 3.0 specification has come up with improvements in implementing the above stated bean types.  
The EJB 3.0 release is focused on a simplification of the Enterprise JavaBeans architecture from the developer’s point of view.
The main features of the EJB 3.0 are: 

--> Simplification of the interface definition requirements for enterprise beans: Elimination of requirements for the specification of home and component interfaces in the EJB 3.0 programming model.
--> Simplification of the contractual requirements between the bean provider and the container: Elimination of the requirements for enterprise beans to implement the javax.ejb.EnterpriseBean interfaces.
--> Simplification of APIs for access to a bean's environment: Definition of a dependency injection facility and simpler look-up APIs.
--> Introduction of Java metadata annotations to be used as an alternative to deployment descriptors.
--> Simplification of object persistence by the definition of a light-weight object/relational mapping facility based on the direct use of Java classes rather than persistent components.

    
With these simplifications, the enterprise development became easy as the bean developer need not worry about writing code for implementing home and other interfaces.

Another major change came with the introduction of the Java Persistence APIs which replaced the Entity beans of the EJB. Hence in EJB 3.0 there are only two types of beans(Session and message driven). The implementation of session and message-driven beans also became easy. The session bean no longer requires to implement the javax.ejb.SessionBean interface. 


The new entity beans are just the POJO(Plain Old Java Objects). The entity relationships are also defined through annotations. In addition, O/R mapping is also done through annotations, and support for several database-specific operations is provided. 

Also the metadata annotations replaced the deployment descriptors. This reduces the burden on the bean provider to specify the methods to the container, instead the same functionality is achieved using the Dependency Injection where the Bean provider informs the container of the methods and interfaces that the bean will use in advance and the container injects these methods when the bean is implemented.    

CONCLUSION:
                       All EJB versions before 3.0 (released in 2006) share common issues that made EJB a hard to learn and use technology. For example, preparing JavaBeans to run in manageable EJB Container required from developers using variety of demanding API. That leads to significant waste of time just for handling mechanisms that simply have nothing to do with business logic. Approach introduced in earlier EJB version was too complicated to efficiently create enterprise applications. 

New EJB 3.0 implementation: avoided using EJBHome design pattern, improved EJB Container mechanisms responsible for creating component references and searching over JNDI, session components are now plain, old java objects (POJO) that can implement just a business interfaces. EJB 3.0 is easier with fewer program artefacts and intuitive defaults. These and some others improvement in EJB standard made from it a great technology ready to use in development of enterprise applications. Having learnt from the earlier EJB versions mistakes, EJB 3.0 specification attracts professional java developers to study it and ease development process of JEE applications.