Java performance optimization skills highlights (2)

xiaoxiao2021-03-06  38

The improved performance techniques described above are suitable for most Java applications, and the problem to be discussed is suitable for applications of JSP, EJB or JDBC.

1. Use buffer marks

Some application servers have joined JSP buffer tags. For example, BEA's WebLogic Server starts supporting this feature from version 6.0, and the Open Symphony project also supports this feature. The JSP buffer tag can buffer the page segment and can buffer the entire page. When the JSP page is executed, if the target segment is already in the buffer, the code generated by generating the piece is not executed. Page-level buffer capture requests for the specified URL and buffer the entire result page. This feature is extremely useful for the homepage of the shopping basket, catalog, and portal. For such applications, the page-level buffer can save the result of the page execution, and the subsequent request is used.

For code logic complex pages, the effect of improving performance using buffer marks is more obvious; contrary, the effect may be slightly coupled.

2. Always access entity beans through session beans

Direct access entity beans are not conducive to performance. Each GET method is a remote call when the client remotely accesses entity beans. Session beans accessed to access entity beans are local, and all data can be organized into a structure and then returns its value.

Access to entity beans can improve transaction management because session beans will only be submitted when the transaction boundary is reached. Each direct call to the GET method generates a transaction, and the container will perform a "load-read" operation after each entity bean's transaction. Time, using entity beans will lead to poor performance. If the unique use of entity beans is to extract and update data, it is changed to use the JDBC access database within session beans to get better performance.

3. Select the appropriate reference mechanism

In a typical JSP application, the header, the footer section is often extracted, then introduced to the page, footage as needed. Currently, there are two ways to introduce external resources in the JSP page. There are two: include instructions, and the include action.

Include directive: for example

<% @ include file = "Copyright.html"%>

This instruction introduces the specified resource when compiling. Before compiling, the pages with the include instructions and the specified resources are merged into one file. The referenced external resource is determined when compiling is more efficient than running.

Include Action: For example

This action introduces the result generated after the specified page execution. Since it is completed at runtime, the control of the output results is more flexible. However, when the referenced page cannot be determined when the referenced page is not determined when the referenced content is frequently changed, or when the request is not appeared on the main page, the use of the include action is used.

4. Set read-only properties in the deployment descriptor

The deployment of the entity bean allows all GET methods to be "read only". When a transaction unit is only included with a method of performing a read operation, setting the read-only property is conducive to improve performance because the container does not have to perform a storage operation.

(Author: Java Research)

5. Buffer access to EJB HOME

The EJB HOME interface is obtained through the JNDI name. This operation requires considerable overhead. JNDI looks up in the init () method of servlet. If you frequent EJB access frequently, you will be best to create an EJBHomeCacche class. The EJBHomeCache class should generally be implemented as a Singleton.

6. Implement local interface for EJB

The local interface is the new content of EJB 2.0 specification, which allows Bean to avoid overhead of remote calls. Consider the following code.

PayBeanhome Home = (paybeanhome) javax.rmi.portableremoteObject.narrow

(CTX.lookup ("paybeanhome"), paybeanhome.class;

PayBean bean = (paybean)

Javax.rmi.PortableRemoteObject.narrow

Home.create (), PayBean.class;

The first statement means that we have to look for the Home interface. This lookup is made through JNDI, it is an RMI call. Then, we locate the remote object, return to the proxy reference, which is also a RMI call. The second statement demonstrates how to create an instance, involving the Stub program that creates a IIOP request and transmits the request on the network, which is also a RMI call. To achieve a local interface, we must make the following modifications:

The method can no longer throw a java.rmi.RemoteException, including exceptions derived from RemoteException, such as TransactionRequiredException, TransactionRolledBackexception, and NosuChobjectException. EJB provides equivalent local exception, such as TransactionRequiredLocalexception, TransactionRolledBackLocalexception, and NosuchobjectLocalexception.

All data and return values ​​are passed in a reference, not the transmission value. The local interface must be used on the machine deployed by the EJB. In short, the client and components of the service must run on the same JVM. If the bean implements a local interface, its reference is not serialized.

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

New Post(0)