EJB Best Practices: Dynamic Delegation

xiaoxiao2021-03-06  61

Original source:

http://www-900.ibm.com/developerWorks/cn/java/J-EJB1119/index.shtml

EJB Best Practices: Dynamic Delegation English Original

content:

Business delegation: Reproduction of review methods Reference information about the author's evaluation

related information:

EJB BEST PRACTICES series BEST PRACTICES IN EJB Exception Handling Subscribe to DeveloperWorks News

Also in the Java area:

Tutorial tools and product codes and components articles

Build more general business delegations with Java reflection

Level: Intermediate Brett Mclaughlin (Brett@oreilly.com) Writers and O'Reilly and Associate 2003 March

Although the business delegation is really brings exciting new flexibility to your business Java design, it is too much trouble to encode a business delegation in your application. in

In this article, Brett Mclaughlin presents a more common version of how to create a business delegate class: Dynamic delegation.

In the previous skill article, we discussed how to use the business delegate class (please do not confuse the business interface mode) to access your EJB component. By inserting a business delegate class between client code and EJB code, we can isolate the application's Web layer with EJB semantics and business logic. One way to study this type of design is to see how common. First start with an application, the business logic and technical functions in the application are closely intertwined, and we have gradually separated the different layers of the application, and different techniques are used to reduce their interdependence. When doing this, you should find that the more common fabrication of the application underlying structure, then the more robust and maintainability over time. In this skill article, we will continue to use the ideas of common design. We will start from the restrictions on the current business delegation implementation, and then I will show you how to overcome these restrictions by creating more common (so non-tall) business delegations. Business delegation: Review reviews the business delegation of the library bean interface we discussed last month. Most of the code of the LibraryDelegate class is just a way to copy the original library bean. LibraryDelegate adds the init (), destroy (), and constructor method, and then uses these methods to delegate the task to library beans. In doing so, delegate the buffer between the Web layer and the enterprise bean. Here is the business interface of the original bean. The process of breeding is 10, 20 or more methods, in consideration of multiple session beans, otherwise the problem is not obvious. In fact, it is not rare to find a session bean with 50 or more methods! Because the service interface of the bean must include all methods of this bean, the business delegation class will also do this. That will make the code too large and it is easy to go wrong.

Is your input too fast? When using EJB components, we often copy a number of methods across remote interfaces, business interfaces, implementation classes, and current business delegations. Many of us like to cut and paste them between editor windows and IDEs, rather than manually enter them, but please note: Press Option V or Control V to be as easy to erroneously entered as manual input methods - you added The more methods, the greater the possibility of error. By carefully verify that you correctly enter the method correctly, and whether it is cut and pasted, you can avoid much trouble. In addition to the huge code, we must also consider the factors. Because the DELEGATE class must copy all the methods of the bean, Bean inevitably change, you will find a lot of time to add a new method to Delegate, let alone recompile, it is possible Test the new code. As far as it is itself, this seems not very serious problem. However, if we start using the business delegation class to abstract business and representations from the technical infrastructure (in this article refer to EJB components). If you need to change your business delegation, then, in fact, our business delegate is still associated with the underlying component. We need a better way, you said yes. There is really a better way. The dynamic delegation solution is to use dynamic delegation, and it uses Java Reflection (REFLECTION). You can make the delegate dynamically call the method of the remote interface of the target EJB component (through the Java Reflection API) without having to hardly encode each business method into the delegation. This allows thoroughly eliminating coupling from a remote interface because it does not need to be changed in the business delegation when adding a method for the service or remote interface of the service or remote interface. Using the dynamic delegation also makes it easier to change your technical infrastructure. Migrate from remote interfaces and EJB technology to another (such as Java Data Objects, JDO) only needs to change the delegated init () method. All other methods call will continue to delegate through the BEAN and can continue to be used without further changes. Listing 1 shows the dynamic version of the Library business delegation: List 1. Library bean's business delegate package com.ibm.library;

Import java.lang.reflect.method;

Import java.lang.reflect.InvocationTargeTexception;

Import java.rmi.remoteexception;

Import java.util.hashmap;

Import java.util.map;

Import javax.ejb.createException;

Import javax.naming.namingexception;

Public class librarydelegate imports ilibrary {

Private iLibrary Library;

Private map availablemethods;

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 (); // get the methods available for use in proxying

AvailableMethods = new hashmap ();

Method [] methods = ilibrary.class.getMethods ();

For (int i = 0; i

AvailableMethods.Put (Methods [i] .Getname (),

Methods [i]);

}

} catch (namingexception e) {

Throw new runtimeException (e);

} catch (createException e) {

Throw new runtimeException (e);

} catch (remoteException E) {

Throw new runtimeException (e);

}

}

// all the hard-codemed Methods Are Removed

Public Object

INVOKE (Object Proxy, Method Method, Object [] ARGS)

Throws throwable {

Try {

// see if this is init () or destroy ()

IF (Method.getName (). Equals ("init")) {

INIT ();

Return NULL;

Else IF (Method.getName (). Equals ("destroy")) {

DESTROY ();

Return NULL;

} else {

Method Method =

AvailableMethods.get (Method.getName ());

// see if we found anything

IF (Method! = NULL) {

Return Method.Invoke (library, args);

} else {

Throw new

Nosuchmethodexception ("THE LIBRARY DOES NOT"

"Support the" method.getname () "Method.");

}

}

} catch (invocationTargeTexception E) {

// We don't support throwing runtimeexceptions from ejbs

// Directly

IF (E.GETTARGETEXCEPTION () InstanceOf RemoteException) {

Throw new runtimeException (e);

} else {

throw E.GETTARGETEXCEPTION ();

}

}

}

Public void destroy () {

// in this case, do nothing

}

}

The dynamic commissioned uniquely solved the coupling problem between delegation, bean and its business interfaces. However, it is not a perfect solution and will not always be the best solution. Although you have achieved great flexibility from this method, it also pays a performance price. Java reflection is not very fast, so some delays will be felt between invoking invoke () and get results. The static business delegation demonstrated in the previous skill article is a faster solution, but it makes your business layer and the technical layer have a high degree of coupling higher than what you want. Therefore, when weighing these two options, choose which one to be based on design or performance. Dynamic delegation is a better choice when the design is more important than the overall performance. When performance is a more important factor, the business delegation is a better choice. You may find that you are using dynamic delegate for intranet applications, in the internal network, all machines are on the local network, and you will add or change your functionally. For e-commerce and customer-oriented applications, the original business delegation may be a better choice. In both cases, you should now have a better understanding of the business delegation and its working principle. It's fun, have fun, let's see you online! References View the full EJB Best Practices Series written by Brett McLaughlin. See Sun's Java Reflection Document for more information on reflection. Java Architecture Designer Srikanth Shenoy demonstrated how to solve problems faster, based on EJB-based systems in its article "Best Practices in EJB Exception Handling" (DeveloperWorks, May 2002). Sun's EJB Technology Homepage is a good starting point for EJB technical information. Another great reference Source ThereVerside.com provides articles and information about J2EE. See the DeveloperWorks Java Technical Tutorial page for a list of the developerWorks free tutorials for EJB 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). Contact Brett with Bret @oreilly.com with Brett.

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

New Post(0)