[Weave message Frame] [Java Core Technology] Dynamic agent application 3

Source: Internet
Author: User
Tags session id

We first use lazy processing to implement the extraction of meta information on the interface class:

 Public Abstract class qrpcfactory {     publicstatic <T> T loadproxy (class<t> target, Qnode ... nodes)     publicstaticlong... ids)  }

Generate proxy instances by factory class binding session ID or node

Rpccontext is a temporary context object that is used to record the sending target and to finish processing the returned results

 Public classRpccontext {Privateqnode[] nodes; Privatelong[] IDs; PrivateIqcallback<?>CB;  Public StaticRpccontext of (Qnode ... nodes) {rpccontext ret=NewRpccontext (); Ret.nodes=nodes; returnret; }     Public StaticRpccontext of (Long ... IDs) {rpccontext ret=NewRpccontext (); Ret.ids=IDs; returnret; }}

@SuppressWarnings ("Unchecked") Public Abstract classQrpcfactory { Public Static<T> T Loadproxy (class<t>target, Qnode ... nodes) {T ret=loadProxy0 (target); Irpccontext CTX=(irpccontext) ret;    Ctx.setcontext (Rpccontext.of (nodes)); returnret; }     Public Static<T> T Loadproxy (class<t>target, Long ... IDs) {T ret=loadProxy0 (target); Irpccontext CTX=(irpccontext) ret;    Ctx.setcontext (Rpccontext.of (IDs)); returnret; }    Private Static<T> T loadProxy0 (class<t>target) {T ret= (T) proxy.newproxyinstance (qrpcfactory.class. getClassLoader (),NewClass[] {target, irpccontext.class},NewInvocationhandler () {Qmodel Modelanno= Target.getannotation (Qmodel.class); Private  ShortModel =Modelanno.value (); PrivateRpccontext CTX; @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {if(Method.getname (). Equals ("SetContext")) {             This. CTX = (rpccontext) args[0]; System.out.println ("SetContext"); return NULL; } Qcommond Commondanno= Method.getannotation (Qcommond.class); //Do send        return NULL;    }    }); returnret; }     Public Static voidMain (string[] args) {Testrpcobject obj= Qrpcfactory.loadproxy (testrpcobject.class, 123L); }}
@QModel (1)publicinterface  testrpcobject {    @QCommond (1)     publicvoid  A ();}


It is the core of lazy processing thought, and some scenes plus cache, double check, ThreadLocal, Weakhashmap technology

Because to solve concurrency problem, so this example cache is not used, the object cannot be a singleton, and each instance parameter is different, can not use threadlocal to record Rpccontext

Invocationhandler can be generated directly when LOADPROXY0 is created rpccontext here for example an interface class how to save the properties of the

[Weave message Frame] [Java Core Technology] Dynamic agent application 3

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.