GoAlfred Wiki

Wiki of Alfred Crombez

Edit

Overview

Edit

Introduction

Enterprise JavaBeans (EJB) is a server-side component architecture that simplifies the process of building enterprise-class distributed component applications in Java:
  • Scalable, reliable, and secure applications.
  • Not writing your own complex distributed component framework.
  • RAD for the server side.
  • Designed to support application portability and re-usability across vendor's enterprise middle-ware services.

EJB is a standard for building server-side components in Java. It defines an agreement (contract) between components and application servers that enables any component to run in any application server. EJB components (called enterprise beans) are deployable, and can be imported and loaded into an application server, which hosts those components.

The top three propositions of EJB are as follows:
  • It is agreed upon by the industry. Those who use EJB will benefit from its widespread use.
  • Portability is easier. The EJB specification is published and available freely to all. Since EJB is a standard, you do not need to gamble on a single, proprietary vendor’s architecture.
  • Rapid application development. Your application can be constructed faster because you get middle-ware infrastructure services such as transactions, pooling, security, and so on from the application server.

Edit

Service-Oriented Architectures

At the core of a service-oriented architecture lies the concept of service. A simplistic definition of service is a group of related components that carry out a given business process function.

A service-oriented architecture (SOA) is a paradigm focusing on development of services rather than piecemeal components such that these services provide a higher level of abstraction from a functional standpoint.

There are more properties to SOA than mere coarse-granularity, such as that services are autonomous in nature. These independent entities can interact with others in spite of differences in the way they have been implemented or the platform they have been deployed on. The notion of putting together (integrating) such autonomous and loosely coupled services to address the changing business needs has a huge value proposition and it is well on its way to realization with the emergence of various choreography, orchestration and collaboration technologies such as WS-BPEL, EbXML BPSS, and WS Choreography.

SOA is not a replacement for component architecture; rather it neatly complements the component architecture. While component architectures enhance re-usability at a finer grain level, SOA can enhance re-usability at a coarser grained level. Hence, from an implementation standpoint, a given service might very well be developed using well-defined component frameworks such as EJB. The latest EJB standard, therefore, has in-built support for Web Services, the most popular stack for building SOA.

Edit

EJB as a Business Tier Component

Enterprise beans are not intended for the client side; they are server-side components. They are meant to perform server-side operations, such as executing complex algorithms or performing high-volume business transactions. Server-side components need to run in a highly available (24/7), fault-tolerant, transactional, and multi-user secure environment. The application server provides this high-end server-side environment for the enterprise beans, and it provides the runtime containment necessary to manage enterprise beans.

EJB is used to help write logic that solves business problems. EJB components (enterprise beans) can perform any of the following tasks:
  • Perform business logic.
  • Access a database.
  • Access another system.

Examples of the clients that can connect to enterprise beans include the following:
  • Thick clients. Thick clients execute on a user’s desktop. They could connect through the network with EJB components that live on a server. Thick clients in Java include applets and applications.
  • Dynamically generated Web pages. Web sites that are transactional and personalized in nature need their Web pages generated specifically for each request. Core technologies such as Java servlets and Java Server Pages (JSP) live within a Web server and can connect to EJB components, generating pages differently based upon the values returned from the EJB layer.
  • Web Service clients. Some business applications require no user interface at all. They exist to interconnect with other business partners’ applications that may provide their own user interface.

Edit

The EJB Ecosystem

EJB encourages collaboration of more than six different parties. Each of these parties is an expert in its own field and is responsible for a key part of a successful EJB deployment. Following are the players form the EJB Ecosystem:
  • Bean Provider. The bean provider supplies business components or enterprise beans. These are not complete applications, but rather deployable components that can be assembled into complete solutions.
  • Application Assembler. The application assembler is the overall application architect. This party is responsible for understanding how various components fit together and writing the applications that combine components.
  • EJB Deployer. After the application assembler builds the application, the application must be deployed in a running operational environment. EJB deployers are aware of specific operational requirements, and understand how to deploy beans within servers and how to customize the beans for a specific environment.
  • System Administrator. Once the deployment goes live, the system administrator steps in to oversee the stability of the operational solution. The system administrator is responsible for the upkeep and monitoring of the deployed system and may make use of runtime monitoring and management tools the EJB server provides.
  • Container and Server Provider. The container provider supplies an EJB application server, the runtime environment in which beans live. The container supplies middle-ware services to the beans and manages them. The server provider is the same as the container provider (Sun has not yet differentiated these).
  • Tool Vendors. To facilitate the component development process, there should be a standardized way to build, manage, and maintain components. In the EJB Ecosystem, there are several IDEs that assist you in rapidly building and debugging components.

Edit

The Java 2 Platform Enterprise Edition (J2EE)

