The agent mode of Java Program performance optimization

Source: Internet
Author: User
Tags instance method

Proxy mode is a lot of useful, some for system security, some for remote invocation, here we, mainly discussed under due to Program performance optimization delay loading.

First, let's look at the proxy mode design

First, let's start with a brief explanation of what the proxy mode is.

The proxy design pattern has an interface, in addition to the real topic class and the proxy class, the real class and the proxy class all implement the interface, the proxy class and the real topic class is the association and the aggregation relation. The client is associated with an interface.

Proxy is divided into static agent and dynamic proxy agent is to create a proxy for the real topic manually, while the dynamic agent is the JVM at run time using bytecode loading technology to automatically create a proxy, do not care about interfaces and really subject class

How to achieve the specific

Oh, yes, I almost forgot. The agent model is exactly how to optimize the program, we specifically look at the next.

The client test code is as follows:

public class Testdynamicproxy {public static void main (string[] args) {Idbquery iy = Jdkdbqueryhandler.createjdkproxy (); System.out.println (Iy.request ());}}

 

Ps: client-to-interface Association

The proxy implementation and logic processing classes are as follows:

Import java.lang.reflect.*;p Ublic class Jdkdbqueryhandler implements Invocationhandler{idbquery real = Null;public Object Invoke (Object object, method method, object[] args) {if (real==null) {real = new dbquery ();} return Real.request ();} public static Idbquery Createjdkproxy () {idbquery jdkproxy = (idbquery) proxy.newproxyinstance ( Classloader.getsystemclassloader (), New Class[]{idbquery.class},new Jdkdbqueryhandler ()); return jdkProxy;}}

  

Ps: proxy implementation and logic processing classes are key codes for implementing lazy loading

When the client starts loading, it loads into the code implementation and logic processing classes and creates the proxy instance, but does not initialize the real topic class. The Invoke () method of the proxy implementation and the logical processing class is executed only when the iy.request () method is called and the real topic class is loaded and initialized. This enables lazy loading, reduces system initialization time, and improves the user experience. To a certain extent can also save memory space, to avoid waste of memory space, (because when used to record initialization, do not have to open up memory space that is not a waste of it)

The code of the interface class and the real topic class is attached here, which makes it easy for us to test

Interface class

Public interface  Idbquery {       String request ();}

Real Theme Class

public class  Dbquery implements Idbquery {public dbquery () {try{thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();}} Public String request () {return "request Coming";}}

These are the dynamic proxy implementations of the JDK. There are also more popular CGLIB Dynamic agents , javaassist dynamic agents

Here I first introduce CGLIB dynamic Agent. CGLIB dynamic agents and JDK dynamic proxies are very similar, let's look at the specific code

The client code is as follows:

public class Testdynamicproxy {public static void main (string[] args) {Idbquery iy = CGLIBDBQUERYINTERCEPTOR.CREATECGLIBP Roxy (); System.out.println (Iy.request ());}}

Not too much to explain here, after reading the above example, I believe we all understand

The proxy implementation and logic processing classes are as follows

Import Java.lang.reflect.Method;  Import Net.sf.cglib.proxy.Enhancer;  Import Net.sf.cglib.proxy.MethodInterceptor;  Import Net.sf.cglib.proxy.MethodProxy;  public class Cglibdbqueryinterceptor implements Methodinterceptor{idbquery real = null;public Object Intercept (object Args0,method arg1,object[] Args2,methodproxy args3) throws Throwable {if (real==null) {real = new dbquery ();} return Real.request ();} public static Idbquery Createcglibproxy () {Enhancer eh = new enhancer (); Eh.setcallback (new Cglibdbqueryinterceptor ()); /Specify the switch, define the proxy class logic eh.setinterfaces (New Class[]{idbquery.class});//Specify interface Idbquery proxy = (idbquery) eh.create ();// Create proxy instance return proxy;}}

  

dynamic Proxy and CGLIB Implementation of JDK the dynamic proxy basically specifies the proxy class logic and the proxy interface which is common

Real topic classes and interfaces as in the previous example, no longer posted

Javaassist 's dynamic agent has two implementations, one using a proxy factory and one using dynamic java code to generate bytecode

This is where the code is directly posted:

Picture upload failed ~ ~ ~

Ps: The above instance method handles the class in the logical processing class , the Real topic class and interface are the same as the above example

The proxy factory specifies the interface after which the proxy class object is generated, and the proxy class object specifies the processing logic.

Roughly all three are similar,javaassist dynamic java code generation bytecode is not introduced, the catch is more troublesome

Other ASM Dynamic agents are more complex to implement and do not introduce

  

  

 

 

The agent mode of Java Program performance optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.