EJB Best Practices: Improved Remote Object Design

xiaoxiao2021-03-06  19

How to help avoid mason Levels: Intermediate Brett McLaughlin (Bret @oreilly.com) writer and editor, O'Reilly & Associate 2002 In this new series of articles, you will learn how to optimally design And optimize Enterprise JavaBeans components, how to reduce RMI communications and JNDI access, and the most efficient use of enterprise applications. In each technique, the main authority of the company Java programming Brett McLaughlin will provide best practices or design patterns. Many skills will be built on the previous article. It is recommended that you read these techniques in order, because they will help you build a policy and design method that helps your own enterprise application programming. In this articles (also the first) Skills, Brett demonstrates how business interface modes can improve remote object design. If you have developed a lot of EJB components, you know the difficulty of designing remote object design. The core of the distribution EJB component is to separate the implementation of the bean (represented by the bean class) with its interface (represented by a remote, or local interface). The interface is disclosed to the client, and is usually used in the JVM different from the actual implementation class. This means that they have little dependencies between them at this level; it means that there may be some methods that are not implementing in the interface, and vice versa. For example, implementation of a utility method that does not have to be placed on a remote interface. Warning: Do not use this model with entity beans! The business interface mode is only available for session beans and message-driven beans; this mode does not apply to entity beans, which should never disclose directly to the application layer. In fact, the business interface mode will stop working in the EJB 2.0 entity bean because these classes are declared as Abstract, so they cannot be selected. So far, there is nothing to listen to not being managed, but you have to realize that the mismatch in the method is unintentional, and the situation will change. When deploying the bean, you will recognize that this is needed, and this happens naturally. Add the method to the Bean implementation class, but then forget to add it to the remote interface. Now you have added the necessary features, but there is no way to let the remote user truly access it. The business interface mode is required at this time. Note: Business Interface Mode is not the only way to avoid mismatch in remote object design. Please read the supplementary article "Alternative Method for Business Interface Mode" to learn other solutions. Business Interface Mode First, you need to write an interface that defines all business methods. This is very like a remote interface, but does not have EJB semantics. Listing 1 shows a simple business interface for the library object.

Listing 1. Library Service Interface package com.ibm.library; import com.ibm.library.exceptions.NoSuchBookException; import java.util.List; public interface ILibrary {public List getBooks (); public List getBooks (String category); public Book getBook (String ISBN) Throws NosuchbookException; Public Boolean Checkout (Book Book); Public Boolean Checkout (List Book); // and so-only ...} is very clear, this example is simple, but you can experience the idea. This business interface explicitly defines the business usage of the library object. In order to make the interface to the EJB specification, you only need to make an EJB-specific concession: Must ensure that each method can throw java.rmi.RemoteException. Listing 2 shows a complete service interface. Listing 2. Complete Library Service Interface package com.ibm.library; import com.ibm.library.exceptions.NoSuchBookException; import java.rmi.RemoteException; import java.util.List; public interface ILibrary {public List getBooks () throws RemoteException ; public List getBooks (String category) throws RemoteException; public Book getBook (String isbn) throws NoSuchBookException, RemoteException; public boolean checkout (Book book) throws RemoteException; public boolean checkout (List books) throws RemoteException; // And so on .. There is a suitable business interface compatible with the EJB. The next step is to extend it in the remote interface. Because all business methods are defined in the Library interface, you don't have to add any new methods to the remote interface. Listing 3 Display: What will look like if you use a business interface mode to build a remote interface. Listing 3. Library remote interface package com.ibm.library; import javax.ejb.ebobject; public interface library extension ilibrary, ejbobject {// all business methods defined in the ilibrary interface} This is. It seems simpler, isn't it? The next step is to encode the local interface, this section is left to you as an exercise (just follow the example using the remote interface). As for the Home interface (and the local Home interface, if needed), you can encode them in accordance with the general situation; do not make any changes to them. However, some are slightly different in the implementation class.

转载请注明原文地址:https://www.9cbs.com/read-77166.html

New Post(0)