1717public class Rpcfx {
1818 public static <T > T create (final Class <T > serviceClass , final String url ) {
1919
20+ // 0. 替换动态代理 -> AOP
2021 return (T ) Proxy .newProxyInstance (Rpcfx .class .getClassLoader (), new Class []{serviceClass }, new RpcfxInvocationHandler (serviceClass , url ));
2122
2223 }
@@ -25,14 +26,17 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
2526
2627 public static final MediaType JSONTYPE = MediaType .get ("application/json; charset=utf-8" );
2728
28-
2929 private final Class <?> serviceClass ;
3030 private final String url ;
3131 public <T > RpcfxInvocationHandler (Class <T > serviceClass , String url ) {
3232 this .serviceClass = serviceClass ;
3333 this .url = url ;
3434 }
3535
36+ // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
37+ // int byte char float double long bool
38+ // [], data class
39+
3640 @ Override
3741 public Object invoke (Object proxy , Method method , Object [] params ) throws Throwable {
3842 RpcfxRequest request = new RpcfxRequest ();
@@ -41,17 +45,26 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
4145 request .setParams (params );
4246
4347 RpcfxResponse response = post (request , url );
48+
49+ // 这里判断response.status,处理异常
50+ // 考虑封装一个全局的RpcfxException
51+
4452 return JSON .parse (response .getResult ().toString ());
4553 }
4654
4755 private RpcfxResponse post (RpcfxRequest req , String url ) throws IOException {
4856 String reqJson = JSON .toJSONString (req );
57+ System .out .println ("req json: " +reqJson );
58+
59+ // 1.可以复用client
60+ // 2.尝试使用httpclient或者netty client
4961 OkHttpClient client = new OkHttpClient ();
5062 final Request request = new Request .Builder ()
5163 .url (url )
5264 .post (RequestBody .create (JSONTYPE , reqJson ))
5365 .build ();
5466 String respJson = client .newCall (request ).execute ().body ().string ();
67+ System .out .println ("resp json: " +respJson );
5568 return JSON .parseObject (respJson , RpcfxResponse .class );
5669 }
5770 }
0 commit comments