EJB is only a portion of a larger offering called the Java 2 Platform, Enterprise Edition (J2EE). The mission of J2EE is to provide a platform-independent, portable, multi-user, secure, and standard enterprise-class platform for server-side deployments written in the Java language.

J2EE is a specification, not a product. J2EE specifies the rules of engagement that people must agree on when writing enterprise software. J2EE is one of three different Java platforms. Each platform is a conceptual superset of the next smaller platform:
  • The Java 2 Platform, Micro Edition (J2ME) is a development platform for applications running on mobile Java-enabled devices.
  • The Java 2 Platform, Standard Edition (J2SE) defines a standard for core libraries that can be used by applets, applications, J2EEapplications, and mobile applications.
  • The Java 2 Platform, Enterprise Edition (J2EE) is an umbrella standard for Java’s enterprise computing facilities.

J2EE is a suite of middle-ware services for developing server-side applications. Some of the major J2EE technologies of J2EE 1.4 and EJB 2.1 are shown in following figure:
Image

J2EE Technologies


Some important technologies and APIs of J2EE:
  • Enterprise JavaBeans (EJB). EJB defines how server-side components are written and provides a standard contract between components and the application servers that manage them.
  • Java API for XML RPC (JAX-RPC). JAX-RPC is the main technology that provides support for developing Web Services on the J2EE platform.
  • Java Remote Method Invocation (RMI) and RMI-IOP. RMI is the Java language’s native way to communicate between distributed objects, such as two different objects running on different machines. RMI-IOP is an extension of RMI that can be used for CORBA integration.
  • Java Naming and Directory Interface (JNDI). JNDI is used to access naming and directory systems.
  • Java Database Connectivity (JDBC). JDBC is an API for accessing relational databases.
  • Java Transaction API (JTA) and Java Transaction Service (JTS). The JTA and JTS specifications provide components with reliable transaction support.
  • Java Messaging Service (JMS). JMS allows for your J2EE deployment to communicate using messaging within the J2EE system as well as outside the J2EE system.
  • Java servlets. Servlets are networked components used to extend the functionality of a Web server. Servlets are request/response oriented in that they take requests from some client host and issue a response back to that host.
  • JavaServer Pages (JSP). JSP technology is very similar to servlets since JSP scripts are compiled into servlets. The largest difference is that JSP scripts are not pure Java code and are much more centered on look-and-feel issues.
  • Java IDL. Java IDL is the Sun Microsystems Java-based implementation of CORBA.
  • JavaMail. The JavaMail service enables you to send e-mail messages in a platform independent, protocol-independent manner from your Java programs.
  • J2EE Connector Architecture (JCA). Connectors enable you to access existing enterprise information systems from a J2EE application. This could include any existing system, such as a mainframe system running high-end transactions, Enterprise Resource Planning (ERP) systems, or your own proprietary systems.
  • The Java API for XML Parsing (JAXP). JAXP is the de facto API for parsing XML documents in a J2EE application and is an implementation-neutral interface to XML parsing technologies such as DOM and SAX.
  • The Java Authentication and Authorization Service (JAAS). JAAS is a standard API for performing security-related operations in J2EE. It enables plugging in an authentication mechanism into a J2EE application server.

Edit

EJB Fundamentals

Edit

Enterprise Beans

An enterprise bean is a server-side software component that can be deployed in a distributed multi-tier environment. An enterprise bean can compose one or more Java objects because a component may be more than just a simple object. Regardless of an enterprise bean’s composition, the clients of the bean deal with a single exposed component interface.

This interface, as well as the enterprise bean itself, must conform to the EJB specification. The specification requires that beans expose a few required methods that allow the EJB container to manage beans uniformly, regardless of which container your bean is running in.

Edit

Types of Beans

EJB defines three different kinds of enterprise beans:
  • Session beans. Session beans model business processes. They are like verbs because they perform actions. The action could be anything, such as adding numbers, accessing a database, or calling a legacy system.
  • Entity beans. Entity beans model business data. They are like nouns because they are data objects—that is, Java objects that cache database information.
  • Message-driven beans. Message-driven beans are similar to session beans in that they perform actions. The difference is that you can call message-driven beans only implicitly by sending messages to those beans.

Following figure illustrates some of the many possibilities of clients interacting with an EJB component system:

Image

EJB Bean Types


Edit

Distributed Objects

EJB components are based on distributed objects. A distributed object is an object that is callable from a remote system. It can be called from an in-process client, an out-of-process client, or a client located elsewhere on the network.

Image

Distributed Objects

The figure above shows how a client can call a distributed object:
  1. The client calls a stub, which is a client-side proxy object. This stub is responsible for masking network communications from the client.
  2. The stub calls over the network to a skeleton, which is a server-side proxy object. The skeleton masks network communication from the distributed object.
  3. The skeleton delegates the call to the appropriate implementation object. This object does its work, and then returns control to the skeleton, which returns to the stub, which then returns control to the client.

