From d39e3e8873f715e41f8d021e0ac65360737fecd6 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Thu, 14 Feb 2019 19:34:38 +0800 Subject: [PATCH 1/2] optimization: RestProtocol - use `Map#computeIfAbsent` to replace check-and-create - use lambdas to replace annonymous inner types --- .../dubbo/rpc/protocol/rest/RestProtocol.java | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java index 4290ef20b71..966881c3b38 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java @@ -28,16 +28,13 @@ import org.apache.http.HeaderElement; import org.apache.http.HeaderElementIterator; -import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.config.SocketConfig; -import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicHeaderElementIterator; import org.apache.http.protocol.HTTP; -import org.apache.http.protocol.HttpContext; import org.jboss.resteasy.client.jaxrs.ResteasyClient; import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; @@ -65,7 +62,7 @@ public class RestProtocol extends AbstractProxyProtocol { private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEWAITTIME_MS = 1000; private static final int HTTPCLIENTCONNECTIONMANAGER_CLOSEIDLETIME_S = 30; - private final Map servers = new ConcurrentHashMap(); + private final Map servers = new ConcurrentHashMap<>(); private final RestServerFactory serverFactory = new RestServerFactory(); @@ -91,12 +88,11 @@ public int getDefaultPort() { protected Runnable doExport(T impl, Class type, URL url) throws RpcException { String addr = getAddr(url); Class implClass = ApplicationModel.getProviderModel(url.getServiceKey()).getServiceInstance().getClass(); - RestServer server = servers.get(addr); - if (server == null) { - server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER)); - server.start(url); - servers.put(addr, server); - } + RestServer server = servers.computeIfAbsent(addr, addr0 -> { + RestServer s = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER)); + s.start(url); + return s; + }); String contextPath = getContextPath(url); if ("servlet".equalsIgnoreCase(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER))) { @@ -124,13 +120,10 @@ protected Runnable doExport(T impl, Class type, URL url) throws RpcExcept server.deploy(resourceDef, impl, contextPath); final RestServer s = server; - return new Runnable() { - @Override - public void run() { - // TODO due to dubbo's current architecture, - // it will be called from registry protocol in the shutdown process and won't appear in logs - s.undeploy(resourceDef); - } + return () -> { + // TODO due to dubbo's current architecture, + // it will be called from registry protocol in the shutdown process and won't appear in logs + s.undeploy(resourceDef); }; } @@ -159,20 +152,17 @@ protected T doRefer(Class serviceType, URL url) throws RpcException { .build(); CloseableHttpClient httpClient = HttpClientBuilder.create() - .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { - @Override - public long getKeepAliveDuration(HttpResponse response, HttpContext context) { - HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); - while (it.hasNext()) { - HeaderElement he = it.nextElement(); - String param = he.getName(); - String value = he.getValue(); - if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) { - return Long.parseLong(value) * 1000; - } + .setKeepAliveStrategy((response, context) -> { + HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); + while (it.hasNext()) { + HeaderElement he = it.nextElement(); + String param = he.getName(); + String value = he.getValue(); + if (value != null && param.equalsIgnoreCase(Constants.TIMEOUT_KEY)) { + return Long.parseLong(value) * 1000; } - return HTTPCLIENT_KEEPALIVEDURATION; } + return HTTPCLIENT_KEEPALIVEDURATION; }) .setDefaultRequestConfig(requestConfig) .setDefaultSocketConfig(socketConfig) @@ -245,7 +235,7 @@ protected String getContextPath(URL url) { protected class ConnectionMonitor extends Thread { private volatile boolean shutdown; - private final List connectionManagers = Collections.synchronizedList(new LinkedList()); + private final List connectionManagers = Collections.synchronizedList(new LinkedList<>()); public void addConnectionManager(PoolingHttpClientConnectionManager connectionManager) { connectionManagers.add(connectionManager); From 7131e86c9dcfe47c8d73121a1117464309f57443 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Sat, 16 Feb 2019 15:39:31 +0800 Subject: [PATCH 2/2] update as requested --- .../java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java index 966881c3b38..26d0aa00cb9 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestProtocol.java @@ -88,7 +88,7 @@ public int getDefaultPort() { protected Runnable doExport(T impl, Class type, URL url) throws RpcException { String addr = getAddr(url); Class implClass = ApplicationModel.getProviderModel(url.getServiceKey()).getServiceInstance().getClass(); - RestServer server = servers.computeIfAbsent(addr, addr0 -> { + RestServer server = servers.computeIfAbsent(addr, restServer -> { RestServer s = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, DEFAULT_SERVER)); s.start(url); return s;