In the Controller layer, things can be improved beyond what we have explained thus far. We stated earlier in this chapter that often, Java classes are better suited for the logic that is needed by the Controller and the Model layers than either servlets or JSPs. What is the best form for these classes to take?
If an application lists scalability as a primary requirement, it is a candidate for the use of Enterprise JavaBeans (EJB). EJBs are classes that have been created according to a fairly strict pattern. They are not instantiated directly by other classes and servlets. EJBs reside in special pieces of software called Application Servers, or EJB Containers. They are instantiated by making a request to the container for an EJB to be instantiated.
These servers provide a variety of services for the classes that they host.
Life-cycle Management The EJB container is responsible for creating and destroying instances of your class (called an EJB), based on the demand.
Availability Some EJB containers can be clustered so that they can provide near 100% availability by redirecting a request to a different container if the first container is unable to respond.
Reliability EJB containers are capable of handling most errors without closing the container. They can retry a request that did not complete.
Security EJB containers can provide authorization as well as encryption/decryption services.
Transaction Support EJB transaction is managed at the container level, enabling you to create multi-EJB transactions.
Distribution Services EJB can call other EJBs, even when they are located on another server.
Threading EJBs run in their own threads automatically.
Persistence EJBs can be written so that they automatically update a database when they are changed.
There are two varieties of EJBs, Session Beans and Entity Beans. Session Beans are expected to maintain state for only one session or instantiation. Entity Beans represent databases and they are expected to maintain state across multiple sessions just like a database does.
The downside to the EJB architecture is the cost. Instead of being a free download from a vendor, you normally purchase the container. Prices vary, but it is not unusual to spend thousands or tens of thousands of dollars on license fees. By the time you pay for training, you have a sizeable amount of money invested. This investment is recouped in both the development and testing phase, however. Development is faster because the container provides so many of the programming tasks. Testing is easier because there is less code to test. Figure 26.9 shows the effect of developing with EJBs on the MVC architecture diagram.
Figure 26.9. EJBs occupy both the Controller layer and the Model layer of the MVC architecture.
The use of EJBs instead of ordinary Java classes in the Controller and Model layers of the MVC architecture can improve your system in many ways. The cost/benefit analysis only works for fairly large systems that have a strong scalability requirement.