A key point is that both the stub and the server-side implementation object implement the same interface (called the remote interface). This means the stub clones the distributed object’s method signatures. A client who calls a method on the stub thinks he is calling the distributed object directly; in reality, the client is calling an empty stub that knows how to go over the network. This is called distribution transparency.

Edit

Implicit Middleware

A distributed object environment requires middleware services such as transaction and security services. With implicit middleware, you can harness complex middleware in your enterprise applications without writing to middleware APIs:
  1. Write your distributed object to contain only business logic without writing to complex middleware APIs.
  2. Declare the middleware services that your distributed object needs in a separate descriptor file, such as a plain text file.
  3. Run a command-line tool provided by the middleware vendor. The tool takes the descriptor file as input and generates a request interceptor object.
  4. The request interceptor intercepts requests from the client, performs the middleware required by the distributed object and then delegates the call to the distributed object.

Following figure illustrates this flow:

Image

Implicit Middleware


The advantages of implicit middleware (also called declarative middleware) are:
  • Easy to write. The request interceptor provides the middleware logic transparently, allowing focus on the business code.
  • Easy to maintain. The separation of business and middleware logic is clean and maintainable. Changing middleware doesn’t require changing application code.
  • Easy to support. Customers can change needed middleware by tweaking the descriptor file.

Edit

The Enterprise Bean Class

The enterprise bean class is a Java class with the implementation of the bean’s logic. It conforms to a well-defined interface and obeys certain rules necessary for the bean to run in the EJB container.

In general, the session, entity, and message-driven bean class contain different logic:
  • For session beans, it contains business process related logic, such as logic to transfer funds between bank accounts.
  • For entity beans, it contains data related logic, such as logic to change the name of a customer.
  • For message-driven beans, it contains message-oriented logic, such as logic to receive a stock trade message and call a session bean that knows how to perform stock trading.

The EJB specification defines a few standard interfaces that a bean class needs to implement to expose certain required methods the EJB container calls to manage your bean and alert your bean to significant events. The most basic interface that all bean classes must implement is the javax.ejb.EnterpriseBean interface that serves as a marker interface; implementing this interface indicates that a class is an enterprise bean class. The interface extends java.io.Serializable, making an enterprise bean convertible to a bit-blob.

Session, entity, and message-driven beans each have more specific interfaces that extend the javax.ejb.EnterpriseBean interface (javax.ejb.SesionBean, javax.ejb.EntityBean, javax.ejb.MesageDrivenBean).

Edit

The EJB Object

Enterprise beans are not full-fledged remote objects. The client’s invocations are intercepted by the EJB container and then delegated to the bean instance. This allows the EJB container to automatically perform implicit middleware services such as:
  • Implicit distributed transaction management. The EJB container provides a transaction service with a low-level implementation of transaction management and coordination. The service is exposed through the Java Transaction API (JTA).
  • Implicit security. The Java SE platform yields a robust security service to authorize and authenticate users, securing deployments from unwanted visitors. EJB adds to this the notion of transparent security, allowing components to reap the benefits of a secure deployment without necessarily coding to a security API.
  • Implicit resource management and component life cycle. The EJB container implicitly manages resources for enterprise beans, such as threads, sockets, and database connections. The life cycle of the enterprise beans is also managed, allowing the EJB container to reuse the enterprise bean instances as necessary.
  • Implicit persistence. EJB offers assistance by automatically saving persistent object data to an underlying storage and retrieving that data at a later time.
  • Implicit remote accessibility. The EJB container handles networking by wrapping the bean in a network-enabled object.
  • Implicit support. EJB containers automatically handle concurrent requests from clients. They provide built-in thread support, instantiating multiple instances of an enterprise bean and pushing one thread through each instance. If multiple clients simultaneously invoke methods on a bean, the invocations are serialized, or performed lock step.
  • Implicit component location transparency. Clients of components are decoupled from the specific whereabouts of the used components.
  • Implicit monitoring. The EJB container can provide monitoring functions such as tracking invoked methods and gathering data for intelligent load balancing.

Thus, the EJB container acts as a layer of indirection between the client code and the bean. This layer manifests itself as a single network-aware object called the EJB object. It is a surrogate object that knows about networking, transactions, security, and more. It is the request interceptor between the client and the bean. EJB objects replicate and expose every business method that the bean itself exposes and delegate all client requests to beans as shown in following figure:

Image

EJB Object


Edit

Other Server Frameworks

Edit

Spring

Lightweight open source but non-standard Java framework.

Edit

Ruby on Rails

Ruby on Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control pattern. It covers from the Ajax in the view, to the request and response in the controller, to the domain model wrapping the database.

Edit

References

TheServerSide.COM Enterprise Java Community
Mastering Enterprise Java Beans - Third Edition
Enterprise JavaBeans Specification

ScrewTurn Wiki version 2.0.19. Some of the icons created by FamFamFam.