You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2022/12/11 13:50:27 UTC

[GitHub] [incubator-eventmesh] jonyangx commented on a diff in pull request #2554: [ISSUE #2545] refactor String concatenation '+=' in loop

jonyangx commented on code in PR #2554:
URL: https://github.com/apache/incubator-eventmesh/pull/2554#discussion_r1045237677


##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientBySystemHandler.java:
##########
@@ -58,38 +58,33 @@ public ShowClientBySystemHandler(EventMeshTCPServer eventMeshTCPServer, HttpHand
      */
     @Override
     public void handle(HttpExchange httpExchange) throws IOException {
-        String result = "";
-        OutputStream out = httpExchange.getResponseBody();
-        try {
+        StringBuffer result = new StringBuffer();
+        try (OutputStream out = httpExchange.getResponseBody()) {
             String queryString = httpExchange.getRequestURI().getQuery();
             Map<String, String> queryStringInfo = NetUtils.formData2Dic(queryString);
             String subSystem = queryStringInfo.get(EventMeshConstants.MANAGE_SUBSYSTEM);
 
             String newLine = System.getProperty("line.separator");
-            logger.info("showClientBySubsys,subsys:{}=================", subSystem);
+            if (LOGGER.isInfoEnabled()) {
+                LOGGER.info("showClientBySubsys,subsys:{}", subSystem);
+            }
             ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping();
             ConcurrentHashMap<InetSocketAddress, Session> sessionMap = clientSessionGroupMapping.getSessionMap();
-            if (!sessionMap.isEmpty()) {
+            if (sessionMap != null && !sessionMap.isEmpty()) {
                 for (Session session : sessionMap.values()) {
                     if (session.getClient().getSubsystem().equals(subSystem)) {
                         UserAgent userAgent = session.getClient();
-                        result += String.format("pid=%s | ip=%s | port=%s | path=%s | purpose=%s", userAgent.getPid(), userAgent
-                                .getHost(), userAgent.getPort(), userAgent.getPath(), userAgent.getPurpose()) + newLine;
+                        result.append(String.format("pid=%s | ip=%s | port=%s | path=%s | purpose=%s",
+                                        userAgent.getPid(), userAgent.getHost(), userAgent.getPort(),
+                                        userAgent.getPath(), userAgent.getPurpose()))
+                                .append(newLine);
                     }
                 }
             }
             NetUtils.sendSuccessResponseHeaders(httpExchange);
-            out.write(result.getBytes(Constants.DEFAULT_CHARSET));
+            out.write(result.toString().getBytes(Constants.DEFAULT_CHARSET));
         } catch (Exception e) {
-            logger.error("ShowClientBySystemAndHandler fail...", e);
-        } finally {
-            if (out != null) {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    logger.warn("out close failed...", e);
-                }
-            }
+            LOGGER.error("ShowClientBySystemAndHandler fail...", e);

Review Comment:
   @walterlife 
   I have deleted redundant code.



##########
eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/ShowClientBySystemHandler.java:
##########
@@ -58,38 +58,33 @@ public ShowClientBySystemHandler(EventMeshTCPServer eventMeshTCPServer, HttpHand
      */
     @Override
     public void handle(HttpExchange httpExchange) throws IOException {
-        String result = "";
-        OutputStream out = httpExchange.getResponseBody();
-        try {
+        StringBuffer result = new StringBuffer();
+        try (OutputStream out = httpExchange.getResponseBody()) {
             String queryString = httpExchange.getRequestURI().getQuery();
             Map<String, String> queryStringInfo = NetUtils.formData2Dic(queryString);
             String subSystem = queryStringInfo.get(EventMeshConstants.MANAGE_SUBSYSTEM);
 
             String newLine = System.getProperty("line.separator");
-            logger.info("showClientBySubsys,subsys:{}=================", subSystem);
+            if (LOGGER.isInfoEnabled()) {
+                LOGGER.info("showClientBySubsys,subsys:{}", subSystem);
+            }
             ClientSessionGroupMapping clientSessionGroupMapping = eventMeshTCPServer.getClientSessionGroupMapping();
             ConcurrentHashMap<InetSocketAddress, Session> sessionMap = clientSessionGroupMapping.getSessionMap();
-            if (!sessionMap.isEmpty()) {
+            if (sessionMap != null && !sessionMap.isEmpty()) {
                 for (Session session : sessionMap.values()) {
                     if (session.getClient().getSubsystem().equals(subSystem)) {
                         UserAgent userAgent = session.getClient();
-                        result += String.format("pid=%s | ip=%s | port=%s | path=%s | purpose=%s", userAgent.getPid(), userAgent
-                                .getHost(), userAgent.getPort(), userAgent.getPath(), userAgent.getPurpose()) + newLine;
+                        result.append(String.format("pid=%s | ip=%s | port=%s | path=%s | purpose=%s",
+                                        userAgent.getPid(), userAgent.getHost(), userAgent.getPort(),
+                                        userAgent.getPath(), userAgent.getPurpose()))
+                                .append(newLine);
                     }
                 }
             }
             NetUtils.sendSuccessResponseHeaders(httpExchange);
-            out.write(result.getBytes(Constants.DEFAULT_CHARSET));
+            out.write(result.toString().getBytes(Constants.DEFAULT_CHARSET));
         } catch (Exception e) {
-            logger.error("ShowClientBySystemAndHandler fail...", e);
-        } finally {
-            if (out != null) {
-                try {
-                    out.close();
-                } catch (IOException e) {
-                    logger.warn("out close failed...", e);
-                }
-            }
+            LOGGER.error("ShowClientBySystemAndHandler fail...", e);

Review Comment:
   done.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org