Monday, July 28, 2008

Enterprise JavaBean

JavaBeans are simple classes that follow a particular coding style (setter and getter methods). Enterprise JavaBeans (EJBs), must operate within the context of a container, are server-side components built to deal with the complexities of distribution. The EJB framework (container and server) allows user to focus on writing business logic code within the EJBs. The EJB server holds containers and provides generic EJB server services required by all containers, including transaction monitoring services, generic security services and administration service. Although the precise demarcation of responsibilities between the server and its containers is not defined by the EJB specification, the container to EJB interface is part of the specification. There are four basic elements that make up an EJB component:
  1. Business logic interface (also known as the object interface)
  2. Life cycle interface (also known as the home interface)
  3. EJB component class
  4. XML deployment descriptor
(since EJB 2.0, local life cycle interface and local business logic interface were introduced).

For every EJB component you must implement certain methods. These are called callback or life cycle methods. For example, a method is called just after a component instance is created, or just before it is destroyed, or just prior to its passivation (a form of serialization) or reactivation.

Although users are responsible for writing the life cycle and business logic, their implementation classes are generated automatically prior to deployment by container tools. XML files are written to describe the required distribution properties for EJBs. With an appropriate XML deployment descriptor editor, we are able to display and alter the deployment properties. Container tools read these XML deployment descriptor files and generate distribution code. Among the various properties there are some worth to mention:
Security - logical security role names are used. Allows user to say who is allowed to invoke which methods.

Transaction scope, persistence requirements, composition, and the type of EJB - e.g. indicate to the container the kind of EJB you have developed and chain of methods used in a transaction. If a persistent EJB, the parts of the EJB that should be synchronized with a table in an underlying RDBMS.

There are three kinds of EJBs:
Session EJBs - used to encapsulate business logic. Stateful session and stateless session EJBs are two variants of session beans. Their data is not synchronized automatically with an underlying persistent store by the container.

Entity EJBs - used to encapsulate business logic. Unlike the session EJBs, their data is synchronized. This synchronization can either be at the control of the container entirely (container-managed persistence), or can be partially the responsibility of the EJB code (bean-managed persistence).

Message driven EJBs - this is new to EJB 2.0. While session and entity EJBs support synchronous communication, message driven EJBs support asynchronous communication. It read message objects off an associated destination (a queue or topic).