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