HttpProtocol extends AbstractProxyProtocol { @Override public void destroy() { super.destroy(); for (String key : new ArrayList<>(serverMap.keySet())) { ProtocolServer server = serverMap.remove(key); if (server != null) { try { if (logger.isInfoEnabled()) { logger.info("Close jsonrpc server " + server.getUrl()); } server.close(); } catch (Throwable t) { logger.warn(t.getMessage(), t); } } } } HttpProtocol(); void setHttpBinder(HttpBinder httpBinder); @Override int getDefaultPort(); @Override void destroy(); static final String ACCESS_CONTROL_ALLOW_ORIGIN_HEADER; static final String ACCESS_CONTROL_ALLOW_METHODS_HEADER; static final String ACCESS_CONTROL_ALLOW_HEADERS_HEADER; }
@Test public void testJsonrpcProtocol() { HttpServiceImpl server = new HttpServiceImpl(); assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); int port = NetUtils.getAvailablePort(); URL url = URL.valueOf("http: Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url)); Invoker<HttpService> invoker = protocol.refer(HttpService.class, url); HttpService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); assertTrue(server.isCalled()); assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); }
@Test public void testJsonrpcProtocolForServerJetty() { HttpServiceImpl server = new HttpServiceImpl(); assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); int port = NetUtils.getAvailablePort(); URL url = URL.valueOf("http: Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url)); Invoker<HttpService> invoker = protocol.refer(HttpService.class, url); HttpService client = proxyFactory.getProxy(invoker); String result = client.sayHello("haha"); assertTrue(server.isCalled()); assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); }
@Test public void testGenericInvoke() { HttpServiceImpl server = new HttpServiceImpl(); Assertions.assertFalse(server.isCalled()); ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); int port = NetUtils.getAvailablePort(); URL url = URL.valueOf("http: Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url)); Invoker<GenericService> invoker = protocol.refer(GenericService.class, url); GenericService client = proxyFactory.getProxy(invoker, true); String result = (String) client.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"haha"}); Assertions.assertTrue(server.isCalled()); Assertions.assertEquals("Hello, haha", result); invoker.destroy(); exporter.unexport(); }