Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 148 149 150 151 152 153 < 154 > 155 156 157 158 159 160 .. 166 >> Next

The worst performance for call setup revolved around the clients where calls were determined at runtime, or where stubs were generated at runtime. The DII with call lookups example in Listing 48.6 and the dynamic proxy example in Listing
48.4 were approximately 2.5 times slower in performance for pre-invocation setup. Both of those clients downloaded the WSDL. The dynamic proxy example was slightly slower because of the generation of the stubs on the fly, but they were quite similar in speed.
428 Item 48
Table 48.1 Table of Results: Pre-invocation Times
LISTING 48.3 (PRECOMPILED STUBS) LISTING 48.4 (DYNAMIC PROXY GENERATION) LISTING 48.5 (DII HARDCODED) LISTING 48.6 (DII - WITH CALL LOOKUPS)
Run 1 590 ms 1683 ms 591 ms 1642 ms
Run 2 601 ms 1683 ms 581 ms 1653 ms
Run 3 591 ms 1672 ms 591 ms 1643 ms
Run 4 591 ms 1672 ms 581 ms 1633 ms
Run 5 591 ms 1783 ms 581 ms 1643 ms
Run 6 601 ms 1663 ms 591 ms 1653 ms
Run 7 591 ms 1692 ms 601 ms 1642 ms
Run 8 591 ms 1713 ms 601 ms 1663 ms
Run 9 591 ms 1672 ms 590 ms 1662 ms
Run 10 591 ms 1682 ms 641 ms 1662 ms
Run 11 641 ms 1662 ms 581 ms 1632 ms
Run 12 591 ms 1683 ms 581 ms 1642 ms
Run 13 591 ms 1722 ms 591 ms 1642 ms
Run 14 581 ms 1692 ms 631 ms 1642 ms
Run 15 591 ms 1682 ms 591 ms 1692 ms
Average Time 594.9333333 ms 1690.4 ms 594.9333333 ms 1649.733333 ms
Conclusion
Our simple example has shown that there is obviously a trade-off between the dynamic features of JAX-RPC and performance. Imagine if we needed to write a client application for working with multiple Web services, or a complex Web service with pages and pages of WSDL. The performance implications of doing everything dynamically—just because those techniques are available—would be awful. Just because you can do things dynamically doesn't necessarily mean you should do things dynamically with JAX-RPC. If there is a reason, do it. When in doubt, use precompiled stubs—your code will look prettier—and if your Web service's interface changes, you will simply need to recompile your client.
Get Your Beans Off My Filesystem! 429
Item 49: Get Your Beans Off My Filesystem!
It is easy to overlook best practices for J2EE because some habits of using the Java 2 Standard Edition die hard. When designing Enterprise JavaBeans solutions, it is sometimes easy to forget what type of programming should not be done. We know, for example, that spawning threads within beans is a big no-no, because we understand that the EJB container handles all threading issues. Oftentimes, however, we overlook the pitfalls that await us when we use the java.io classes to access files in the filesystem.
To demonstrate this issue, we provide a very simple example of a session bean reading properties from the filesystem. In our example, a session bean is used to calculate the amount of tax that shoppers must pay when they proceed to check out. In doing so, the two-character abbreviation for the shoppers' state is passed to the bean, so the bean can calculate the approximate sales tax. Because sales tax varies from state to state, the bean reads from a local properties file with different sales tax percentages:
Salestax.AB=.04
Salestax.VA=.045
For brevity (and to do a simple demonstration of this pitfall), we are only listing two states. In our example, we assume that if a state is not in this properties file, the state has no sales tax. Listing 49.1 shows the code for our BadTaxCalculatorBean that reads the properties file. A client calling this session bean passes the purchase amount (double cost) and the two-letter state abbreviation (String state), and the tax on the purchase amount is returned. We load the properties file in our session bean's calculateTax() method, and we calculate the sales tax and return it as a double. In lines 18 to 31, we load the properties file using the java.io.FilelnputStream class and calculate the tax:
001 package 9 4 m e t i s l l a LM t i p a v a j g. r o
002
003 import java.rmi.RemoteException;
004 import javax.ejb.SessionBean;
005 import javax.ejb.SessionContext;
006 import java.util.Properties;
007 import java.io.*;
008 import javax.ejb.EJBException;
009
010
011 public class BadTaxCalculatorBean implements SessionBean
012 {
013 public double calculateTax(double cost, String state)
014 {
015 Properties p = new Properties();
016 double tax = 0;
Listing 49.1 Bean reading properties file for values (continued)
430 Item 49
017
018 try
019 {
020 p.load(new FileInputStream("C://salestax.properties"));
021: tax = Double.parseDouble(p.getProperty("Salestax." + state)) *
022 cost;
023
024 }
025 catch ( IOException e )
026 {
027 e.printStackTrace();
028
029 throw new EJBException("Can't open the properties file! "
030 + e.toString());
031: }
032
033 String taxString = p.getProperty("Salestax.” + state);
034
035 if ( taxString != null && !taxString.equals("”) )
036 {
037 tax = Double.parseDouble(taxString) * cost;
038 }
039
040 return (tax);
Previous << 1 .. 148 149 150 151 152 153 < 154 > 155 156 157 158 159 160 .. 166 >> Next