Why Use EJBs?

Despite the recommendations of the J2EE Blueprints, the use of EJBs is not mandatory. You can build very successful applications using servlets, JSPs or standalone Java applications.

As a general rule of thumb, if an application is small in scope and is not required to be highly scalable, you can use J2EE components, such as servlets, together with direct JDBC connectivity to build it. However, as the application complexity grows or the number of concurrent users increases, the use of EJBs makes it much easier to partition and scale the application. In this case, using EJBs gives you some significant advantages.

Hiding Complexity

Early middleware environments, such as “raw” CORBA, require the application developer to write a lot of code that interacts with the CORBA environment and facilitates the connectivity and registration process. Such code can be likened to the plumbing that pipes water around a house. It needs to be there but, as the user of a sink or shower, you do not want to be intimately involved with it. In J2EE application terms, business developers want to write business code, not “plumbing” code. The EJB model tries to reduce such interaction to a minimum by using the following mechanisms:

  • Each bean conforms to a defined lifecycle and set of rules. This provides a distinct boundary between system code and application code.

  • Declarative attributes allow a developer to specify, say, the transactional behavior of the component without having to write code to control such functionality.

  • The deployment information provided with the deployable J2EE application provides information about the relationships between multiple EJBs and also defines the resources required by an EJB.

Separation of Business Logic from UI and Data Access

One of the key facets of applying EJBs is that they allow business functionality to be developed and then deployed independently of the presentational layer. You might, for example, create an application with a user interface built using Java's Swing API. This application might then provide access to some business functionality for the employees working on the company's internal network. If the underlying business functionality is implemented using EJBs, a different user interface could take its place without having to redevelop the entire application. A Web-based interface that used servlets would make the application available from the Internet without having to change a single line of code in the business functionality. Figure 4.1 is a UML component diagram that shows this. (More information on UML can be found in Appendix A,“An Introduction to UML,” on the accompanying CD-ROM.)

Figure 4.1. An application implemented using EJBs can have more than one user interface.


It can sometimes be difficult to distinguish between the functionality that an application provides and the user interface that is used to invoke that functionality. This is not unexpected because many common applications—such as a word-processor—are single-tier; the presentational logic and the business functionality are a single entity. On the other hand, consider programming a video recorder. Most modern video recorders can be programmed either directly on the console or through a remote control unit. Either user interface will accomplish the task of recording your favorite TV show, but there is only a single “application.”

Consider another example. In most supermarkets, a cashier is responsible for scanning the items in your shopping cart and then requesting a payment for the total. However, some supermarkets also offer a trust system, whereby the customer scans the items with a mobile scanner as they place the item into the shopping cart. To pay for the goods in the shopping cart, the customer simply swipes his or her own card, and then leaves with the goods. Again, there is a single application (to purchase shopping items) but two different interfaces—the cashier's till and the customer's mobile scanner.

To implement a distributed application using EJBs, make sure you have distinguished between the user interface and the underlying business function. The EJB itself is concerned only with the latter of these.

Container Services

The container provides various services for the EJB to relieve the developer from having to implement such services, namely

  • Distribution via proxies— The container will generate a client-side stub and server-side skeleton for the EJB. The stub and skeleton will use RMI over IIOP to communicate.

  • Lifecycle management— Bean initialization, state management, and destruction is driven by the container, all the developer must do is implement the appropriate methods.

  • Naming and registration— The EJB container and server will provide the EJB with access to naming services. These services are used by local and remote clients to look up the EJB and by the EJB itself to look up resources it may need.

  • Transaction management— Declarative transactions provide a means for the developer to easily delegate the creation and control of transactions to the container.

  • Security and access control— Again, declarative security provides a means for the developer to easily delegate the enforcement of security to the container.

  • Persistence (if you want)— Using the Entity EJB's container-managed persistence mechanism, state can be saved and restored without having to write a single line of code.

All of these container services are covered in more detail as the book progresses.

Now that you know why you would want to use an EJB and how to apply it, you can examine the inner workings of an EJB to understand how all the parts fit together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.34