EJB Best Practices: Business Delegation Model (Reprinted from IBM China Developer Website)

xiaoxiao2021-03-06  65

Original source:

http://www-900.ibm.com/developerWorks/cn/java/J-EJB1022/INDEX.SHTML

EJB Best Practices: Business Delegation

English original

content:

Business Interface Mode Business Delegation Mode Reference About the author to the evaluation of this article

related information:

EJB BEST PRACTICES series

There is also a Java area:

Teaching tools and product code and components all articles practical skills

Abstract business, representation and application logic in EJB design

Level: Intermediate Brett Mclaughlin (Bret @oreilly.com) Writer and editor, O'Reilly and Associate 2003 February

One of the most complicated issues in application planning is the necessary separation between the business layer and the implementation layer. To achieve this separation, Brett McLaughlin is based on the Business Interface mode, with a class to handle the logical Web layer abstraction. Business Delegate mode can help you avoid the coupling of your application difficult to maintain and upgrade.

If you have been studying this series from the beginning, you know that one of the problems we have always cared is to separate the application and represent logic with business logic. For example, in the first technique article, we focus on discussing the problem of remote object design, especially the challenges that appear when the implementation of the implementation is separated from its interface. We use the business interface mode to solve this problem. In this article we will use this mode again. The last technique has solved the conflict between the two requirements that Beans directly exposed to the application layer while supporting the data contained in the entity bean. In these two examples, the core problem is solved by cauting abstract business logic implementation from the code using business logic. As this skill article will be demonstrated, the abstraction is one of the foundations of good application design, especially for EJB-based applications. Here, we will use business interface patterns and session beans to keep your web layer and Enterprise JavaBeans components loosely coupled. Business Interface Mode Our previous business interface mode implementation allows us to provide a business-specific interface for interacting with session. Listing 1 shows the interface, in this skill article we will use this interface again: Listing 1. Library business 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 ...

}

Note: The Library interface does not contain any EJB semantics. Instead, all implementations and EJB details are processed within the session bean that implements the interface. Now let's take a look at the web layer of the enterprise application to access the library interface and support its session bean: Listing 2. Library, LibraryHome LibraryHome =

(Libraryhome) ejbhomefactory.getInstance ().

Lookup ("Java: Comp / Env / Ejb / LibraryHome",

LibraryHome.Class;

ILibrary Library = libraryhome.create ();

Listing 2 uses another class. If you learn from this series, you should be familiar with this class. By querying the Home interface of the library bean, the EJBHomeFactory class further separates the business and application logic. Then get the implementation of the library interface from the HOME interface. The above code is not bad - compared to the initial context loaded into JNDI, manually obtain the Home interface, then directly process the entity bean, it does have improved. But it does include a basic disadvantage: the web layer is still combined with the EJB component, especially with the library session bean (not just business interface). What is your application code when you need to have a new version of Java Data Object, or a new version of Java Data Object, or a new version of the EJB specification? What will I do when you feel more like a clear-separated system (this is inevitable)? Although it is easier to write a closely coupled code in the short term (ie, the semantics introduced into the business layer of the business layer), this is almost inevitably shortened to shorten the service life of the application. This creates a business delegation mode. Get "Intermediary": The business delegation mode business delegation mode is completely different from the business interface mode. Although business interface modes define the business logic that can be used, the business delegation provides access to that logic without generating the relevance to its implementation technology (in this example is EJB component). Clearly, the business delegation is a helper class, which handles the details of the EJB container to get any desired resources and the details of those resources. Listing 3 uses the library interface in Listing 1 as its start point, but please note the introduced new librarydelegate class and method. Listing 3. Business delegation for library beans

Package com.ibm.library;

Import java.rmi.remoteexception;

Import javax.ejb.createException;

Import javax.naming.namingexception;

Public class librarydelegate imports ilibrary {

Private iLibrary Library;

Public librarydelegate () {

INIT ();

}

Public void init () {

// Look Up and Obtain Our session bean

Try {

LibraryHome LibraryHome =

(Libraryhome) ejbhomefactory.getInstance (). Lookup

"Java: comp / env / ejb / libraryhome", libraryhome.class;

Library = libraryHome.create ();} catch (namingexception e) {

Throw new runtimeException (e);

} catch (createException e) {

Throw new runtimeException (e);

} catch (remoteException E) {

Throw new runtimeException (e);

}

}

Public List getBooks () throws ApplicationException {

Try {

Return Library.getBooks ();

} catch (remoteException E) {

Throw New ApplicationException (E);

}

}

Public List getBooks (String category) throws applicationException {

Try {

Return Library.getBooks (Category);

} catch (remoteException E) {

Throw New ApplicationException (E);

}

}

Public Book getBook (String ISBN)

Throws nosuchbookException, ApplicationException {

Try {

Return Library.getBook (ISBN);

} catch (remoteException E) {

Throw New ApplicationException (E);

}

}

Public Boolean Checkout (book book) throws ApplicationException {

Try {

Return Library.checkout (BOOK);

} catch (remoteException E) {

Throw New ApplicationException (E);

}

}

Public Boolean Checkout (list books) throws ApplicationException {

Try {

Return Library.checkout (Books);

} catch (remoteException E) {

Throw New ApplicationException (E);

}

}

// and so on ...

Public void destroy () {

// in this case, do nothing

}

}

In the LibraryDelegate class, each method can respond to requests by passing the operation request to a session bean. Init () method handles the collection of initialization and resources; the destroy () method handles resource release. Although it is very concise, LibraryDelegate still produces considerable impact on the entire application. Note the difference between the list 2 and the code segments below:

ILibrary Library = New LibraryDelegate ();

You have removed all technical dependence on the application web layer by introducing librarydelegate. This creates not only a clearer, better application code, but also positions the application to make changes in its life during its service life. For example, if you need to transfer from using EJB technology to use the pure JDBC class, you can use the new JDBC class to override your business delegation. Your web layer will not know that changes have changed, nor does it need to recompile. In the next series of articles, we will detail the use of the business delegation mode, which in turn makes it more common. The resulting result will be better code and less dependence with EJB technology. In the article, the Java architect designer Srikanth Shenoy's "Best Practices in EJB EXCEPTION HANDLING" (DEVELOPERWORKS, 2002), he demonstrated how to encode to solve problems faster on systems based on EJB technology. The EJB technology homepage of Sun Microsystems is a good starting point for EJB technology information. Another excellent reference is THSERVERSIDE.COM, which provides articles and information about J2EE. See the developerWorks Java Access Tutorial page for a list of free tutorials related to EJB technology and J2EE. On the DeveloperWorks Java technology area, you can find up to hundreds of Java Technical References.

About the author BRETT MCLAGHLIN from the LOGO era (Remember that small triangle?) Started working on computer. He is now specializing in building an application infrastructure with Java and Java related technologies. In the past few years he has been committed to achieving these infrastructure in NEXTEL COMMUNICATIONS and Allegiance Telecom, Inc. Brett is one of the common founders of the Java Apache project Turbine, which develops a reusable component architecture with Java Servlet for web applications. He is also one of the volunteer developers of the EJBoss project (an open source EJB application server) and COCOON (an open source XML web publishing engine). You can contact Brett with Brett@oreilly.com.

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

New Post(0)