@Test public void rpc() throws Exception { Server server = null; try { server = new RPC.RPCServer(); server.register(IRPCService.class, RPCServiceImpl.class); server.start(); int count = 0, max = 1000; while (!server.isRunning() && count < max) { Thread.sleep(5); count++; } IRPCService testServiceImpl = RPC.getProxy(IRPCService.class, "localhost", server.getPort()); Thread.sleep(50); assertEquals("Hello, Benedict!", testServiceImpl.printHello("Benedict")); } finally { if (server != null) server.stop(); } }
@SuppressWarnings("unchecked") public static <T> T getProxy(final Class<T> clazz, String host, int port) { final Client client = new Client(host, port); InvocationHandler handler = (Object proxy, Method method, Object[] args) -> { Call call = new Call(); call.setInterfaces(clazz); call.setMethodName(method.getName()); call.setParams(args); call.setParameterTypes(method.getParameterTypes()); client.invokeCall(call); return call.getResult(); }; return (T) Proxy.newProxyInstance(RPC.class.getClassLoader(), new Class[]{clazz}, handler); }
RPC { @SuppressWarnings("unchecked") public static <T> T getProxy(final Class<T> clazz, String host, int port) { final Client client = new Client(host, port); InvocationHandler handler = (Object proxy, Method method, Object[] args) -> { Call call = new Call(); call.setInterfaces(clazz); call.setMethodName(method.getName()); call.setParams(args); call.setParameterTypes(method.getParameterTypes()); client.invokeCall(call); return call.getResult(); }; return (T) Proxy.newProxyInstance(RPC.class.getClassLoader(), new Class[]{clazz}, handler); } }
RPC { @SuppressWarnings("unchecked") public static <T> T getProxy(final Class<T> clazz, String host, int port) { final Client client = new Client(host, port); InvocationHandler handler = (Object proxy, Method method, Object[] args) -> { Call call = new Call(); call.setInterfaces(clazz); call.setMethodName(method.getName()); call.setParams(args); call.setParameterTypes(method.getParameterTypes()); client.invokeCall(call); return call.getResult(); }; return (T) Proxy.newProxyInstance(RPC.class.getClassLoader(), new Class[]{clazz}, handler); }  }
RPC { @SuppressWarnings("unchecked") public static <T> T getProxy(final Class<T> clazz, String host, int port) { final Client client = new Client(host, port); InvocationHandler handler = (Object proxy, Method method, Object[] args) -> { Call call = new Call(); call.setInterfaces(clazz); call.setMethodName(method.getName()); call.setParams(args); call.setParameterTypes(method.getParameterTypes()); client.invokeCall(call); return call.getResult(); }; return (T) Proxy.newProxyInstance(RPC.class.getClassLoader(), new Class[]{clazz}, handler); }  @SuppressWarnings("unchecked") static T getProxy(final Class<T> clazz, String host, int port); }
RPC { @SuppressWarnings("unchecked") public static <T> T getProxy(final Class<T> clazz, String host, int port) { final Client client = new Client(host, port); InvocationHandler handler = (Object proxy, Method method, Object[] args) -> { Call call = new Call(); call.setInterfaces(clazz); call.setMethodName(method.getName()); call.setParams(args); call.setParameterTypes(method.getParameterTypes()); client.invokeCall(call); return call.getResult(); }; return (T) Proxy.newProxyInstance(RPC.class.getClassLoader(), new Class[]{clazz}, handler); }  @SuppressWarnings("unchecked") static T getProxy(final Class<T> clazz, String host, int port);  }