Objects in the deployment layer—called deployment wrappers—are the heart of J2EE architecture. Deployment wrappers publish business object functionality to Java classes that could be on separate machines (including anything in the presentation tier). Examples of deployment wrappers include enterprise beans (e.g., session beans, message-driven beans), RMI services, and Web services. CORBA services are also deployment wrappers but are not considered a traditional part of J2EE architecture.
I consider entity beans to be DAOs, not deployment wrappers. Although technically you can call entity beans from remote clients, doing so slows performance and is therefore not recommended. Calls to entity beans are usually consolidated in session beans. This practice is known as a session bean façade.
Use enterprise beans to publish business logic. Some developers consider enterprise beans to represent business logic. Although it is technically possible to consolidate what I call business objects and enterprise beans into the same class, I don't recommend it. Keeping the business logic separate gives you complete deployment flexibility. You can deploy the same business object as an enterprise bean, message-driven bean, a CORBA service, a Web service, or even a client application with no changes to the underlying business object. Separating the business logic into deployment-independent classes does create additional classes, but the classes are less complex.
Document a deployment wrapper as one class in your object model, even though it's composed of several classes. Take a stateless session bean example. A session bean has a client proxy, an interface, a bean, and a home object implementation. With the exception of the home implementation, all these classes will contain the same method signatures. There's no value in tediously documenting every piece of the deployment layer.
You can have multiple deployment wrappers for the same business object. For example, it is not uncommon to create a message-driven bean and a Web service deployment for a customer object.
Each type of deployment wrapper has its advantages and disadvantages. Choosing which type of deployment wrapper to use is difficult if you haven't substantially completed use-case analysis and prototyping (chapter 3) or defining application interfaces (chapter 4). Table 5.3 summarizes the differences between the types of deployment wrappers.
|
Feature |
EJB |
Web Services |
Messaging/JMS |
RMI |
HTTP |
CORBA |
|---|---|---|---|---|---|---|
|
Caller platform requirements |
Java-compliant only |
Any |
Any |
Java-compliantonly |
Any |
Any |
|
Communication method supported |
Synch. only |
Both |
Both |
Synch. only |
Synch. only |
Synch. only |
|
Coupling |
Tight |
Loose |
Loose |
Tight |
Loose |
Loose |
|
Transaction support |
Local and JTA |
Local and JTA |
Local |
Local |
Local |
Local |
|
Requires J2EE container? |
Yes |
No |
No |
No |
No |
No |
|
Supports clustering for scalability and availability? |
Yes |
Yes |
Yes |
No |
Yes |
Yes |
Faced with deciding which deployment wrappers to use and which business objects to publish, ask yourself the following questions; your answers will help you navigate through the decision-making process.
|
Is your business object called from applications that aren't Java? | ||
|
Is your business object likely to be used by dynamic HTML page constructs, such as servlets and JSPs? | ||
|
Does your business object receive and process messages via JMS? | ||
|
Does your business object require two-phase commit functionality? |
Answers
The pattern commonly used for deployment wrappers is a combination of the session façade pattern and the proxy pattern. This combination (or slight variations thereof) works for all the deployment wrappers that I can think of. The session façade pattern has been especially popular with EJB deployments, but the concept is valid for most types of distributed objects.
One of the primary objectives of the session façade pattern is to minimize network traffic. Figure 5.8 illustrates how you can effectively combine the session façade and proxy patterns.
Although not required by the session façade pattern, I like to provide client proxies for my distributed services, such as enterprise beans, RMI services, and CORBA services. This makes objects in the deployment layer easier to use because it eliminates the need for a caller in the presentation layer to care about wrapper-specific deployment details. Figure 5.9 depicts enterprise beans deployed in a session façade pattern.